6

我已经看到了有关此主题的另一个问题,但我仍然无法更改 ggplot 中分组条形图的颜色。它为我提供了蓝色刻度,但我想要绿色刻度。我对 ggplot 非常陌生,可能遗漏了一些明显的东西。

这是我的一些代码:

TCplot=ggplot(mTCdf,aes(x=types4,y=TCs,group=years3,color=years3))
+geom_bar(aes(fill=years3),stat="identity",position="dodge",color="black")


mTCdf$types4=factor(mTCdf$types4,levels=c("Single Year Lease","Multi-Year  Lease","Permanent"))
levels(mTCdf$types4)  ###just to get my labels in my desired order

TCplot=TCplot+ggtitle("Total Costs by Transaction_Type")
+theme(plot.title=element_text(lineheight=.7,face="bold"))
+xlab("Transaction Type")
+ylab("Costs ($)")

library(scales)
TCplot=TCplot+scale_y_continuous(labels=comma)        
   TCplot=TCplot+scale_fill_manual(values=c("#66FF22","#33FF22","#33EE22","#33DD22","#33CC22","#33BB22","#33AA22","#339922","#338822","#337722","#336622"))
TCplot=TCplot+scale_fill_manual(values=c("#66FF22","#33FF22","#33EE22","#33DD22","#33CC22","#33BB22","#33AA22","#339922","#338822","#337722","#336622"))

错误:提供给离散刻度的连续值!!!啊!

***有人可以帮我应用绿色渐变吗?谢谢!!

4

2 回答 2

14

问题是您将years3列视为离散(分类)变量,而 R 认为它是连续的(数字)。@JPC 的解决方案解决了您的问题,但我建议您最好解决根本问题。这可以通过将您的years3列更改为一个因子来完成:

mTCdf$years3 <- as.factor(mTCdf$years3)

然后像你一样制作情节。

于 2013-08-19T20:57:00.393 回答
13

您想使用 scale_fill_gradient。下面是一个包含一些组成数据的快速示例

  t=data.frame(c1=c('a','a','b','b'),c2=c(1,0,1,0),c3=c(10,20,30,40))
  ggplot(t,aes(x=c1,y=c3,group=c2,fill=c2))+geom_bar(stat="identity")+scale_fill_gradient(low="green",high="darkgreen")
于 2013-08-19T20:40:10.677 回答