Home > Blockchain >  Extend canvas of ggplot with same background color as plot
Extend canvas of ggplot with same background color as plot

Time:10-05

I wish to make room at the top of a plot with blank space the same color as the panel.background of my plot.

In the example, panel.background -> dark_grey. When I use plot.margin to make some space, the background is white. How is it possible for this background to be dark_grey with no grid lines?

It would look something like this: enter image description here

library("tidyverse")

dark_grey <- "#525250"

df <- tibble(
  x = c(1, 2, 3, 4, 5),
  y = c(2, 3, 5, 8, 20)
)

ggplot(data = df, aes(x = x, y = y, group = 1))  
  geom_line(colour = "red")   
  theme(
    panel.background = element_rect(fill = dark_grey), 
    plot.margin = unit(c(4,0,0,0), "cm")
  )

CodePudding user response:

Have you tried setting plot.background rather than or as well as panel.packground?

CodePudding user response:

OK So here are a couple of fudged solutions as what you want can not be explicitly done.

library("tidyverse")

dark_grey <- "#525250"

df <- tibble(
    x = c(1, 2, 3, 4, 5),
    y = c(2, 3, 5, 8, 20)
)

ggplot(data = df, aes(x = x, y = y, group = 1))  
    geom_line(aes(colour = "red"))   
    theme(
        panel.background = element_rect(fill = dark_grey), 
       # plot.margin = unit(c(4,0,0,0), "cm")
    )  
    theme(legend.position="top", 
          legend.background = element_rect(fill = dark_grey),
          legend.key.size = unit(0, "cm"),
          legend.margin = margin(1,5.3,1,5.3,unit = "cm"), 
             # likely to need adjusted to create the box
          legend.spacing = unit(0,"cm"),
          legend.box.margin = margin(0,0,-0.5,0,unit = "cm"), 
              # may need adjusted to move box up / down
          legend.text = element_text(colour = NA),
          legend.title = element_text(colour = NA),
          legend.key = element_rect(fill = dark_grey),
          legend.key.height = unit(0, "cm"),
          legend.key.width = unit(0,"cm")
          ) 
    guides(colour = guide_legend(override.aes = list(colour = dark_grey))) 
           # colours the tiny red dot grey to make it vanish

enter image description here This solution is adding a legend but then making its content either not be displayed at all, or be displayed but the same colour as the background to essentially be invisible. The downside - this box size is set manually - so as the export size changes the legend box size would need reset

The alternative, is to use ggarrange from ggpubr (alternatives might be cowplot and grid.arrange) to join two plots together. Plot gg is essentially your original plot without any additional margin added. Plot lg is your legend area. This is essentially the same graph with no geom and lines removed. Margins tweaked to ensure they have no white space above and below. Then merged

require(ggpubr)
gg <-ggplot(data = df, aes(x = x, y = y, group = 1))  
    geom_line(aes(colour = "red"))   
    theme(
        panel.background = element_rect(fill = dark_grey), 
        # plot.margin = unit(c(4,0,0,0), "cm")
    )  
    theme(legend.position = "none",
          plot.margin = margin(t=-5,5.5,5.5,5.5, "pt"))

lg <-ggplot(data = df, aes(x = x, y = y, group = 1))  
   # theme_void()  
    theme(
        panel.background = element_rect(fill = dark_grey),
        panel.grid = element_line(colour = NA),
        axis.title = element_text(colour = NA),
        axis.text = element_text(colour = NA),
        axis.ticks = element_line(colour = NA),
        plot.margin = margin(5.5,5.5,b=-30,5.5, "pt"))

ggarrange(lg,gg, ncol=1)

enter image description here

At the moment this is too big on top but you can adjust the height of lg in ggarrange.

Not sure how your custom legend it to be added...

  • Related