Search code examples
rplotggplot2cowplot

How do I leave the graph centralized by ggplot2


I have following commands by cowplot

require(cowplot);
tiff('./solution/AGM2.tiff', height = 18,width = 25, units = 'in',res = 60)

plot_grid(a28 + theme(axis.title.y = element_text(size =40),
                      axis.title.x=element_text(size =40),
                      axis.text.x=element_text(size =35),
                      axis.text.y=element_text(size =40),
                      title=element_text(size = 30),
                      legend.text=element_text(size = 30)),
          a33 + theme(axis.title.y = element_text(size =40),
                      axis.title.x=element_text(size =40),
                      axis.text.x=element_text(size =35),
                      axis.text.y=element_text(size =40),
                      title=element_text(size = 30),
                      legend.text=element_text(size = 30)),
          a61 + theme(axis.title.y = element_text(size =40),
                      axis.title.x=element_text(size =40),
                      axis.text.x=element_text(size =35),
                      axis.text.y=element_text(size =40),
                      title=element_text(size = 30),
                      legend.text=element_text(size = 30)),
          align = 'h', nrow=2, ncol = 2,hjust=0.5,vjust=0.5)

dev.off()

And got the following graph

enter image description here

But I would like to leave the graph centralized, specifically the third figure (MUFAt) of the graph. Can someone please help me?


Solution

  • Using cowplot you can arrange you graphs not only using a grid, but also specifying where to draw each individual graph using draw_plot():

    library(ggplot2)
    library(cowplot)
    
    # theme is repeated for each plot
    my_theme <- theme(axis.title.y = element_text(size = 40),
                      axis.title.x = element_text(size = 40),
                      axis.text.x = element_text(size = 35),
                      axis.text.y = element_text(size = 40),
                      title = element_text(size = 30),
                      legend.text = element_text(size = 30))
    
    # Dummy plots
    g1 <- ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, color = Species)) +
        geom_point(size = 5, show.legend = F) +
        my_theme
    
    g2 <- ggplot(iris, aes(x = Petal.Width, y = Petal.Length, color = Species)) +
        geom_point(size = 5, show.legend = F) +
        my_theme
    
    g3 <- ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, color = Species)) +
        geom_smooth(show.legend = F) +
        my_theme
    
    
    # tiff(height = 18,width = 25, units = 'in',res = 60)
    png(height = 18,width = 25, units = 'in',res = 60)    
    
    # Use x, y, height and width to customize each plot
    ggdraw() +
        draw_plot(g1, x = 0, y = .5, height = .5, width = .5) +
        draw_plot(g2, x = .5, y = .5, height = .5, width = .5) +
        draw_plot(g3, x = .25, y = 0, height = .5, width = .5)
    
    dev.off()
    

    enter image description here