[英]R: Print ggplot() to png recursively for 120 columns
I have a melted dataset : 我有一个融化的数据集 :
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
There are 120 variables for each of 50 unique IDs. 50个唯一ID中的每一个都有120个变量。
[Edited based on comments] [根据评论编辑]
I would like to create a single plot for each variable, showing the mean & sem for each ID. 我想为每个变量创建一个图,显示每个ID的均值和sem。 I would like the means to be ordered
我想要订购的方式
First: I would like to create a plot for one variable: 首先:我想为一个变量创建一个图:
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")
Now, I would like to do the same thing for all 120 variables. 现在,我想对所有120个变量执行相同的操作。 How do I initialize a for statement to create a plot for each variable?
如何初始化for语句为每个变量创建图?
I tried: 我试过了:
for(i in 1:120)
{unique <- unique(test$variable)
ggplot(unique[i], aes(x = KGID, y = mean))
But that doesn't work. 但这是行不通的。 I want ggplot() to take each unique ID and make the plot.
我希望ggplot()接受每个唯一的ID并进行绘制。
I'd recommend creating all your plots, then saving them. 我建议创建所有图,然后保存它们。
# par() works for base graphics, it does nothing for ggplot
# par(mfrow=c(3, 4)) #just try to plot 12 variables right now
Creating the plots: 创建图:
# 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")
}
Now you can print them in your R session, check them, make sure they look good 现在,您可以在R会话中打印它们,检查它们,确保它们看起来不错
print(my_plots[[1]])
print(my_plots[[8]])
Save them in a for loop: (you could also use lapply
) 将它们保存在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]])
}
Though, if I were you, I think faceting might work much better: 不过,如果我是您,我认为刻面效果会更好:
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)
a lazy option would be (using mtcars as an example) 一个懒惰的选项是(以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()
or, in two steps, 或者分两步
pl = plyr::dlply(mtcars, "carb", "%+%", e1 = p)
ggsave("plot%03d.png", gridExtra::marrangeGrob(pl, nrow=1, ncol=1))
Try adding a print statement around your ggplot
calls as ggplot just returns the ggplot object within a loop and does not print the plot. 尝试在
ggplot
调用周围添加一条打印语句,因为ggplot只会在循环内返回ggplot对象,而不打印该图。 Also I don't think ggplot listens to par(mfrow(
. The standard for a grid of ggplots is grid.arrange
as you can see in this post . You can also just lapply over your variables to store the ggplot objects in a list and print with arrange.grid
via a do.call
command as in this post . Hope this helps! 我也不认为ggplot会监听
par(mfrow(
。ggplots网格的标准是grid.arrange
正如您在这篇文章中看到的那样。您也可以套用变量来将ggplot对象存储在列表中,与打印arrange.grid
通过do.call
命令,在这篇文章 。希望这有助于!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.