0

I am generating some plots with the following code.I have 8 plots generated with the the following code and what I want is to have them on the same page with no titles. More specifically, I want in every plot to have on the left up-corner a letter (a,b..) and at the end of the plot to have something like an one-row legend (e.g Plots: a. category one, b. category two, ...).

Code:

g1= ggplot(som, aes(x=value, y=variable))+geom_smooth(method=lm,alpha=0.25,col='green',lwd=0.1) +ylim(0,1000)+xlim(-2,2)+
         geom_point(shape=23,fill="black",size=0.2)+theme_bw()+theme(plot.background = element_blank(),panel.grid.major = element_blank()
        ,panel.grid.minor = element_blank()) +labs(x="something here",y="something else")+
        theme(axis.title.x = element_text(face="bold", size=7),axis.text.x = element_text(size=5))+
        theme(axis.title.y = element_text(face="bold", size=7),axis.text.y = element_text(size=5))+
        theme(plot.title = element_text(lineheight=.8, face="bold",size=8))
grid.arrange(g1,g2,g3,g4,g5,g6,g7,g8,ncol=2)

Is it possible to do that with ggplot? If so, how can I do this?

p.s I have no problem with the above code

Thank you.

3
  • 1
    you want to look at a ggplot2 arranging package like library(cowplot) Commented Jan 3, 2018 at 15:40
  • 1
    Depending on how similar these are you could try using facet. I'm not as familiar with cowplot but the grid.arrange() is a very solid method. Commented Jan 3, 2018 at 16:32
  • Thank you both for your comments. @Nate I will check this. Mike the problem is not the placement of the plots but the use of indices within them and the creation of one general, one-row legend at the bottom of the page. Commented Jan 3, 2018 at 16:34

1 Answer 1

1

This is how you could do it with library(cowplot).

First some plots:

set.seed(1)
plots <- list()

for (i in 1:8) {
    my_cars <- mtcars[sample(1:nrow(mtcars), 10), ]

    plots[[i]] <- ggplot(my_cars, aes(mpg, hp, color = as.factor(cyl))) +
        geom_point() +
        geom_smooth(method = "lm", color = "black")
}

Then to have a unifying title (or legend here) we use a combination of two plot_grid() calls.

lbls <- LETTERS[1:length(plots)]
# add a line break because its long
lbls <- gsub("E", "\nE", lbls)

grid <- plot_grid(plotlist = plots, labels = lbls, ncol = 2)

legend <- ggdraw() +
    draw_label(paste0(lbls, "= category",1:length(plots), collapse = "  "))

plot_grid(grid, legend, rel_heights = c(1, .1), ncol = 1)

enter image description here

The documentation for cowplot is great and has a ton of examples. Check it out here and here. Let me know if you get stuck.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for your answer. cowplot seems really good!thanks for the examples.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.