我有五个线图,我想从中输出一个阴影区域,代表它们绘制的上部和下部区域之间的区域。我创建了一个R脚本(见下文),因为我有多个数据集,需要重复这个练习。R ggplot2:使用嵌套循环在单个图中覆盖多个geom_ribbon对象
但是,我只能打印来自最后一个i和j对的geom_ribbon - 我似乎无法将每个geom_ribbon输出到创建的列表中。
我很感激任何有关如何将所有geom_ribbon对象导入列表的想法。 print(Z)
(下面的例子)只打印一张图。如果可能的话,我想要将所有geom_ribbon对象重叠并打印为单个ggplot?
Z <- list()
allmaxi <- list(cahp_max_plot15cb$decade_maxa, cahp_max_plot15cb$decade_maxc,cahp_max_plot15cb$decade_maxd, cahp_max_plot15cb$decade_maxe, cahp_max_plot15cb$decade_maxf)
allmaxj <- list(cahp_max_plot15cb$decade_maxa, cahp_max_plot15cb$decade_maxc,cahp_max_plot15cb$decade_maxd, cahp_max_plot15cb$decade_maxe, cahp_max_plot15cb$decade_maxf)
for (i in allmaxi) {
for (j in allmaxj) {
l <- geom_ribbon(data=cahp_max_plot15cb,aes(x=decade,ymin=i, ymax=j))
Z[[length(Z) + 1]] <- l
print(i)
print(j)
}
}
print(ggplot() + Z)
样本输出(从打印(i)和印刷(j)在脚本)输入来自一个数据集(decade_maxa)至i列出,以及四个其他数据集到j列表:
[1] 2010.811 1723.783 1961.088 1662.909 1587.191 1662.140 1665.415 1602.974 1807.453 1586.106
[11] 1580.880 1685.253 1653.178 1824.842
[1] 1390.260 1247.700 1263.578 1711.638 1228.326 1762.045 1260.147 1171.914 1697.987 1350.867
[11] 1434.525 1488.818 1610.513 1536.895
`
`[1] 2010.811 1723.783 1961.088 1662.909 1587.191 1662.140 1665.415 1602.974 1807.453 1586.106
[11] 1580.880 1685.253 1653.178 1824.842
`
`[1] 1120.2700 1094.3047 1196.8792 1227.9660 1236.9170 1266.0935 1127.1480 974.6948 947.3365
[10] 1244.3242 1254.2704 1082.3667 1286.9080 1126.1943
`
`[1] 2010.811 1723.783 1961.088 1662.909 1587.191 1662.140 1665.415 1602.974 1807.453 1586.106
[11] 1580.880 1685.253 1653.178 1824.842
`
`[1] 1396.695 1425.073 1382.941 1913.495 1401.754 1499.763 1600.656 1367.043 1413.390 1343.804
[11] 1431.790 1402.292 1329.192 1696.729
`
`[1] 2010.811 1723.783 1961.088 1662.909 1587.191 1662.140 1665.415 1602.974 1807.453 1586.106
[11] 1580.880 1685.253 1653.178 1824.842
`
`[1] 1718.874 1389.134 1501.574 1233.189 1262.480 1508.919 1291.467 1431.869 1505.102 1376.519
[11] 1441.181 1421.552 1326.547 1635.599
`
> print(ggplot() + Z)
`
这是我的目标。也许乐队有更好的方法吗?
这是通过整合中值的输出图像,如下建议:
median_g <- group_by(cahp_max_plot15cbm,decade) median_gm <- mutate(median_g, median=median(value)) p2 <- ggplot(median_gm) + geom_ribbon(aes(x=decade, ymin=median,ymax=value,group=variable),alpha=0.40,fill="#3985ff") + geom_line(aes(x=decade,y=value,group=variable,color=variable),lwd=1) + geom_point(aes(x=decade,y=median)) p2
它出现在变量名(仅i和j)不被传递到列表:>ž [[1]] 映射:X =十年中,YMIN = I,YMAX = j geom_ribbon:na。RM = FALSE stat_identity: position_identity:(宽度= NULL,高度= NULL) [[2]] 映射:X =十年中,YMIN = I,YMAX = j的 geom_ribbon:na.rm = FALSE stat_identity: position_identity:(宽度= NULL,高度= NULL) [[3]] 映射:X =十年中,YMIN = I,YMAX = j的 geom_ribbon:na.rm = FALSE stat_identity: position_identity:(宽度= NULL,height = NULL) ... – 2015-08-28 07:39:47
为什么你需要这个循环?更好的方法可能是重塑你的数据。更易读,更简单的绘图定制。 – Heroka