I have a summary histogram that looks like this:

While the colors look beautiful, it is ridiculous to have so many identical colors representing different drugs. I would like to have a separate color palette for each column in the graph, for example, class1 can use the βBluesβ palette, while class2 can use the βBuGnβ palette (the color palette names are found here )
I found some examples where people manually coded colors for each bar (for example, here ), but I'm not sure what I ask for is possible - these bars should be based on palettes, since there are so many drugs in each class of drugs.
Code for creating the above graph:
library(ggplot2) library(plyr) library(RColorBrewer) drug_name <- c("a", "a", "b", "b", "b", "c", "d", "e", "e", "e", "e", "e", "e", "f", "f", "g", "g", "g", "g", "h", "i", "j", "j", "j", "k", "k", "k", "k", "k", "k", "l", "l", "m", "m", "m", "n", "o") df <- data.frame(drug_name) #get the frequency of each drug name df_count <- count(df, 'drug_name') #add a column that specifies the drug class df_count$drug_class <- vector(mode='character', length=nrow(df_count)) df_count$drug_class[df_count$drug_name %in% c("a", "c", "e", "f")] <- 'class1' df_count$drug_class[df_count$drug_name %in% c("b", "o")] <- 'class2' df_count$drug_class[df_count$drug_name %in% c("d", "h", "i")] <- 'class3' df_count$drug_class[df_count$drug_name %in% c("g", "j", "k", "l", "m", "n")] <- 'class4' #expand color palette (from http://novyden.blogspot.com/2013/09/how-to-expand-color-palette-with-ggplot.html) colorCount = length(unique(df_count$drug_name)) getPalette = colorRampPalette(brewer.pal(9, "Set1")) test_plot <- ggplot(data = df_count, aes(x=drug_class, y=freq, fill=drug_name) ) + geom_bar(stat="identity") + scale_fill_manual(values=getPalette(colorCount)) test_plot