繁体   English   中英

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

[英]How to remove the default grey fill for linetype legend in barplot with ggplot2?

我有一个带有两个不同变量的条形图。 对于其中一个因素(gr),我在 plot 中选择了不同的“lintype”。 “gr”的图例显示了“lintype”,但填充了深灰色,我认为这令人困惑。

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

    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, device, ccgroup, var)

ggplot(data=df, aes(x=var)) +
  geom_bar( color = "black", size = 1, aes(weights = yval, fill = trt, linetype = gr) , position = "dodge")

她就是剧情

这可以通过例如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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM