0
votes

Please bear with me as I'm quite new to R and specially to ggplot(). This is the second time I'm asking something regarding the use of this function. I would like to create a plot which resembles the one below:

enter image description here

I've been using a script looking like this for such purpose:


df <- tibble::tribble(~Proportion, ~Error, ~Time,
                      0.351 , 0.154, "Day",
                      0.223 , 0.157 , "Night")

dfnew <- df %>% 
  mutate(ymin = Proportion - Error,
         ymax = Proportion + Error)

p <-   ggplot(data = dfnew, aes(x = Time, y = Proportion)) +
  geom_point() + geom_line(aes(group = 1), color="lightblue",size=2) + 
  geom_errorbar(aes(x = Time, ymin = ymin, ymax = ymax),color="purple",width=0.1,size=1)


p<-p+theme(axis.text=element_text(size=12),
        axis.title=element_text(size=14))

However, I now face the problem of having data for confidence intervals that includes the upper and lower values, instead of an error value as I have on the script above. My data looks like this:

> df
# A tibble: 2 x 4
  Average Lower Upper Time 
    <dbl> <dbl> <dbl> <chr>
1   0.351 0.284 0.421 Day  
2   0.223 0.178 0.275 Night

Any idea on how I can implement this two Lower and Upper values into error bars?

Any input is appreciated!

1
just pass the appropriate columns via aes() eg geom_errorbar(aes(y= Average, ymin = Lower, ymax = Upper))? - Nate
@Nate Yes. I'm just concerned about the Warning: How can I remove it? > dfnew <- df %>% + mutate(ymin = Proportion - Lower, + ymax = Proportion + Upper) > p <- ggplot(data = dfnew, aes(x = Time, y = Proportion)) + + geom_point() + geom_line(aes(group = 1), color="lightblue",size=2) + + geom_errorbar(aes(y= Proportion, ymin = Lower, ymax = Upper),color="purple",width=0.1,size=1) Warning: Ignoring unknown aesthetics: y > p<-p+theme(axis.text=element_text(size=12), + axis.title=element_text(size=14)) - juansalix
geom_errorbar() is complaining about the y = Average I said you should include (my b). The errorbar only wants to know the high and the low, so it ignored the y. Removing the y = should remove the warning - Nate
@If I do that, then I'm left with a plot where the error bars are not where they're supposed to be. You should be able to replicate the error with the sample I posted - juansalix

1 Answers

0
votes

Does this help?

Using the new data:

df2 <- read.table(
  header = T,
  stringsAsFactors = F,
  text = "  Average Lower Upper Time
1   0.351 0.284 0.421 Day  
2   0.223 0.178 0.275 Night"
)

And adapting the plot data source a bit:

ggplot(data = df2, aes(x = Time, y = Average)) +
  geom_point() +
  geom_line(aes(group = 1), color="lightblue",size=2) + 
  geom_errorbar(aes(x = Time, ymin = Lower, ymax = Upper),
                color="purple",width=0.1,size=1)

I get this graph out:

enter image description here

Which looks corrected to my eyes, but I might be missing something that you pick up on. Let me know..