0

First ever question so sorry if I don't get the formatting correct.

PROBLEM

Hi everyone, I am having some trouble overcoming this error. I am trying to conditionally jitter points and error bars in ggplot2. The MeVtrue variable is a column with only true and false values in a larger dataset

MeVtrue : logi [1:576] FALSE FALSE FALSE TRUE FALSE FALSE ...

Currently plot generates error bars only if the coefficient of variation (CV) of my values are above a certain amount. This results in scenarios where the error bars are stacked on top of each other. I have written code to jitter all the points and error bars, but I am trying to figure out how to only jitter the points/error bars that are identified as above the CV trheshold.

After writing this question and typing out all the potential attempts giving me the same error, I'm guessing my issue has something to do with how the data is stored in that column. Any advice would be appreciated.

WHAT I DID

Here is some sample code of me trying to replicate this question; can not use conditions in ggplot with position arguments - conditional dodge

pd_true = position_jitter()
pd_false = position_identity()

ggplot(data = testtrue, aes(x = Day, y =MeV,  
                            shape = Temp, color = Temp)) +
  geom_line(aes(linetype = Temp)) +
  geom_hline(yintercept = 288, linetype = 'dashed') +
  geom_point(position = if(unique(testtrue$MeVtrue)) pd_true else pd_false)

**and these variations as well: **

geom_point(position = if (MeVtrue) pd_true else  pd_false)
geom_point(position = {
    if (MeVtrue == TRUE) {
      pd_true
    } else {  pd_false
      }
    }
    ) 
  {
    if ( MeVtrue ){
      geom_point(position = postion_jitter)
    } else {
      geom_point(position = "identity")
    }
  } 

Give me this error: In if (unique(testtrue$MeVtrue)) pd_true else pd_false : the condition has length > 1 and only the first element will be used

I'm fairly new to R and coding in general, but to from what I understand I thought a logical vector should work here. In my googling it seems the common advice to overcome this error is to use ifelse() however, if I use ifelse() this is what happens:

Error in rep(yes, length.out = len) : attempt to replicate an object of type 'environment'

Here is the code for that

 geom_point(position = ifelse(MeVtrue, pdtrue, pdtrue))

Error in rep(yes, length.out = len) : attempt to replicate an object of type 'environment'

ANSWER

Edit: I figured out how to do what I was trying without resolving the error. Instead I just had two geom_point calls subsetted by whether my MeVtrue was true or false. :) much easier than whatever I was trying to do!

geom_point( 
    data = subset(testtrue, MeVtrue == TRUE),
    position = position_jitter(seed = 7, width = .2, height = 0)) +
  geom_point(
    data = subset(testtrue, MeVtrue == FALSE),
    position = "identity")

Thank you: R selective ggplot geom_point(position = position_dodge())

I am going to post this question anyway because I spent too long writing it and dealing with that issue above. And I need reputation points so I can go back and upvote that other guy's answer.

mn334
  • 1
  • 3

0 Answers0