在for循环中重命名ggplot2图 [英] renaming ggplot2 graphs in a for loop
本文介绍了在for循环中重命名ggplot2图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个关于在for循环中创建ggplot2图形的问题,根据迭代对它们进行重命名,然后将图形排列在一个网格中。
我想要做的类似这个虚拟示例
library(ggplot2)
a = c(1,2,3) $(b
$ bb = c(4,5,6)
for(i in c(1:5)){
x = i * a
y = i * b
p = qplot(x,y)
...做一些将p重命名为plot_i ...
}
...做一些事情来绘制图块 plot_1 ...
plot_6
转换为2 x 3格子
有什么建议吗?您可以将这些图保存到列表中:
(1,2,3,4)
b <-c(4,5,...,c)(code $ c $ library $ g
$ 6)
out< - NULL
for(i in 1:10){
take< - data.frame(a = a * i,b = b * i)
出[[i]] < - ggplot(取,aes(x = a,y = b))+ geom_point()
}
grid.arrange(out [[1]],out [[10]],out [ 2]],out [[5]],nrow = 2)
I have a question about creating ggplot2 graphs in a for loop, renaming them based on the iteration and then arranging the graphs in a grid.
I want to do something like this dummy example
library(ggplot2)
a = c(1, 2, 3)
b = c(4, 5, 6)
for ( i in c(1:5)){
x = i*a
y = i*b
p = qplot(x, y)
... do something to rename p as plot_i...
}
... do something to arranage plots plot_1
... plot_6
into a 2 x 3 grid
Any suggestions?
解决方案
You could save the plots into a list:
library(ggplot2)
library(gridExtra)
a <- c(1, 2, 3)
b <- c(4, 5, 6)
out <- NULL
for (i in 1:10){
take <- data.frame(a = a * i, b = b * i)
out[[i]] <- ggplot(take, aes(x = a, y = b)) + geom_point()
}
grid.arrange(out[[1]], out[[10]], out[[2]], out[[5]], nrow = 2)
这篇关于在for循环中重命名ggplot2图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文