我有一个包含两个不同变量的条形图。\n对于其中一个因素 (gr),我在图中选择了不同的 \xc2\xb4lintype\xc2\xb4。\n“gr”的图例显示 \xc2\xb4lintype\xc2 \xb4 但用深灰色填充,我认为这很令人困惑。
\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")\nRun Code Online (Sandbox Code Playgroud)\n
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)
