Change the color palette in a histogram

I am trying to change the colors of my histogram, but not sure how to do it, what is my code:

qplot(user, count, data=count_group, geom="histogram", fill=group, xlab = "users", ylab="count", main="Users")+ opts(axis.text.x=theme_text(angle=90, hjust=0, size=7)) 

here is the histogram that I get, but the default colors are too bright,

enter image description here

I would like to use colors like this

I tried to add a line, but that did not work.

  scale_fill_brewer(palette = palette) 
+4
source share
2 answers

If you want to use the Brewer Set1 with this set of groups, you can do something like this:

 library(ggplot2) count_group <- data.frame(user=factor(rep(1:50, 2)), count=sample(100, 100, replace=T), group=factor(rep(LETTERS[1:20], 5))) library(RColorBrewer) cols <- colorRampPalette(brewer.pal(9, "Set1")) ngroups <- length(unique(count_group$group)) qplot(user, count, data=count_group, geom="histogram", fill=group, xlab = "users", ylab="count") + opts(axis.text.x=theme_text(angle=90, hjust=0, size=7)) + scale_fill_manual(values = cols(ngroups)) 

Brewer Set 1 with many groups in ggplot


EDIT

You can create and use multiple colorRampPalette s, for example. To assign blues to groups AJ and red for groups K to T:

 blues <- colorRampPalette(c('dark blue', 'light blue')) reds <- colorRampPalette(c('pink', 'dark red')) qplot(user, count, data=count_group, geom="histogram", fill=group, xlab = "users", ylab="count") + opts(axis.text.x=theme_text(angle=90, hjust=0, size=7)) + scale_fill_manual(values = c(blues(10), reds(10))) # blues(10) and reds(10) because you want blues for the first ten # groups, and reds thereafter. Each of these functions are equivalent # to providing vectors containing ten hex colors representing a gradient # of blues and a gradient of reds. 

ggplot with blues and reds

+11
source

Answers to jbaums. As far as I can understand, with the new ggplot2 (as of March 2014) the following syntax is available:

 p <- qplot(user, count, data = count_group, geom = "histogram", stat = "bin2d", fill = group, xlab = "users", ylab = "count" ) p <- p + theme(axis.text.x = element_text(angle = 90, hjust = 0, size = 7)) p <- p + scale_fill_manual(values = cols(ngroups)) p 

It was a bit long for comment, but this is not a complete answer, the rest of the code is given by jbaums, who need to be thanked!

+1
source

Source: https://habr.com/ru/post/1397984/


All Articles