I am attempting to plot two points that have error bars on them on top of each other in ggplot. However, the error bars are not syncing up with the points. This is the code that I'm using, and I have my ensuing graph attached:
df = data.frame(rtix = mean(DandI_Variance$`1RTI`[1:11]),
rtiy = 50,
rtixmin = DandI_Variance$`1RTI`[11],
rtixmax = DandI_Variance$`1RTI`[1],
rtiymin = 52,
rtiymax = 42,
rtcx = mean(DandI_Variance$`1RTC`[1:11]),
rtcy = 75,
rtcxmin = DandI_Variance$`1RTC`[11],
rtcxmax = DandI_Variance$`1RTC`[1],
rtcymin = 69,
rtcymax = 79)
ggplot(data = df, aes(x = rtix, y = rtiy)) +
geom_point() +
geom_errorbar(aes(ymin = rtiymin, ymax = rtiymax, width = .07, color = "blue")) +
geom_errorbarh(aes(xmin = rtixmin, xmax = rtixmax, height = 10, color = "blue")) +
geom_point(aes(x = rtcx, y = rtcy)) +
geom_errorbar(aes(ymin = rtcymin, ymax = rtcymax, width = .07, color = "red")) +
geom_errorbarh(aes(xmin = rtcxmin, xmax = rtcxmax, height = 10, color = "red")) +
xlab("S Equalibrium") +
ylab("Time to Equalibrium") +
ylim(0, 100) +
xlim(0, 1) +
ggtitle("Performance of Models")
I think there might be some confusion caused within ggplot since you have the geom_errorbar() and geom_errorbarh() functions twice in the same call. It also just looked like you're structuring your data frame in a weird way. Rather than having one row, why not give your data frame 2 rows, each with identifying columns?
I'd try structuring the code like this as a first step (hopefully this solves it).
I've just compressed the dataframe into 2 rows and 7 columns (adding a new one for type to use for color), I've then just called the ggplot2 functions once rather than twice, and moved the width outside of the aes call (since the aes call take the inputs as names, not values, this means a width of 0.7 is actually a factor called "0.7" not what you're desiring, which is a numerical width of 0.7) and kept the color in (only because color is now using a column instead of a name, notice on your plot "blue" is actually red and vice versa, that's because of the same problem as the width issue I described above). Finally I've added the manual colour scale so we can choose which has which colour. You can switch blue and red around if you want it in the other order.
df = data.frame(rtx = c(mean(DandI_Variance$`1RTI`[1:11]),
mean(DandI_Variance$`1RTC`[1:11])),
rty = c(50,75),
rtxmin = c(DandI_Variance$`1RTI`[11],
DandI_Variance$`1RTC`[11]),
rtxmax = c(DandI_Variance$`1RTI`[1],
DandI_Variance$`1RTC`[1]),
rtymin = c(52,69),
rtymax = c(42,79),
rttype = c('I', 'C')
)
ggplot(data = df, aes(x = rtx, y = rty)) +
geom_point() +
geom_errorbar(aes(ymin = rtymin, ymax = rtymax, color = rttype), width = .07) +
geom_errorbarh(aes(xmin = rtxmin, xmax = rtxmax, color = rttype), height = 10) +
scale_color_manual(values = c("blue", "red")) +
xlab("S Equalibrium") +
ylab("Time to Equalibrium") +
ylim(0, 100) +
xlim(0, 1) +
ggtitle("Performance of Models")