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!
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()
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.
Unfortunately,
mfrow
doesn't work withggplot2
. You have to use other methods like this one or this one or use the nativeplot
function.Maybe you can use faceting to get the 8 plots onto one page, then the second link to put it into multiple documents...
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 ongrid
package so you need to usegridExtra
to arrange your plots. Then you loop through , for each 8 plots, you store them in a list and you callgrid.arrange
over it, and you repeat this until the end of your plots...untested.