我有这样的代码:

library(tidyverse)

set.seed(123)
df <- data.frame(
  var_a = rnorm(800),
  var_b = rnorm(800),
  criteria = rep(c("a", "b", "c", "d", "e", "f", "g", "h"), each = 100)
)



ggplot(df, aes(var_a, var_b)) +
  stat_density_2d(aes(fill = ..level..), geom = "polygon")+
  scale_fill_distiller(palette=1, direction=-1)+
  geom_smooth(aes(color = criteria), method = "lm", se = FALSE) +
  geom_abline(slope=1, intercept=0, color = "gray35", lwd = 0.3)+
  ylab("x label")+
  xlab("y_label")+
  xlim(-4, 4)+
  ylim(-4, 4)+
  guides(fill = FALSE)+
  theme(legend.key = element_rect(fill = "white"))

问题是the legend.key = element_rect(fill = "white")不起作用.无论是否使用此命令,图例中线条的背景都为黑色.

有人认为这与R版本或以前的定义有关,但我将上面的代码粘贴在Posit Cloud上,假设所有内容都是最新的,同样的事情.

我有两个问题:

  1. 这种情况可以改变吗?
  2. 建议这样做吗?

推荐答案

一种可能的 Select 是不添加填充 colored颜色 (使用show.legend = FALSE),例如

library(tidyverse)

set.seed(123)
df <- data.frame(
  var_a = rnorm(800),
  var_b = rnorm(800),
  criteria = rep(c("a", "b", "c", "d", "e", "f", "g", "h"), each = 100)
)



ggplot(df, aes(var_a, var_b)) +
  stat_density_2d(aes(fill = after_stat(level)), geom = "polygon",
                  show.legend = FALSE)+
  scale_fill_distiller(palette=1, direction=-1)+
  geom_smooth(aes(color = criteria), method = "lm", se = FALSE) +
  geom_abline(slope=1, intercept=0, color = "gray35", lwd = 0.3)+
  ylab("x label")+
  xlab("y_label")+
  xlim(-4, 4)+
  ylim(-4, 4)
#> `geom_smooth()` using formula = 'y ~ x'

创建于2024-02-05年第reprex v2.1.0


另一种 Select 是自己指定key_glyph(按https://ggplot2.tidyverse.org/reference/draw_key.html),例如

library(tidyverse)

set.seed(123)
df <- data.frame(
  var_a = rnorm(800),
  var_b = rnorm(800),
  criteria = rep(c("a", "b", "c", "d", "e", "f", "g", "h"), each = 100)
)

ggplot(df, aes(var_a, var_b)) +
  stat_density_2d(aes(fill = after_stat(level)), geom = "polygon",
                  key_glyph = "path")+
  scale_fill_distiller(palette=1, direction=-1)+
  geom_smooth(aes(color = criteria), method = "lm", se = FALSE) +
  geom_abline(slope=1, intercept=0, color = "gray35", lwd = 0.3)+
  ylab("x label")+
  xlab("y_label")+
  xlim(-4, 4)+
  ylim(-4, 4)
#> `geom_smooth()` using formula = 'y ~ x'

创建于2024-02-05年第reprex v2.1.0


或者如果你想要一些不同的东西,你可以制作你自己的字形,例如

library(tidyverse)

set.seed(123)
df <- data.frame(
  var_a = rnorm(800),
  var_b = rnorm(800),
  criteria = rep(c("a", "b", "c", "d", "e", "f", "g", "h"), each = 100)
)

draw_key_stat_2d <- function(data, params, size) {
  if (is.null(data$linewidth)) {
    data$linewidth <- 0.5
  }
  
  lwd <- min(data$linewidth, min(size) / 4)
  
  rectGrob(
    width = unit(1, "npc") - unit(lwd, "mm"),
    height = unit(1, "npc") - unit(lwd, "mm"),
    gp = gpar(
      col = "grey20" %||% NA,
      fill = radialGradient(colours = c("#00000000", "deepskyblue"),
                            r1 = 0.2, r2 = 0.8) %||% "grey20", 
      lty = data$linetype %||% 1,
      lwd = lwd * .pt,
      linejoin = params$linejoin %||% "mitre",
      lineend = params$lineend %||% "butt"
    ))
}

library(grid)
library(gridExtra)
#> 
#> Attaching package: 'gridExtra'
#> The following object is masked from 'package:dplyr':
#> 
#>     combine
ggplot(df, aes(var_a, var_b)) +
  stat_density_2d(aes(fill = after_stat(level)), geom = "polygon",
                  key_glyph = draw_key_stat_2d)+
  scale_fill_distiller(palette=1, direction=-1)+
  geom_smooth(aes(color = criteria), method = "lm", se = FALSE) +
  geom_abline(slope=1, intercept=0, color = "gray35", lwd = 0.3)+
  ylab("x label")+
  xlab("y_label")+
  xlim(-4, 4)+
  ylim(-4, 4)
#> `geom_smooth()` using formula = 'y ~ x'

创建于2024-02-05年第reprex v2.1.0

R相关问答推荐

R创建一个数据透视表,计算多个组的百分比

为什么当我try 在收件箱中使用合并功能时会出现回收错误?

R Tidymodels textercipes-使用spacyR进行标记化-如何从生成的标记列表中删除标点符号

使用R中相同值创建分组观测指标

任意列的欧几里得距离

将饼图插入条形图

如何删除最后一个可操作对象

绘制采样开始和采样结束之间的事件

更新R中的数据表(使用data.table)

如何移除GGPlot中超出与面相交的任何格网像元

R中Gamma回归模型均方误差的两种计算方法不一致

在R中,如何从一系列具有索引名的变量快速创建数据帧?

按组和连续id计算日期差

如何调整一个facet_work()面板内的框图和移动标签之间的水平宽度?

Broom.Mixed::Augment不适用于Sample::分析

隐藏基于 case 总数的值

将字符变量出现次数不相等的字符框整形为pivot_wider,而不删除重复名称或嵌套字符变量

真实世界坐标的逆st_变换

如何使用ggsurvfit包更改风险表中的标签名称?

Ggplot2水平线和垂直线的图例图标不匹配