4

I am trying to create a plot that will be combination of 6 plots made in ggplot2. The conditions are

  1. One main title
  2. Three subtitles
  3. Common background color
  4. Diffrent sizes of plots
  5. One legend at the bottom

And it should look something like it: Desired plot

I have found bits and pices, but I dont know how to put it togheter.

To add main title I used Place title of multiplot panel with ggplot2

layout <- matrix(c(1, 1, 2, 3, 3, 4), nrow = 2, byrow = TRUE)
grid.arrange(A, B, C, D, top = "Title",
             layout_matrix = layout)

Plot with grid.arrange

I have found function multiplot (http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/) and it dose allow to plot several charts with diffrent sizes, but the other reqirements are not met

plot_list <- list(A, B, C, D)
layout <- matrix(c(1, 1, 2, 3, 3, 4), nrow = 2, byrow = TRUE)
multiplot(plotlist = plot_list, layout = layout) 

Plot with multiplot

I have also found how to create common legend, but the charts sizes are the same (Add a common Legend for combined ggplots)

grid_arrange_shared_legend <- function(...) {
  plots <- list(...)
  g <- ggplotGrob(plots[[1]] + theme(legend.position="bottom"))$grobs
  legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
  lheight <- sum(legend$height)
  grid.arrange(
    do.call(arrangeGrob, lapply(plots, function(x)
      x + theme(legend.position="none"))),
    legend,
    ncol = 1,
    heights = unit.c(unit(1, "npc") - lheight, lheight))
}

grid_arrange_shared_legend(A, B, C, D)

Plot with grid_arrange_shared_legend

sample data

DF <- data.frame(ID = 1:10, Pop = (1:10)^2, gr = c("A", rep("B", 8), "A"))
DF_Pie <- DF %>%
  group_by(gr) %>%
  summarise(Years = n(),
            Pop_Years = sum(Pop))

A <-  ggplot(DF, aes(x = ID, col = gr, fill = gr)) +
  geom_bar()+ 
  theme(legend.position="none")

B <- ggplot(DF_Pie, aes(x = factor(1), y = Years, fill = gr))+
  geom_bar(width = 1, stat = "identity") +
  coord_polar("y", start=0)+ 
  theme(legend.position="none")

C <- ggplot(DF, aes(x = ID, y = Pop, col = gr, fill = gr)) +
  geom_bar(stat="identity") +
  theme(legend.position="bottom")

D <- ggplot(DF_Pie, aes(x = factor(1), y = Pop_Years, fill = gr))+
  geom_bar(width = 1, stat = "identity") +
  coord_polar("y", start=0) + 
  theme(legend.position="none")

EDIT

Thanks to @hpesoj626 I know how to make everything, but the condition 3 still applys - the common background color Chart after hpesoj626  changes

EDIT 2

I have created following chart end result but the space between plots and titles are huge

Cœur
  • 37,241
  • 25
  • 195
  • 267
AAAA
  • 461
  • 6
  • 22

1 Answers1

3

From the linked post, there is a ggpubr solution. I looked at the package and saw ggpubr::anotate_figure, which seems able to do what you want. I did some tweaks with the plots A, B, C, D.

A <-  ggplot(DF, aes(x = ID, col = gr, fill = gr)) +
  geom_bar() + 
  xlab(NULL) + theme(legend.position = "none")

B <- ggplot(DF_Pie, aes(x = factor(1), y = Years, fill = gr))+
  geom_bar(width = 1, stat = "identity") +
  coord_polar("y", start=0) +  labs(x = NULL, y = NULL) + theme(legend.position = "none")

C <- ggplot(DF, aes(x = ID, y = Pop, col = gr, fill = gr)) +
  geom_bar(stat="identity")  

D <- ggplot(DF_Pie, aes(x = factor(1), y = Pop_Years, fill = gr))+
  geom_bar(width = 1, stat = "identity") +
  coord_polar("y", start=0) +  labs(x = NULL, y = NULL)


p1 <- ggarrange(A, B, ncol=2) 
p2 <- ggarrange(C, D, ncol=2, common.legend = TRUE, legend = "bottom") 

p1 <- annotate_figure(p1, top = text_grob("According to years"))
p2 <- annotate_figure(p2, top = text_grob("According to population"))

p <- ggarrange(p1, p2, nrow=2, common.legend = TRUE, legend="bottom", heights = c(3,3.75)) 
annotate_figure(p, top = text_grob("Main title", face = "bold", size = 16))

enter image description here

hpesoj626
  • 3,529
  • 1
  • 17
  • 25
  • Thanks! it almost answers all my questions. is there a way to change everywhere background color to gray and to change that first column will be twice as wide? – AAAA Jun 10 '18 at 13:38
  • Ok, I have delt with the widths, but the problem with background colour stays - please see edit in question – AAAA Jun 10 '18 at 13:47
  • ok, solved that also 'p + bgcolor("#f5f5f2")+ border("#f5f5f2")' – AAAA Jun 10 '18 at 14:16
  • Sorry, i have just read your comments. You did a good job, it seems. Glad i could help. – hpesoj626 Jun 10 '18 at 15:00
  • Not sure if this will help, but if you want a white background for your graphs, you should adjust the theme. I would try `theme_set(theme_void())` or any other here: http://ggplot2.tidyverse.org/reference/ggtheme.html – A Duv Jun 11 '18 at 06:25
  • is there a way to get the titles closer to the plots? please see the edit – AAAA Jul 17 '18 at 09:49