如何编辑 ggMarginal 背景以匹配绘图背景?

问题描述 投票:0回答:1

我正在尝试使用 ggExtra::ggMarginal() 在侧面添加密度直方图。它工作正常,但背景颜色是白色,而不是我想要的“#fffdf7”颜色。我似乎无法更改 ggMarginal 的背景颜色以匹配绘图颜色。我希望整个图具有相同的“#fffdf7”颜色,而不是您在 ggMarginal 与 ggplot 交汇处看到的白线。

有什么想法吗?

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
    )


r ggplot2 colors background
1个回答
0
投票

似乎没有可以直接在函数内执行此操作的选项。但是,无需太多努力,您就可以在生成的 gTable 中扩展原始绘图背景:

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

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

这会导致

tr2

© www.soinside.com 2019 - 2024. All rights reserved.