gpt4 book ai didi

r - 如何在一个图例中结合 alpha 和颜色

转载 作者:行者123 更新时间:2023-12-04 14:41:07 26 4
gpt4 key购买 nike

我有一个堆叠条形图,其中颜色代表类别,我调整了 alpha 以将它们分割为 2。
图例显示了 alpha(灰色阴影)和颜色。
但是我想制作一个包含组合的图例。

为了将它们结合起来,我查看了 this question但我不能结合阿尔法和填充。
这是一个不起作用的可重复数字:

mtcars %>% 
ggplot(aes(gear, mpg, fill = as.factor(vs), alpha = as.factor(am)))+
geom_bar(stat = "identity")+
scale_fill_manual(name = "legend",
values = c(
"0" = "red",
"1" = "blue",
"0"="red",
"1"="blue"
),
labels = c("V-engine, automatic",
"V-engine, manual",
"Straight-engine, automatic",
"Straight-engine, manual")
)+
scale_alpha_manual(name = "legend",
values = c(
"0" = 1,
"1"=2/5,
"0"=1,
"1"=2/5
),
labels = c(
"V-engine, automatic",
"V-engine, manual",
"Straight-engine, automatic",
"Straight-engine, manual"
) )

image

最佳答案

我没有使用 aes(alpha) 来解决您的问题但治疗alphafill.colourvs 的组合和 am .

mtcars %>% 
ggplot(aes(gear, mpg, fill = interaction(as.factor(vs), as.factor(am)))) +
geom_bar(stat = "identity") +
scale_fill_manual(name = "legend",
values = c(
"0.0" = alpha("red", 1),
"1.0" = alpha("blue", 1),
"0.1" = alpha("red", 2/5),
"1.1" = alpha("blue", 2/5)
),
labels = c("V-engine, automatic",
"V-engine, manual",
"Straight-engine, automatic",
"Straight-engine, manual")
)

enter image description here

关于r - 如何在一个图例中结合 alpha 和颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39249457/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com