0
votes

I am plotting error bars on my data using output from the summarySE() function in the Rmisc package. I would like to change the width of the horizontal lines on each error bar. For some reason, width is not working properly.

I have looked at solutions and NONE of them worked:

Width of error bars in ggplot2

http://www.sthda.com/english/wiki/ggplot2-error-bars-quick-start-guide-r-software-and-data-visualization

ggplot2 position_dodge affects error bar width

Nothing has worked. Here is a snip it of my data:

df <- structure(list(yrmonth = structure(c(1456790400, 1456790400, 
                                 1456790400, 1459468800, 1459468800, 1459468800, 1462060800, 1462060800, 
                                 1462060800, 1464739200, 1464739200, 1464739200), class = c("POSIXct", 
                                                                                            "POSIXt"), tzone = "UTC"), index = structure(c(1L, 4L, 5L, 1L, 
                                                                                                                                           4L, 5L, 1L, 4L, 5L, 1L, 4L, 5L), .Label = c("N-S", "N-S", "E-W", 
                                                                                                                                                                                       "E-W", "OS"), class = "factor"), N = c(2, 1, 1, 2, 1, 1, 2, 1, 
                                                                                                                                                                                                                              1, 2, 1, 1), GDDTomatoes = c(151, 136, 61, 221.5, 211, 151, 273, 
                                                                                                                                                                                                                                                           253, 207, 376, 386, 362), sd = c(7.07106781186548, NA, NA, 3.53553390593274, 
                                                                                                                                                                                                                                                                                            NA, NA, 0, NA, NA, 5.65685424949238, NA, NA), se = c(5, NA, NA, 
                                                                                                                                                                                                                                                                                                                                                 2.5, NA, NA, 0, NA, NA, 4, NA, NA), ci = c(63.5310236808735, 
                                                                                                                                                                                                                                                                                                                                                                                            NA, NA, 31.7655118404367, NA, NA, 0, NA, NA, 50.8248189446988, 
                                                                                                                                                                                                                                                                                                                                                                                            NA, NA)), .Names = c("yrmonth", "index", "N", "value", 
                                                                                                                                                                                                                                                                                                                                                                                                                 "sd", "se", "ci"), row.names = c(NA, 12L), class = "data.frame")

Here is one of my ggplot attempts that didn't work. When I use the width parameter, no matter what number I put in width the horizontal lines disappear altogether. I would just like to shorten them a bit.

ggplot(df, aes(x=yrmonth,y=value,colour=factor(index))) + 
  geom_errorbar(aes(ymin=value-se, ymax=value+se), width=0.5) +
  geom_line() 
1
Possible duplicate of Width of error bars in ggplot2 - Adam Quek
@AdamQuek Unless I am not understanding something in that answer, I could not get it to work on my problem. - phaser
geom_errorbar width adjust relative width based on the number of levels of factors of x. In your data, the x value is a continuous gradient. Change it to a factor and you will be able to manipulate the width for geom_errorbar. Alternatively, go with geom_ribbon. - Adam Quek
Look at the numeric values of yrmonth and then look at the numeric value of the width parameter you're trying. You should probably try something along the lines of width = 1e6 to get them to show up at that scale. - Brian
yrmonth is encoded as numbers of seconds since 1970. That means the space between two values on the x-axis is a few million seconds. If your errorbar was only half a second wide, it would be invisible. - Brian

1 Answers

1
votes
ggplot(df, aes(x=as.factor(yrmonth),y=value)) + 
  geom_point() +
  geom_errorbar(aes(ymin=value-se, ymax=value+se), width=.5) +
  geom_line(aes(x=as.numeric(as.factor(yrmonth)))) +
  facet_wrap(~index)

enter image description here