R 在ggplot中添加具有边界的新绘图

R 在ggplot中添加具有边界的新绘图,r,ggplot2,R,Ggplot2,我有这些数据: dt1 <- structure(list(yr = 2004:2010, X = c(0.637, 0.9701, 0.701, 0.4535, 0.5058, 0.4698, 0.6228), lower = c(0.4254, 0.6442, 0.4699, 0.2929, 0.3311, 0.3213, 0.4276), upper = c(0.8614, 1.32, 0.955, 0.6261, 0.6901, 0.6276, 0.8385)), .Names =

我有这些数据:

dt1 <- structure(list(yr = 2004:2010, X = c(0.637, 0.9701, 0.701, 0.4535, 0.5058, 0.4698, 0.6228), lower = c(0.4254, 0.6442, 0.4699, 0.2929, 0.3311, 0.3213, 0.4276), upper = c(0.8614, 1.32, 0.955, 0.6261, 0.6901, 0.6276, 0.8385)), .Names = c("yr", "X", "lower", "upper"), row.names = 50:56, class = "data.frame")

dt2 <- structure(list(yr = 2004:2010, X = c(0.1753, 0.2872, 0.3038, 0.1994, 0.2486, 0.235, 0.2604), lower = c(0.1059, 0.1747, 0.1879, 0.1174, 0.1542, 0.1507, 0.1704), upper = c(0.2554, 0.4121, 0.4319, 0.2876, 0.3542, 0.3222, 0.3588)), .Names = c("yr", "X", "lower", "upper"), row.names = 8:14, class = "data.frame")

…同样,对于dt2:


现在我想在同一个图表上显示两个图。我该怎么做呢?

我会在两个数据帧中添加一个新变量(“dt”)(基于原始数据帧的名称),然后将它们重新绑定到一个新的数据帧中(dt3)

更改点和线的颜色(添加:color=dt,linetype=0,更改:而不是fill=“blue”:aes(fill=dt))。最后两行用于定制颜色

myplot<-ggplot(dt3, aes(x=yr, y=X, group=dt, colour=dt,ymin = lower, ymax = upper)) +  
geom_ribbon(alpha = 0.2, linetype=0)+ geom_line() +  
geom_point(shape=21, size=3, aes(fill=dt)) +  
theme_gray(12) +  
opts(panel.background = theme_rect(fill='grey80')) +  
ylim(0,1.7)  
myplot<-myplot+ scale_color_manual(values=c("red", "blue"))  
myplot<-myplot+ scale_fill_manual(values=c("red", "blue"))  
myplot  

myplot我通过反复试验找到了这个答案:

ggplot(dt1, aes(x=yr, y=X, group=1, ymin = lower, ymax = upper)) +
    geom_ribbon(alpha = 0.2) +
    geom_line() +
    geom_point(shape=21, size=3, fill="blue") +
    theme_gray(12) +
    opts(panel.background = theme_rect(fill='grey80')) +
    ylim(0,1.7) +
    geom_path(aes(x=yr, y=X, group=1, ymin = lower, ymax = upper), data=dt2) +
    geom_ribbon(alpha = 0.2, data=dt_inh) +
    geom_point(shape=21, size=3, fill="red", data=dt2)


这是一个好方法吗?

谢谢!(+1)我想你是在我写我自己的解决方案时发布的。我喜欢你的方法,因为它使代码更紧凑,但是添加额外的变量有点不方便……顺便说一句,在你的方法中,我如何控制点的颜色-即,将一个设置为蓝色,另一个设置为红色?你应该将另一个输入添加到aes():color=dt,并更改geom_ribbon()的线型代码:ggplot(dt3,aes(x=yr,y=x,group=dt,color=dt,ymin=lower,ymax=upper))+geom_色带(alpha=0.2,线型=0)+geom_线()+geom_点(shape=21,size=3,fill=“black”)+主题灰(12)+选项(panel.background=theme_rect(fill='grey80'))+ylim(0,1.7)再次感谢。您修改的代码更改了线条的颜色,但点都是黑色的。有没有办法也更改点的颜色,或者只更改点并保持线条为黑色;或者两者都更改?
ggplot(dt3, aes(x=yr, y=X, group=dt, ymin = lower, ymax = upper)) +  
     geom_ribbon(alpha = 0.2) +  
     geom_line() +  
     geom_point(shape=21, size=3, fill="blue") +  
     theme_gray(12) +  
     opts(panel.background = theme_rect(fill='grey80')) +  
     ylim(0,1.7)  
myplot<-ggplot(dt3, aes(x=yr, y=X, group=dt, colour=dt,ymin = lower, ymax = upper)) +  
geom_ribbon(alpha = 0.2, linetype=0)+ geom_line() +  
geom_point(shape=21, size=3, aes(fill=dt)) +  
theme_gray(12) +  
opts(panel.background = theme_rect(fill='grey80')) +  
ylim(0,1.7)  
myplot<-myplot+ scale_color_manual(values=c("red", "blue"))  
myplot<-myplot+ scale_fill_manual(values=c("red", "blue"))  
myplot  
ggplot(dt1, aes(x=yr, y=X, group=1, ymin = lower, ymax = upper)) +
    geom_ribbon(alpha = 0.2) +
    geom_line() +
    geom_point(shape=21, size=3, fill="blue") +
    theme_gray(12) +
    opts(panel.background = theme_rect(fill='grey80')) +
    ylim(0,1.7) +
    geom_path(aes(x=yr, y=X, group=1, ymin = lower, ymax = upper), data=dt2) +
    geom_ribbon(alpha = 0.2, data=dt_inh) +
    geom_point(shape=21, size=3, fill="red", data=dt2)