R:递归地将ggplot()打印到png 120列

时间:2015-08-06 17:17:14

标签: r ggplot2

我有一个已融化的数据集

 ID variable        mean          sd         sem
1 0001      1 0.000000000 0.000000000 0.000000000
2 0001      2 0.000000000 0.000000000 0.000000000
3 0001      3 1.374013050 0.083787761 0.001524927
4 0001      4 1.622939744 0.232510250 0.004231658
5 0001      5 0.004092427 0.004076841 0.000074198

每个50个唯一ID共有120个变量。

[根据评论编辑]

我想为每个变量创建一个单独的图,显示平均值& sem为每个ID。我想订购手段

第一: 我想为一个变量创建一个图:

  ggplot(subset(dataset, variable=="1"), aes(x = ID, y = mean)) +
    geom_bar(position = position_dodge(), stat = "identity") + 
    geom_errorbar(aes(ymin=mean-sem, ymax=mean+sem))  +
    theme(axis.title.x = element_text(face="bold",size=16),
          axis.text.x = element_text(angle=90, colour = "black", vjust=1, hjust        = 1, size=14),
          axis.text.y = element_text(colour = "black", size=14),
          axis.title.y = element_text(face="bold", size=16),
          plot.title = element_text(size = 18),
          legend.title = element_text(size=14),
          legend.text = element_text(size = 13),
          legend.position="right",
          strip.text.x = element_text(size=12, face="bold"),
          strip.text.y = element_text(size=12, face="bold"),
          strip.background = element_rect(colour="black")) + ylab("Mean") + xlab("ID") 

现在,我想对所有120个变量做同样的事情。如何初始化for语句以为每个变量创建一个图?

我试过了:

for(i in 1:120)
{unique <- unique(test$variable)
ggplot(unique[i], aes(x = KGID, y = mean))

但这不起作用。我希望ggplot()获取每个唯一ID并制作图。

3 个答案:

答案 0 :(得分:2)

我建议您创建所有图表,然后保存它们。

# par() works for base graphics, it does nothing for ggplot
# par(mfrow=c(3, 4)) #just try to plot 12 variables right now

创建图:

# initialize a list to put the plots in
my_plots = list()

u <- unique(dataset$variable) #Get all unique values for variable

for(i in 1:length(u)) {
    # I just put your data subset inside the plot
    my_plots[[i]] = ggplot(test[dataset$variable==u[i], ],
                           aes(x = ID, y = mean)) +  
        geom_bar(position = position_dodge(), stat = "identity") + 
        geom_errorbar(aes(ymin=mean-sem, ymax=mean+sem))  +
          # I deleted all your theme stuff to keep this minimal and 
          # easy to understand, you can add it back in
        ylab("Mean") + xlab("ID") 
}

现在您可以在R会话中打印它们,检查它们,确保它们看起来很好

print(my_plots[[1]])
print(my_plots[[8]])

将它们保存在for循环中:(您也可以使用lapply

for (i in 1:length(my_plots)) {
    ggsave(filename = paste0("plot_", i, ".png"),
           # you can specify height, width, cairodevice, etc.
           plot = my_plots[[i]])
}

但是,如果我是你,我认为分面可能会更好:

ggplot(dat, aes(x = ID, y = mean)) +  
    geom_bar(position = position_dodge(), stat = "identity") + 
    geom_errorbar(aes(ymin=mean-sem, ymax=mean+sem))  +
    ylab("Mean") + xlab("ID") +
    facet_wrap(~ variable)

答案 1 :(得分:1)

一个懒惰的选项(使用mtcars作为例子)

p = ggplot(mtcars, aes(x = hp, y = drat)) + geom_point()

png("plot%03d.png")
plyr::d_ply(mtcars, "carb", "%+%", e1 = p, .print=TRUE)
dev.off()

或者,分两步,

pl = plyr::dlply(mtcars, "carb", "%+%", e1 = p)
ggsave("plot%03d.png", gridExtra::marrangeGrob(pl, nrow=1, ncol=1))

答案 2 :(得分:0)

尝试在ggplot调用周围添加一个print语句,因为ggplot只返回循环中的ggplot对象而不打印该图。我也不认为ggplot会听par(mfrow(。正如您在this post中所看到的那样,ggplots网格的标准为grid.arrange。您还可以只使用变量来将ggplot对象存储在列表中,并使用arrange.grid命令通过do.call命令进行打印,如this post所示。希望这有帮助!