在R中自定义分组的条形图颜色

问题描述 投票:0回答:1

我正在尝试根据其变量(男性或女性)更改条形的填充颜色

这是我的有效代码:

ggplot(sex_excercise_genhlth, aes(genhlth, prop_exer, fill=sex)) + 
geom_bar(stat="identity", colour="black", position="dodge" ) + labs(title="Proportions of males to females who exercised in last 30 days compared to their general health", x="General Health", y="Proportion Exercise")

“

当添加以下代码时出现错误:

+scale_fill_manual(values=c(“blue”,“pink”))
r ggplot2 plot fill
1个回答
0
投票

这里是一个可行的示例,说明如何在用例中正确使用scale_fill_manual

首先,我将创建一些虚拟数据:

df <- data.frame(sex = rep(c("Male", "Female"), 5),
           place = rep(letters[1:5], each = 2),
           vals = rep(c(0.1, 0.2), 5) + seq(0.9, 0.45, -0.05))

现在是情节

ggplot(data = df, aes(x = place, y = vals)) + 
  geom_bar(stat = "identity", 
           colour = "black", 
           aes(fill = sex), 
           position = "dodge") +
   labs(title="Proportions of people exercising", 
        x="General Health", 
        y="Proportion Exercise") +
   scale_fill_manual(values = c("pink", "blue"))

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.