条形图中的颜色

时间:2018-10-18 21:09:08

标签: r ggplot2 colors

我正在尝试绘制在y轴上带有百分比的条形图,在X轴上带有6个类别的分类变量。构面是由另一个具有7个级别的类别变量完成的。 我想将不同的颜色添加到表示每个方面的6个变量x级别的条上。

我尝试编写此代码:

ggplot(data= choices, aes(x=pre2_Altdrinks)) +
    geom_bar(aes(y = (..count..)/sum(..count..)), alpha=0.75) + 
    facet_grid(~Residence) +
    scale_fill_brewer(palette="Dark2")
labs(title = "Alternate drinks protective behaviour across different residence status",
         x = "alternate drinks", y= "Percentage")

它生成没有颜色的该图: graph

然后我尝试了以下代码:

ggplot(data= choices, aes(x=pre2_Altdrinks,fill= pre2_Altdrinks )) +
    geom_bar(aes(y = (..count..)/sum(..count..)), alpha=0.75)+ 
    facet_grid(~Residence) +
    scale_fill_brewer(palette="Dark2")
labs(title = "Alternate drinks protective behaviour across different residence status",
         x = "alternate drinks", y= "Percentage")

它给了我

Error: Continuous value supplied to discrete scale

1 个答案:

答案 0 :(得分:1)

尝试将fill = Residence放入“ geom_bar”美学通话中。