我正在try 使用ggExtra::ggMarginal()添加侧面的密度直方图.它工作正常,但背景 colored颜色 是白色,而不是我想要的"#fffdf7" colored颜色 .我似乎无法更改ggMarginal的背景 colored颜色 以匹配图的 colored颜色 .我希望整个图是相同的"#fffdf7" colored颜色 ,而不是ggMarginal与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
    )


推荐答案

似乎没有在函数中直接执行此操作的选项.然而,不需要太多的努力,您就可以在生成的gTable中展开原始的绘图背景:

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

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

这将导致

tr2

enter image description here

R相关问答推荐

使用对管道内单元格的引用生成新变量

找出疾病消失的受试者

带有叠加饼图系列的Highmap

在R中创建一个包含转换和转换之间的时间的列

矩阵%*%矩阵中的错误:需要数字/复杂矩阵/向量参数

在发布到PowerBI Service时,是否可以使用R脚本作为PowerBI的数据源?

在R中为马赛克图中的每个字段着色

如何使用ggplot对堆叠条形图进行嵌套排序?

在rpart. plot或fancyRpartPlot中使用带有下标的希腊字母作为标签?

R中边际效应包中Logistic回归的交互作用风险比

函数可以跨多个列搜索多个字符串并创建二进制输出变量

有没有可能用shiny 的书签恢复手风琴面板?

使用gt_summary是否有一种方法来限制每个变量集进行配对比较?

按组和连续id计算日期差

我正在try 创建一个接近cos(X)的值的While循环,以便它在-或+1-E10范围内

具有自定义仓位限制和计数的GGPLATE直方图

在shiny 表格中输入的文本在第一次后未更新

将边列表转换为路径长度列表

从多行中 Select 最小值

如何在GGPlot中控制多个图例和线型