r - ggplot column chart - order of colours in brewer object has no effect -
i trying change colours on ggplot column chart. after googling, thought following code work:
require(ggplot2) require(rcolorbrewer) state <- c(rep("nswtc",5), rep("tcv",5), rep("qtc",5), rep("watc",5), rep("safa",5), rep("other",5)) year <- rep(c("11-12","12-13","13-14","14-15","15-16"),6) ##some random data funding.programme <- abs(rnorm(30)) df <- data.frame(state, year, funding.programme) ##this line makes graph in order inputted it, rather alphabetical df$state <- factor(df$state, levels=unique(df$state)) ##ugly coloured bars <- ggplot(df) + aes(x=year ,y=funding.programme, fill=year) + geom_bar(stat='identity') + facet_grid(facets=~state) + scale_y_continuous('gross issuance requirements') ##nicely coloured blues <- brewer.pal(5, "blues") blues <- rev(blues) ##the following 2 graphs have same colours bars <- ggplot(df) + aes(x=year ,y=funding.programme, fill=year) + geom_bar(stat='identity') + facet_grid(facets=~state) + scale_y_continuous('gross issuance requirements') + scale_fill_brewer(blues) bars bars <- ggplot(df) + aes(x=year ,y=funding.programme, fill=year) + geom_bar(stat='identity') + facet_grid(facets=~state) + scale_y_continuous('gross issuance requirements') + scale_fill_brewer(blues.rev) bars ##and not adjust default colours bars <- ggplot(df)+ aes(x=year,y=funding.programme, fill=year) + geom_bar(stat='identity') + facet_grid(facets=~state) + scale_y_continuous('gross issuance requirements') + scale_colour_manual(values = blues.rev) bars
but last method not work, , second , third-last charts produced identical, despite order of colours being reversed in object.
you want scale_fill_manual(values = blues)
or conversely blues.rev
(which didn't create in example code, assume typo).
only use scale_*_brewer
when you're selecting 1 of default palette's name. otherwise, use scale_*_manual
sort of thing.
the last 1 doesn't work because using colour instead of fill.
finally, carriage returns , tabs: love them, cherish them, use them!
Comments
Post a Comment