R 如何使用错误条将两个ggplot点堆叠在彼此的顶部
我试图在ggplot中绘制两个点,这两个点上都有错误条。但是,错误条没有与点同步。这是我正在使用的代码,我附上了下面的图表:R 如何使用错误条将两个ggplot点堆叠在彼此的顶部,r,ggplot2,errorbar,R,Ggplot2,Errorbar,我试图在ggplot中绘制两个点,这两个点上都有错误条。但是,错误条没有与点同步。这是我正在使用的代码,我附上了下面的图表: df = data.frame(rtix = mean(DandI_Variance$`1RTI`[1:11]), rtiy = 50, rtixmin = DandI_Variance$`1RTI`[11], rtixmax = DandI_Variance$`1RTI`[1
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")
我认为ggplot中可能会出现一些混乱,因为在同一个调用中有两个geom_errorbar()和geom_errorbarh()函数。它也只是看起来你正在以一种奇怪的方式构建你的数据框架。与其有一行,为什么不给数据框两行,每行都有标识列 我会尝试这样构造代码作为第一步(希望这能解决问题) 我刚刚将dataframe压缩为2行7列(为type添加一个新的用于颜色),然后我只调用了ggplot2函数一次而不是两次,并将宽度移到aes调用之外(因为aes调用将输入作为名称,而不是值,这意味着0.7的宽度实际上是一个称为“0.7”的因子)不是你想要的,它是一个0.7的数字宽度),并保留颜色(只是因为颜色现在使用的是一列而不是一个名称,请注意,在你的绘图上“蓝色”实际上是红色,反之亦然,这是因为与我上面描述的宽度问题相同的问题)。最后,我添加了手动色标,这样我们可以选择哪种颜色。如果你想换成另一种顺序,你可以把蓝色和红色调来调去
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")
尝试将
width/height=XXX,color=XXX
移出aes()
,因为这些(我假定)是您希望使用的实际值,而不是美学映射。如果这不起作用,请在您的问题中包含dput(df)
的结果,以便我们更好地了解您的数据帧的实际外观。