我有以下数据,并希望创建一个像这样的分组条形图:
data<-as.data.frame(c("a","b","c","a","b","c"))
colnames(data)<-"Y"
data$X<-c("x","x","x","y","y","y")
data$Z<-c(1,2,3,1,2,3)
ggplot(data, aes(x=X, y=Z, fill=Y) +
geom_bar(stat="identity", colour="black", position="dodge", size=0.25, width=0.8, alpha=0.8) +
scale_fill_manual(values=c("red","red","red","blue","blue","blue"))
在代码的最后一行,我希望改变条形图的颜色 - 我希望组中的所有条形图&#34; x&#34;被涂上红色和组的条形&#34; y&#34;变成蓝色。但是,如下面的结果所示,我无法使用scale_fill_manual
设法执行此操作。
答案 0 :(得分:4)
您需要将group
和fill
映射到正确的变量:
ggplot(data, aes(x=X, y=Z, group=Y, fill=X)) +
geom_bar(stat="identity", colour="black", position="dodge", size=0.25, width=0.8, alpha=0.8) +
scale_fill_manual(values=c("red","blue"))