Dear google group
I’m new to
R and struggle with the code of a multi-plot panel. I need to arrange 45 bar
charts on one page with annotations at top, bottom and left hand side. I used
ggplot2 to create the plots and grid to place the plots on the single page. I’m
happy with the location of the plots and the annotations. I’m really unhappy
with the margins between the plots. The margins between the plots are too big
and the plots as such a far too small. I guess the source of the problem is in
the grid.layout function. Changing the
ggplot2 margins (via theme) did not show any changes.
I would be
very happy when someone could help me how I could increase plot size and
decrease margins between plots grid.layout cells respectively. Below, you’ll
find a simplified version of the code I’ve used.
Many thanks for your help!
Ulrika
library(ggplot2)
library(grid)
library(gridExtra)
#ggplots
# Foliage of trees
foliage.factors<-c(1,2,3,4,5)
frequency<-c(0.0333, 0.0000, 0.1667, 0.7667, 0.0333)
foliage<-data.frame(foliage=foliage.factors, frequency=frequency)
foliage
p1<-
ggplot(foliage, aes(x=foliage.factors, y=frequency))+
geom_histogram(stat="identity", color="Green", fill="Green", width=.2, binwith=.002)+
scale_x_discrete(expand=c(0,0), limits=c(1:5), labels=c("1-2", "3-4", "5-6", "7-8", "9-10"))+
scale_y_continuous(expand=c(0,0), limits=c(0,1), labels=NULL)+
theme(axis.text.x=element_text(size = 0.8 , lineheight = 0.9, colour = "black", vjust = 1))+
theme(axis.text.y=element_text(size = 0.8, lineheight = 0.9, colour = NULL, hjust = 1))+
theme(legend.position="none")+
theme_bw()+
theme(panel.grid.minor=element_blank(), panel.grid.major=element_blank())+
theme(panel.border=element_blank())+
theme(axis.line=element_line(colour="black", size=.05),
axis.line.y=element_blank())+
theme(axis.text.x=element_text())+
theme(axis.ticks.margin=unit(c(0.03,0.03),"cm"))+
theme(axis.ticks.y=element_blank())+
theme(strip.background=element_rect(fill=NA, colour=NA, size=.5, linetype=2))+
theme(strip.text.x=element_text(colour="black", size=4))+
theme(axis.title.x=element_blank())+
theme(axis.title.y=element_blank())
p1
#Feeding damage of trees
fed.dam.factors<-c(0,1,2,3,4,5)
fed.frequency<-c(0.0000, 0.1667, 0.5000, 0.2333, 0.0667, 0.0333)
feedingdamage<-data.frame(feeding.damage=fed.dam.factors, frequency=fed.frequency)
feedingdamage
p2<-
ggplot(feedingdamage, aes(x=fed.dam.factors, y=fed.frequency))+
geom_histogram(stat="identity", color="Red", fill="Red", width=.3, binwith=.002)+
scale_x_discrete(expand=c(0,0), limits=c(0:5), labels=c("0", "1", "2", "3", "4", "5"))+
scale_y_continuous(expand=c(0,0), limits=c(0,1), labels=NULL)+
theme(axis.text.x=element_text(size = 0.8 , lineheight = 0.9, colour = "black", vjust = 1))+
theme(axis.text.y=element_text(size = 0.8, lineheight = 0.9, colour = NULL, hjust = 1))+
theme(legend.position="none")+
theme_bw()+
theme(panel.grid.minor=element_blank(), panel.grid.major=element_blank())+
theme(panel.border=element_blank())+
theme(axis.line=element_line(colour="black", size=.05),
axis.line.y=element_blank())+
theme(axis.text.x=element_text())+
theme(axis.ticks.margin=unit(c(0.03,0.03),"cm"))+
theme(axis.ticks.y=element_blank())+
theme(strip.background=element_rect(fill=NA, colour=NA, size=.5, linetype=2))+
theme(strip.text.x=element_text(colour="black", size=4))+
theme(axis.title.x=element_blank())+
theme(axis.title.y=element_blank())
p2
#grid
grid.newpage()
pushViewport(viewport(layout=grid.layout(11, 7, heights = unit(c(1,4,4,4,4,4,4,4,4,4,1), "null"),
widths=unit(c(0.5,0.5,1,1,1,1,1),"null"))))
vplay<-function(x,y)viewport(layout.pos.row=x, layout.pos.col=y)
grid.text("MAIN TITLE", vp = viewport(layout.pos.row = 1, layout.pos.col = 1:7), gp=gpar(fontsize=14, fontface="bold"))
print(p1,vp=vplay(2,3)) ###print=Befehl um Plots darzustellen(output),
print(p2,vp=vplay(2,4)) ###vp=specifies the viewport in which to draw the relevant output(here:plot)
print(p1,vp=vplay(2,5)) ###vplay()=Position of output(here: plot) in vplay
print(p2,vp=vplay(2,6))
print(p1,vp=vplay(2,7))
print(p1,vp=vplay(3,3))
print(p2,vp=vplay(3,4))
print(p1,vp=vplay(3,5))
print(p2,vp=vplay(3,6))
print(p1,vp=vplay(3,7))
print(p1,vp=vplay(4,3))
print(p2,vp=vplay(4,4))
print(p1,vp=vplay(4,5))
print(p2,vp=vplay(4,6))
print(p1,vp=vplay(4,7))
print(p1,vp=vplay(5,3))
print(p2,vp=vplay(5,4))
print(p1,vp=vplay(5,5))
print(p2,vp=vplay(5,6))
print(p1,vp=vplay(5,7))
print(p1,vp=vplay(6,3))
print(p2,vp=vplay(6,4))
print(p1,vp=vplay(6,5))
print(p2,vp=vplay(6,6))
print(p1,vp=vplay(6,7))
print(p1,vp=vplay(7,3))
print(p2,vp=vplay(7,4))
print(p1,vp=vplay(7,5))
print(p2,vp=vplay(7,6))
print(p1,vp=vplay(7,7))
print(p1,vp=vplay(8,3))
print(p2,vp=vplay(8,4))
print(p1,vp=vplay(8,5))
print(p2,vp=vplay(8,6))
print(p1,vp=vplay(8,7))
print(p1,vp=vplay(9,3))
print(p2,vp=vplay(9,4))
print(p1,vp=vplay(9,5))
print(p2,vp=vplay(9,6))
print(p1,vp=vplay(9,7))
print(p1,vp=vplay(10,3))
print(p2,vp=vplay(10,4))
print(p1,vp=vplay(10,5))
print(p2,vp=vplay(10,6))
print(p1,vp=vplay(10,7))
###Annotations of the Multi-plot panels
#Ordinate
grid.text("2009", vp=vplay(2,1), gp=gpar(fontsize=12, fontface="bold"))
grid.text("2010", vp=vplay(5,1), gp=gpar(fontsize=12,fontface= "bold"))
grid.text("2011", vp=vplay(8,1), gp=gpar(fontsize=12,fontface= "bold"))
grid.text("2012", vp=vplay(10,1), gp=gpar(fontsize=12, fontface="bold"))
grid.text("Date 1",vp=vplay(2,2), gp=gpar(fontsize=12))
grid.text("Date 2", vp=vplay(3,2), gp=gpar(fontsize=12))
grid.text("Date 3", vp=vplay(4,2), gp=gpar(fontsize=12))
grid.text("Date 1",vp=vplay(5,2), gp=gpar(fontsize=12))
grid.text("Date 2", vp=vplay(6,2), gp=gpar(fontsize=12))
grid.text("Date 3", vp=vplay(7,2), gp=gpar(fontsize=12))
grid.text("Date 1",vp=vplay(8,2), gp=gpar(fontsize=12))
grid.text("Date 3", vp=vplay(9,2), gp=gpar(fontsize=12))
grid.text("Date 1", vp=vplay(10,2), gp=gpar(fontsize=12))
#Abszisse
grid.text("Foliage", vp=vplay(11,3), gp=gpar(fontsize=12, fontface="bold"))
grid.text("Feeding Damage", vp=vplay(11,4), gp=gpar(fontsize=12, fontface="bold"))
grid.text("Foliage", vp=vplay(11,5), gp=gpar(fontsize=12, fontface="bold"))
grid.text("Feeding Damage", vp=vplay(11,6), gp=gpar(fontsize=12, fontface="bold"))
grid.text("Foliage", vp=vplay(11,7), gp=gpar(fontsize=12, fontface="bold"))