0

How to plot a bar chart with continous coloring, similar to scale_fill_brewer() but more continously?

ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar()
Carl
  • 4,232
  • 2
  • 12
  • 24
Klaus
  • 1,946
  • 3
  • 19
  • 34

2 Answers2

1

I would comment on this, but not enough rep yet...

Your problem with the Diamonds data set is that the data is discrete, meaning each value / observation belonging to it is distinct and separate. In order to do a continuous fill you need continuous data (values / observations belonging to it may take on any value within a finite or infinite interval).

When you have a continuous data set you can use the following ggplot2 command: scale_colour_gradient.

EDIT

You can try this: ggplot(diamonds, aes(clarity, fill=..count..)) + geom_bar(), but you loose the Cut information:

enter image description here

americo
  • 1,013
  • 8
  • 17
0

Color Brewer has sequential color schemes built in - check colorbrewer2.org. For instance,

ggplot(diamonds, aes(clarity, fill=cut)) + 
  geom_bar() + 
  scale_fill_brewer(palette="PuBu")

yields

enter image description here

Update:

Per OP's comment below, it appears that OP wants to map alpha to the factor levels of cut, with alpha decreasing as factor levels increase (i.e., alpha for 'Fair' should be higher than alpha for Ideal). We can manually assign alpha using scale_alpha_manual, but a simpler solution is to use scale_alpha_discrete with the levels argument defined 'in reverse':

ggplot(diamonds, aes(clarity, alpha=cut)) + geom_bar(fill="darkblue") +
  scale_alpha_discrete(range=c(1, 0.2)) #Note that range is ordered 'backwards'

Of course you can adjust the color using the fill argument to geom_bar (darkblue comes out looking fairly purple).

enter image description here

Drew Steen
  • 16,045
  • 12
  • 62
  • 90
  • I know but it should look closer to `y<-hist(rnorm(1000),breaks=30)$count df<-data.frame(x=1:length(y),y=y,key="A") df2<-data.frame(x=1:length(y),y=y*0.4,key="B") df<-rbind(df,df2) p<-ggplot(df,aes(x=x)) p<-p + geom_bar(subset=.(key =="A"),aes(y = y),stat="identity",fill = "blue", alpha = 0.2) p<-p + geom_bar(subset=.(key =="B"),aes(y = y),stat="identity",fill = "blue", alpha = 0.2) p` – Klaus Aug 20 '13 at 14:38
  • In what sense should it look closer - in terms of the actual colors that are used? In terms of alpha? I think you are looking for something more specific than you have specified in your question, but I can't quite tell what. – Drew Steen Aug 20 '13 at 14:47
  • Not in terms of a specific color. Rather in terms of the alpha, it should look that a container get filled with water. – Klaus Aug 20 '13 at 15:18