BAlpine
BAlpine

Reputation: 111

Reduce space between plots ggplot2

I am working with bookdown and the emmeans package. I am trying to render a pdf where I can fit a series of plot with minimal space between each other

```{r 3AeMixEffCmass,out.width = c("32%","32%","32%"), fig.align = "center", fig.pos="!h",fig.show = 'hold',fig.cap='(ref:3AeMixEffCmass)', message=FALSE, warning=FALSE, echo=FALSE}

### plot letter identifier:
library(grid)
my_grob = grobTree(textGrob("A1", x=0.85,  y=0.95, hjust=0,  gp=gpar(col="black", fontsize=15, fontface="italic")))


library(ggplot2)
### Order the levels for printing:
cld.mixed_C_L$managem = factor(cld.mixed_C_L$managem, levels=c("WTH", "CH", "CHF")) 
### reset row number: 
cld.mixed_C_L<- with(cld.mixed_C_L, cld.mixed_C_L[order(managem),])
rownames(cld.mixed_C_L) <- NULL 

cld.mixed_C_L looks like this:

cld.mixed_C_L <- structure(list(managem = structure(c(1L, 3L, 2L), .Label = c("CH", 
+ "CHF", "WTH"), class = "factor"), response = c(2.46142878854869, 
+ 2.47526369032351, 3.25797765207699), SE = c(0.111893590886356, 
+ 0.112522508873481, 0.148103743733873), df = c(5.22163854368496, 
+ 5.22163854368496, 5.22163854368496), lower.CL = c(2.10382917999276, 
+ 2.11565412905959, 2.78465437801484), upper.CL = c(2.87981160196529, 
+ 2.89599809934785, 3.81175432945472), .group = c("a", "a", "b"
+ )), row.names = c(1L, 3L, 2L), class = "data.frame")

create plot:

plot.mixed.lme<-ggplot(cld.mixed_C_L,aes(x = managem,y=response,color= managem, label=.group))+ 
        theme_bw()+
        geom_point(shape  = 15, size   = 4) +
        geom_errorbar(aes(ymin  =  lower.CL,ymax  =  upper.CL),width =  0.2,size  =  0.7)+ 
        theme(aspect.ratio = 2/1) + 
#### following lines adds legend inside the boxplot:
        theme(legend.justification=c(1,0), legend.position=c(1,0),
        legend.direction="vertical",
        legend.box="vertical",
        legend.box.just = c("top"), 
        legend.background = element_rect(fill=alpha('white', 0.4)))+
        #removes legend:
        guides(fill = FALSE, color= FALSE) +
#### remove axis labels:
        theme(plot.title = element_blank(), axis.title.x = element_blank(), axis.title.y = element_blank()) +
#### move significance letters:
        geom_text(nudge_x = c(0.3, 0.3, 0.3),
                  nudge_y = c(0, 0,0),
                  color   = "black") +
#### set colours of the boxes:
        scale_color_manual(values = c("#333333", "#333333", "#333333"))+
        annotation_custom(my_grob)
### print three plots as example: 


plot.mixed.lme
plot.mixed.lme
plot.mixed.lme

The result on the final pdf document looks as follows: enter image description here

I have been trying to figure out how to reduce the space between plots, but so far I've had no luck. Any help is appreciated.

Many thanks in advance.

Issue with sizes of plots not matching

(see comments) enter image description here

Upvotes: 0

Views: 959

Answers (1)

Dan
Dan

Reputation: 12074

Remove out.width = c("32%","32%","32%") from the chunk options and use cowplot's plot_grid function, like this:

cowplot::plot_grid(plot.mixed.lme, plot.mixed.lme, plot.mixed.lme, nrow = 1)

enter image description here

Upvotes: 1

Related Questions