I am doing an exploratory analysis of my data and need to plot multiple graphics using ggplot. The amount of graphics is really huge (206 Stations), and I wanted to plot them in 1 column vs. 8 rows per page over the so many pages needed. I am aware of functions like viewport or grid.arrange, but I am not managing to make them work in this case. I have already noticed that layout() nor par(mfrow=c(8,1)) do not work with ggplot, but I send the part of the code where I am stuck bellow. Any help would be much appreciated!
我正在对我的数据进行探索性分析,需要使用ggplot绘制多个图形。图形数量非常庞大(206个工作站),我希望在所需的这么多页面上将它们绘制成1列而不是每页8行。我知道viewport或grid.arrange之类的函数,但我不能让它们在这种情况下工作。我已经注意到layout()和par(mfrow = c(8,1))不能与ggplot一起使用,但是我发送了代码的一部分,我被卡在了下面。任何帮助将非常感激!
pdf('test.pdf', width=21, height=27)
par(mfrow=c(8,1))
for(i in levels(tab$Station))
{
print(ggplot(tab[tab$Station==i], aes(x=Date)) +
geom_line(aes(y=Tmin), col="blue", size=0.1) +
geom_line(aes(y=Tmax), col="red", size=0.1) +
geom_text(aes(x=as.Date('2010-01-01'), y=45), label=i) +
ylim(0, 45) +
scale_x_date(labels = date_format("%Y")) +
theme_bw() +
theme(
plot.background = element_blank()
,panel.grid.major = element_blank()
,panel.grid.minor = element_blank()
,panel.border = element_rect(color = 'black')
,panel.background = element_blank()
)
)
}
dev.off()
4 个解决方案
#1
10
library(plyr)
library(gridExtra)
p = ggplot(tab, aes(x=Date)) +
geom_line(aes(y=Tmin), col="blue", size=0.1)
plots = dlply(tab , "Station", `%+%`, e1 = p)
ml = do.call(marrangeGrob, c(plots, list(nrow=8, ncol=1)))
ggsave("multipage.pdf", ml)
untested.
未经测试。
#2
1
You should simplify your plot since once you get the right order with a simple plot you just replace it with your complicated one. ggplot2
are based on grid
package so you need to use gridExtra
to arrange your plots. Then you loop through , for each 8 plots, you store them in a list and you call grid.arrange
over it, and you repeat this until the end of your plots...
您应该简化您的情节,因为一旦您通过简单的情节得到正确的顺序,您只需将其替换为复杂的情节。 ggplot2基于网格包,因此您需要使用gridExtra来排列图。然后你循环,对于每8个图,你将它们存储在一个列表中,然后在它上面调用grid.arrange,然后你重复这个直到你的图结束......
library(gridExtra)
library(ggplot2)
pdf('test.pdf', width=21, height=27)
i = 1
plot = list()
for (n in unique(tab$Station)){
### process data for plotting here ####
plot[[i]] = ggplot(tab[tab$Station==n], aes(x=Date)) +...
if (i %% 8 == 0) { ## print 8 plots on a page
print (do.call(grid.arrange, plot))
plot = list() # reset plot
i = 0 # reset index
}
i = i + 1
}
if (length(plot) != 0) {
print (do.call(grid.arrange, plot))
}
dev.off()
#3
1
Faceting might be the way to go. Decide how many faceted mini-plots you want per page, then loop through the required number of times, generating a png or a pdf as you go. So if you have 200 data items and you want 50 per page, in facets of 5 across and 10 down, just loop through 200/50 = 4 iterations. Crude, but should work.
分面可能是要走的路。确定每页需要多少个刻面迷你图,然后循环所需的次数,随时生成png或pdf。因此,如果您有200个数据项并且每页需要50个数据项,则在5个横向和10个向下的方面,只需循环200/50 = 4个迭代。原油,但应该工作。
library(ggplot2)
ii <- 7
nn <- 49
mydf <- data.frame(date = rep(seq(as.Date('2013-03-01'),
by = 'day', length.out = ii), nn),
value = rep(runif(nn, 100, 200)))
mydf$facet.variable <- rep(1:nn, each = ii)
p <- ggplot(mydf, aes(x = date, y = value)) +
geom_line() +
facet_wrap(~ facet.variable, ncol = ii)
print(p)
#4
0
Unfortunately, mfrow
doesn't work with ggplot2
. You have to use other methods like this one or this one or use the native plot
function.
不幸的是,mfrow不适用于ggplot2。您必须使用此类或此类的其他方法或使用本机绘图功能。
Maybe you can use faceting to get the 8 plots onto one page, then the second link to put it into multiple documents...
也许你可以使用faceting将8个图表放到一个页面上,然后将第二个链接放到多个文档中......
#1
10
library(plyr)
library(gridExtra)
p = ggplot(tab, aes(x=Date)) +
geom_line(aes(y=Tmin), col="blue", size=0.1)
plots = dlply(tab , "Station", `%+%`, e1 = p)
ml = do.call(marrangeGrob, c(plots, list(nrow=8, ncol=1)))
ggsave("multipage.pdf", ml)
untested.
未经测试。
#2
1
You should simplify your plot since once you get the right order with a simple plot you just replace it with your complicated one. ggplot2
are based on grid
package so you need to use gridExtra
to arrange your plots. Then you loop through , for each 8 plots, you store them in a list and you call grid.arrange
over it, and you repeat this until the end of your plots...
您应该简化您的情节,因为一旦您通过简单的情节得到正确的顺序,您只需将其替换为复杂的情节。 ggplot2基于网格包,因此您需要使用gridExtra来排列图。然后你循环,对于每8个图,你将它们存储在一个列表中,然后在它上面调用grid.arrange,然后你重复这个直到你的图结束......
library(gridExtra)
library(ggplot2)
pdf('test.pdf', width=21, height=27)
i = 1
plot = list()
for (n in unique(tab$Station)){
### process data for plotting here ####
plot[[i]] = ggplot(tab[tab$Station==n], aes(x=Date)) +...
if (i %% 8 == 0) { ## print 8 plots on a page
print (do.call(grid.arrange, plot))
plot = list() # reset plot
i = 0 # reset index
}
i = i + 1
}
if (length(plot) != 0) {
print (do.call(grid.arrange, plot))
}
dev.off()
#3
1
Faceting might be the way to go. Decide how many faceted mini-plots you want per page, then loop through the required number of times, generating a png or a pdf as you go. So if you have 200 data items and you want 50 per page, in facets of 5 across and 10 down, just loop through 200/50 = 4 iterations. Crude, but should work.
分面可能是要走的路。确定每页需要多少个刻面迷你图,然后循环所需的次数,随时生成png或pdf。因此,如果您有200个数据项并且每页需要50个数据项,则在5个横向和10个向下的方面,只需循环200/50 = 4个迭代。原油,但应该工作。
library(ggplot2)
ii <- 7
nn <- 49
mydf <- data.frame(date = rep(seq(as.Date('2013-03-01'),
by = 'day', length.out = ii), nn),
value = rep(runif(nn, 100, 200)))
mydf$facet.variable <- rep(1:nn, each = ii)
p <- ggplot(mydf, aes(x = date, y = value)) +
geom_line() +
facet_wrap(~ facet.variable, ncol = ii)
print(p)
#4
0
Unfortunately, mfrow
doesn't work with ggplot2
. You have to use other methods like this one or this one or use the native plot
function.
不幸的是,mfrow不适用于ggplot2。您必须使用此类或此类的其他方法或使用本机绘图功能。
Maybe you can use faceting to get the 8 plots onto one page, then the second link to put it into multiple documents...
也许你可以使用faceting将8个图表放到一个页面上,然后将第二个链接放到多个文档中......