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()
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:
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
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).