I am new to R and have a problem with ggplot and the following dataset (chosen as representative from larger set) where geom_errorbar
bars do not align with the mean point (using geom_point) and in several cases the horizontal bars do not align with the vertical bar in geom_errorbar
, so that instead of coming out as an "I" with cross bars at top and bottom, the crossbars are separated from the vertical line or off-center.
I have looked at all the man pages for ggplot
, geom_point
, geom_errorbar
, position_jitter
(dodge, jitterdodge). I have also tried a bunch of things from here, such as altering the aesthetics within the geom_point
and geom_errorbar
calls (e.g. How to make dodge in geom_bar agree with dodge in geom_errorbar, geom_point)
Here's a basic data set:
df <- structure(list(
Test = c("A", "B", "C", "D", "A", "C", "D"),
mean = c(1, 100.793684, 1, 1, 51.615601, 1, 2.456456),
sd = c(1, 2.045985, 1, 1, 4.790053, 1, 4.250668),
lower = c(2, 102.839669, 2, 2, 56.405654, 2, 6.707124),
upper = c(0, 98.747699, 0, 0, 46.825548, 0, -1.79421)),
row.names = c(NA, -7L), class = c("tbl_df", "tbl", "data.frame"))
Now the code I am using:
subplot <- ggplot(df, aes(x = Test, y = mean)) +
geom_point(aes(x= Test, y = mean),
position = position_jitter(width = 0.2, height = 0.2))+
geom_errorbar(aes(ymin = lower, ymax = upper),
width = 0.1,
position = position_jitter(width = 0.2, height = 0.2))
subplot
This is what I get:
I suspect it is something basic that I have missed. I have used the same code in line plots and other scatter plots and it has been fine, so I am lost as to what I have done. I have tested it on two different installations of R on separate computers too.
Any help greatly appreciated.