How can I edit ggMarginal background to match the plot background?

27 views Asked by At

I'm trying to use ggExtra::ggMarginal() to add the density histograms on the side. It works fine but the background color is white instead of the "#fffdf7" color I want. I can't seem to change the background color of the ggMarginal to match the plot color. I want the entire plot to be the same "#fffdf7" color instead of the white line you see where the ggMarginal meets the ggplot.

Any ideas? enter image description here

library(tidyverse)
library(readxl)
library(summarytools)
library(ggExtra)
library(ggtext)

tr <- ggplot(mtcars) +
  aes(x = mpg, y = disp, color = as.character(cyl), shape = as.character(cyl)) +
  geom_point(size = 3) +
  geom_smooth(method = "lm",
              se = TRUE,
              show.legend = FALSE,
              alpha = 0.1) +
  scale_color_manual(values = c("#325756FF", "#7D9FC2FF", "#C582B2FF", "#51806AFF", "#4D5F8EFF", "#A092B7FF")) +
  labs(
    # Main title label
    # New plot <b style='color:#009E73'>title</b>
    title = "<i> Mtcars</i> Vizualization Project",
    # Subtitle label
    subtitle = "Model for <b style='color:#325756FF'>4</b>, <b style='color:#7D9FC2FF'>6</b>, and <b style='color:#C582B2FF'>8</b> Cylinders",
    # X-axis label
    x = "MPG",
    # Y-axis label
    y = "Dispersion",
    # Caption label
    caption = "Sources: mtcars.",
    # Legend titles
    shape = "Cylinder",
    color = "Cylinder",
    fill = "Cylinder"
  ) +
  theme(
    # Legend location, formatting, and background
    legend.position = "bottom",
    legend.title = element_text(
      family = "",
      face = "bold",
      size = 16,
      color = "grey30"
    ),
    # Legend baclground fill and ourline (color)
    legend.background = element_blank(),
    # 
    legend.box.background = element_blank(),
    # Legend box for shapes
    legend.key = element_blank(),
    # Overall text formatting
    text = element_text(
      family = "",
      face = "bold",
      size = 16,
      color = "grey30"
    ),
    # Plot title formatting
    plot.title = element_markdown(
      lineheight = 1.1,
      family = "",
      size = 20,
      face = "bold",
      hjust = 0.5,
      color = "grey30"
    ),
    # Plot subtitle formatting
    plot.subtitle = element_markdown(
      family = "",
      size = 14,
      hjust = 0.5,
      face = "plain",
      color = "grey30"
    ),
    # Plot caption formatting
    plot.caption = element_text(
      family = "",
      size = 10,
      face = "plain",
      color = "grey30"
    ),
    # Axes text angle and formatting
    axis.text.x = element_text(
      angle = 0, 
      face = "bold"
      ),
    axis.text.y = element_text(
      angle = 0, 
      face = "bold"
      ),
    # Overall (x and y) axes ticks formatting
    axis.ticks = element_blank(),
    # Overall (x and y) axes lines formatting
    axis.line = element_line(
      color = "#b4aea9"
      ),
    # Panel grid formatting
    panel.grid = element_line(
      color = "grey95"
      ),
    # Minor overall (x and y) panel grid formatting
    panel.grid.minor = element_blank(
      
    ),
    # Major overall (x and y) panel grid formatting
    panel.grid.major = element_line(
      linetype = "solid"
      ),
    # Panel background (what is inside axes)
    panel.background = element_rect(
      fill = "#fffdf7", 
      color = "#fffdf7"
      ),
    # Plot background (what is outside axes)
    plot.background = element_rect(
      fill = "#fffdf7", 
      color = "#fffdf7"
      )
  )

tr %>% 
  ggMarginal(
    groupColour = TRUE,
    groupFill = TRUE
    )


1

There are 1 answers

2
Allan Cameron On

There doesn't seem to be an option to do this directly within the function. However, without too much effort you can expand the original plot background in the resulting gTable:

tr2 <- tr %>% 
  ggMarginal(
    groupColour = TRUE,
    groupFill = TRUE
  ) 

tr2$layout$t[1] <- 1
tr2$layout$r[1] <- max(tr2$layout$r)

This results in

tr2

enter image description here