Home > Software design >  ggarrange - change background of shared legend and remove borders between plots
ggarrange - change background of shared legend and remove borders between plots

Time:11-10

I would like the background to all be navy blue without the white borders around the plots and the legend background to also be navy blue (no large white box). I have seen some solutions here but they seem needlessly complicated and would involve a lot of rewriting of the script or even switching packages. Surely there is an easier way? Thanks in advance.

Example script:

df <- data.frame(supp=rep(c("VC", "OJ"), each=3),
                  dose=rep(c("D0.5", "D1", "D2"),2),
                  len=c(6.8, 15, 33, 4.2, 10, 29.5))

cols <- c("VC" = "#0FC9F7", "OJ" = "#1010EB")


p1 <- ggplot(df, aes(x=dose, y=len, color=factor(supp)))  
  geom_line(aes(group = supp))  
  theme(
    plot.title = element_text(hjust = 0.5),
    panel.background = element_rect(fill = "#140F4B",
                                    colour = "#140F4B",
                                    size = 0.5, linetype = "solid"),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    plot.background = element_rect(fill = "#140F4B"),
    text = element_text(colour = "white"),
    axis.line = element_line(colour = "white"),
    axis.text = element_text(colour = "white")
  )  
  scale_color_manual(values = cols,
                     name = "supp",
                     breaks=c("VC", "OJ"),
                     labels=c("VC", "OJ"))

p2 <- ggplot(df, aes(x=dose, y=len, color=factor(supp)))  
  geom_line(aes(group = supp))  
  theme(
    plot.title = element_text(hjust = 0.5),
    panel.background = element_rect(fill = "#140F4B",
                                    colour = "#140F4B",
                                    size = 0.5, linetype = "solid"),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    plot.background = element_rect(fill = "#140F4B"),
    text = element_text(colour = "white"),
    axis.line = element_line(colour = "white"),
    axis.text = element_text(colour = "white")
  )  
  scale_color_manual(values = cols,
                     name = "supp",
                     breaks=c("VC", "OJ"),
                     labels=c("VC", "OJ"))

plots <- list(p1, p2)

combined_plots <- ggpubr::ggarrange(plotlist = plots,
                                    ncol = 2, nrow = 1, align = "hv", common.legend = TRUE, legend = "bottom")

#Annotate plot
combined_plots <- annotate_figure(
  combined_plots,
  top = text_grob(paste0("Example"), size = 18, color = "white")
)   bgcolor("#140F4B")  border("#140F4B")

enter image description here

CodePudding user response:

It looks like the problem is that there is a missing legend.background in your theme which might explain why there is a white background around your legend. I managed to solve the problem using patchwork! Hopefully, this helps.

library(ggplot2)
library(patchwork)

df <- data.frame(supp=rep(c("VC", "OJ"), each=3),
                  dose=rep(c("D0.5", "D1", "D2"),2),
                  len=c(6.8, 15, 33, 4.2, 10, 29.5))

cols <- c("VC" = "#0FC9F7", "OJ" = "#1010EB")


p1 <- ggplot(df, aes(x=dose, y=len, color=factor(supp)))  
  geom_line(aes(group = supp))  
  theme(
    plot.title = element_text(hjust = 0.5),
    panel.background = element_rect(fill = "#140F4B",
                                    colour = "#140F4B",
                                    size = 0.5, linetype = "solid"),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    plot.background = element_rect(fill = "#140F4B"),
    text = element_text(colour = "white"),
    axis.line = element_line(colour = "white"),
    axis.text = element_text(colour = "white")
  )  
  scale_color_manual(values = cols,
                     name = "supp",
                     breaks=c("VC", "OJ"),
                     labels=c("VC", "OJ"))

p2 <- ggplot(df, aes(x=dose, y=len, color=factor(supp)))  
  geom_line(aes(group = supp))  
  theme(
    plot.title = element_text(hjust = 0.5),
    panel.background = element_rect(fill = "#140F4B",
                                    colour = "#140F4B",
                                    size = 0.5, linetype = "solid"),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    plot.background = element_rect(fill = "#140F4B"),
    text = element_text(colour = "white"),
    axis.line = element_line(colour = "white"),
    axis.text = element_text(colour = "white")
  )  
  scale_color_manual(values = cols,
                     name = "supp",
                     breaks=c("VC", "OJ"),
                     labels=c("VC", "OJ"))



combined <- p1   p2   plot_annotation(title = "Title Here")  
  plot_layout(guides = "collect") & 
  theme(plot.title = element_text(colour  = "white", size = 18), legend.position = "bottom",
        legend.background = element_rect(fill = "#140F4B", colour = "#140F4B" ),
        legend.key = element_rect(fill = "#140F4B", colour = "#140F4B" ),
        plot.background = element_rect(fill = "#140F4B", colour = "#140F4B"))

 
 combined
 
  • Related