R 从数据帧列表生成多个绘图-带颜色(ggplot2)
我试图在ggplot2中绘制9个线图,每个线图使用列表(称为“我的列表”)中每个数据帧的数据 我在这里找到了一个简洁的解决方案: 但我需要一个额外的扭曲:每个情节上的线条需要有不同的颜色R 从数据帧列表生成多个绘图-带颜色(ggplot2),r,ggplot2,R,Ggplot2,我试图在ggplot2中绘制9个线图,每个线图使用列表(称为“我的列表”)中每个数据帧的数据 我在这里找到了一个简洁的解决方案: 但我需要一个额外的扭曲:每个情节上的线条需要有不同的颜色 编辑-我找到了答案,请参见下面的答案。我设法找到了答案,我想分享我学到的东西 # first create a vector with the colors you want colors = c("red", "blue", "green", "purple", "cyan", "orange", "tea
编辑-我找到了答案,请参见下面的答案。我设法找到了答案,我想分享我学到的东西
# first create a vector with the colors you want
colors = c("red", "blue", "green", "purple", "cyan", "orange", "teal", "pink", "maroon")
# then loop through the colors in geom_line (as opposed to doing it in the ggplot aes, that gave me lines of the same color every time).
for(i in 1:length(my_list))
{
df1 = as.data.frame(my_list[[i]])
plotdf1 <- ggplot(df1, aes(x=x, y=y)) +
geom_line(size=1,color=colors[i])
print(plotdf1)
}
#首先创建一个带有所需颜色的向量
颜色=c(“红色”、“蓝色”、“绿色”、“紫色”、“青色”、“橙色”、“青色”、“粉红”、“栗色”)
#然后在geom_行中循环使用颜色(与在ggplot aes中循环相反,它每次都给我相同颜色的行)。
对于(i in 1:长度(我的清单))
{
df1=as.data.frame(我的_列表[[i]])
plotdf1我认为定义总结函数更容易
dl = replicate(5, data.frame(x=1:10, y=rnorm(10)), simplify = FALSE)
plot_fun = function(d, col) {
ggplot(d, aes_string(x="x",y="y")) + geom_line(col=col) +
theme()
}
pl = mapply(plot_fun, d = dl, col = palette()[1:5], SIMPLIFY=FALSE)
# interactive use
gridExtra::grid.arrange(grobs=pl)
## save to a device
ggsave("plotstosave.pdf", gridExtra::arrangeGrob(grobs=pl))
您好,有什么关于动态保存地块的建议吗?谢谢