如何使用 ggplot2 删除 barplot 中线型图例的默认灰色填充?

bgj*_*bgj 3 r ggplot2

我有一个包含两个不同变量的条形图。\n对于其中一个因素 (gr),我在图中选择了不同的 \xc2\xb4lintype\xc2\xb4。\n“gr”的图例显示 \xc2\xb4lintype\xc2 \xb4 但用深灰色填充,我认为这很令人困惑。

\n

有谁知道如何去除填充或将其更改为白色或透明?
\n(我发现的所有提示仅更改图例的背景,但不影响灰色填充)

\n
    yval <- c(3, 7, 4, 4, 8, 9, 4, 7, 9, 6, 6, 3)\ntrt <- rep(c("A", "B", "C"), times=4)\ngr <- rep(c(rep(("case"), times = 3), rep(("control"), times = 3)), times = 2)\nvar <- c(rep(("var1"), times = 6), rep(("var2"), times = 6))  \ndf <- data.frame(yval, device, ccgroup, var)\n\nggplot(data=df, aes(x=var)) +\n  geom_bar( color = "black", size = 1, aes(weights = yval, fill = trt, linetype = gr) , position = "dodge")\n
Run Code Online (Sandbox Code Playgroud)\n

她就是剧情

\n

ste*_*fan 6

guide_legend这可以通过例如允许您设置图例中使用的填充颜色来实现。尝试这个:

library(ggplot2)

yval <- c(3, 7, 4, 4, 8, 9, 4, 7, 9, 6, 6, 3)
trt <- rep(c("A", "B", "C"), times=4)
gr <- rep(c(rep(("case"), times = 3), rep(("control"), times = 3)), times = 2)
var <- c(rep(("var1"), times = 6), rep(("var2"), times = 6))  
df <- data.frame(yval, trt, gr, var)

ggplot(data=df, aes(x=var)) +
  geom_bar(color = "black", size = 1, aes(weights = yval, fill = trt, linetype = gr) , position = "dodge") +
  guides(linetype = guide_legend(override.aes = list(fill = c(NA, NA))))
#> Warning: Ignoring unknown aesthetics: weights
Run Code Online (Sandbox Code Playgroud)