7

I already saw another question regarding this topic, but I still am not able to change my colors on my grouped barplot in ggplot. It is providing me with a scale of blue, but I want a scale of green. I am veryyy new to ggplot and probably am missing something obvious.

Here is some of my code:

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"))

Error: Continuous value supplied to discrete scale!!! argh!

***Can someone please help me apply a green gradient to this?? Thanks!!

Jessica Marie
  • 189
  • 1
  • 2
  • 14
  • Welcome to SO. Thank you for posting your code, but note that we can't actually reproduce it since we don't know what the `mTCdf` data frame contains. The best way to deal with this problem is to recreate your problem with a minimal example. Alternately you could use `dput` to output your actual data frame, and paste the results into your question. See here: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Drew Steen Aug 19 '13 at 20:59
  • Can you please provide a workable example? It's hard to help when I can't reproduce your results. – emhart Aug 19 '13 at 21:00

2 Answers2

15

The problem is that you are treating your years3 column as if it is a discrete (categorical) variable, when R thinks it is continuous (numeric). @JPC's solution fixes your problem, but I suggest that you would do better to fix the underlying problem. This can be done by changing your years3 column to a factor:

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

and then making the plot as you have done.

Drew Steen
  • 16,045
  • 12
  • 62
  • 90
13

You want to be using scale_fill_gradient. Below a quick example with some made up data

  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")
JPC
  • 1,891
  • 13
  • 29
  • 2
    An explanation would be useful. I have the same error as the OP, but I don't see the solution here... it may be obvious but can't see it yet ... – PatrickT Nov 23 '13 at 18:18
  • 3
    since she is using a continuous variable for fill, ggplot uses a gradient. By changing the values in scale_fill_gradient to the appropriate greens she wants, it will do the rest for her. Is that clearer? – JPC Dec 09 '13 at 16:57