2

I'm new in designing plots and I've tried constructing barplots with:

bar <- ggplot(month1, aes(x=variable, y=value, fill=merge1)) + geom_bar(stat="identity")
bar_f <- bar  + ggtitle("k = 0") + theme(axis.text=element_text(size=12, color="gray0"), 
axis.title=element_blank()) 
+ scale_y_continuous(breaks= function(x) unique(floor(pretty(seq(0, (max(x) + 1) * 1.1))))) 
+ scale_fill_manual(values= grp_colors, guide=F) 


bar2 <- ggplot(month2, aes(x=variable, y=value, fill=merge2)) + geom_bar    (stat="identity") 
bar2_f <- bar2 + ggtitle(expression(k %in% group("[", "1;12", "]"))) 
+ theme(axis.text=element_text(size=12 , color="gray0"), axis.title=element_blank()) 
+ scale_y_continuous(limits=c(0,3), breaks=seq(3)) + scale_fill_manual(values= grp_colors, guide=F)

combine <- gridExtra::grid.arrange(bar_f, bar2_f, nrow=1, top= textGrob("Monatsdaten: Häufigkeiten", gp=gpar(fontsize=20,font=1)))

My problem is, that I've an mathematical notation in my bar2 plot, produced by

expression(k %in% group("[", "1;12", "]"))

If I would change the title to a "normal" one, the size of the two plots are equal. Obviously the 2nd plot is a bit smaller, caused by the mathematical title. I tried to define outside of the ggplot environment and inside of grid.arrange, but it doesn't work with the expression notation. Is there way to have the same size of the plot objects ?

And another (it's not my main question, but I didn't find an answer) question, can I enlarge the distance between my top title and the plot titles ?

enter image description here

zx8754
  • 52,746
  • 12
  • 114
  • 209
rook1996
  • 237
  • 3
  • 9

2 Answers2

0

Try the cowplot package

# some plots
p <- ggplot(iris, aes(Species, Sepal.Length)) +
          stat_summary(geom="bar", fun.y="mean")

p1 <- p + ggtitle("k=0")    
p2 <- p + ggtitle(expression(k %in% group("[", "1;12", "]"))) 

# the plot. Add horizontal alignment
cowplot::plot_grid(p1, p2, align = "h")

enter image description here

Roman
  • 17,008
  • 3
  • 36
  • 49
0

grid.arrange has a "heights" option where you can change the size of your plots relative to one another. You would add a term like

heights =c(1,1.1) 

to your grid.arrange call to make the plotted area of your second plot slightly larger. See Specify widths and heights of plots with grid.arrange for some examples.

AnnVyrgyl
  • 56
  • 6