【发布时间】:2017-11-08 00:29:01
【问题描述】:
我尝试使用for loop 将虹膜数据集中的每个Species 数据保存为.png 文件。但在此之前,我想在实际数据绘图过程中修改刻面带厚度。
但是,当我尝试编写每个方面时 下面的代码只是给了我每个物种的空图。
这是我的尝试,
library(ggplot2)
plot_list = list()
for (i in unique(iris$Species)) {
p = ggplot(iris[iris$Species == i, ], aes(x=Sepal.Length, y=Sepal.Width)) +
geom_point(size=3, aes(colour=Species))+
facet_wrap(~Species)
#this part to modify facet_wrap strips
g1 = ggplotGrob(p)
pos = c(unique(subset(g1$layout, grepl("panel", g1$layout$name), select = t)))
for(i in pos) g1$heights[i-1] = unit(0.4,"cm")
grobs = which(grepl("strip", g1$layout$name))
for(i in grobs) g1$grobs[[i]]$heights <- unit(1, "npc")
grid.newpage()
grid.draw(g1)
plot_list[[i]] = g1
}
#finally write the modified graphs to file
for (i in 1:3) {
file_name = paste("iris_plot_", i, ".png", sep="")
tiff(file_name)
print(plot_list[[i]])
dev.off()
}
目前这段代码正在生成空图,不知道为什么!任何帮助将不胜感激!
【问题讨论】: