Cobin
Cobin

Reputation: 930

How to adjust plot areas in ggplot?

I am trying to use grid.arrange to display multiple graphs on the same page generated by ggplot.Each subplot with difference x and y scale. Two subplot share the legend. My perpose is that to display the plot areas same size. Are there parameter to adjust plot area ( except legend area)? The facet is inadequate to arrange it.

df <- data.frame(class=paste0('a',1:20),
         x1=runif(20),
         x2=runif(20),
         y1=runif(20),
         y2=runif(20))

p1 <- ggplot(df,aes(x=x1,y=y1))+
  geom_point(aes(color=class),size=2,show.legend=TRUE)+
  stat_smooth(method='lm',color='black')+
  theme_bw()

p2 <- ggplot(df,aes(x=x2,y=y2))+
  geom_point(aes(color=class),size=2,show.legend=FALSE)+
  stat_smooth(method='lm',color='black')+
  theme_bw()
grid.arrange(p1,p2,nrow=2)

enter image description here

Upvotes: 0

Views: 88

Answers (1)

Tung
Tung

Reputation: 28391

Using patchwork package

# install.packages("devtools", dependencies = TRUE)
# devtools::install_github("thomasp85/patchwork")
library(patchwork)
p1 / p2 +   plot_annotation(title = "Plot title",
                            subtitle = "Plot subtitle",
                            tag_levels = 'A',
                            tag_suffix = ')')

Created on 2018-11-20 by the reprex package (v0.2.1.9000)

Upvotes: 1

Related Questions