Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2: Fix colors to factor levels

Tags:

I'm working on a larger project for which I am creating several plots in ggplot2. The plots are concerned with plotting several different outcomes across several different discreet categories (think: countries, species, types). I would like to completely fix the mapping of discrete types to colors such that Type=A is always displayed in red, Type=B is always displayed in blue, and so on across all plots irrespective of what other factors are present. I know about scale_fill_manual() where I can provide color values manually and then work with drop = FALSE which helps in dealing with unused factor levels. However, I find this extremely cumbersome since every plot will need some manual work to deal with sorting the factors in the right way, sorting color values to match factor sorting, dropping unused levels, etc.

What I am looking for is a way where I can map once and globally factor levels to specific colors (A=green, B=blue, C=red, ...) and then just go about plotting whatever I please and ggplot picking the right colors.

Here is some code to illustrate the point.

# Full set with 4 categories df1 <- data.frame(Value = c(40, 20, 10, 60),                    Type = c("A", "B", "C", "D"))  ggplot(df1, aes(x = Type, y = Value, fill = Type)) + geom_bar(stat = "identity")   # Colors change complete because only 3 factor levels are present df2 <- data.frame(Value = c(40, 20, 60),                    Type = c("A", "B", "D"))  ggplot(df2, aes(x = Type, y = Value, fill = Type)) + geom_bar(stat = "identity")   # Colors change because factor is sorted differently df3 <- data.frame(Value = c(40, 20, 10, 60),                    Type = c("A", "B", "C", "D")) df3$Type <- factor(df3$Type, levels = c("D", "C", "B", "A"), ordered = TRUE)  ggplot(df3, aes(x = Type, y = Value, fill = Type)) + geom_bar(stat = "identity") 
like image 812
Chris Avatar asked Apr 16 '17 17:04

Chris


People also ask

How do I assign a value to a color in R?

In R, colors can be specified either by name (e.g col = “red”) or as a hexadecimal RGB triplet (such as col = “#FFCC00”). You can also use other color systems such as ones taken from the RColorBrewer package.

How do I change the color scheme in R?

Navigate to Tools → Global options → Appearance and switch the theme in the Editor Theme option. By default, you will have the Textmate theme activated. There is a wide in-built variety of themes to choose, from light to dark themes.

What are Ggplot default colors?

By default, ggplot2 chooses to use a specific shade of red, green, and blue for the bars.


2 Answers

You could make a custom plot function (including scale_fill_manual and reasonable default colours) in order to avoid repeating code:

library(ggplot2) custom_plot <- function(.data,   colours = c("A" = "green", "B" = "blue", "C" = "red", "D" = "grey"))  {   ggplot(.data, aes(x=Type, y=Value, fill= Type)) + geom_bar(stat="identity") +    scale_fill_manual(values = colours) }  df1 <- data.frame(Value=c(40, 20, 10, 60), Type=c("A", "B", "C", "D")) df2 <- data.frame(Value=c(40, 20, 60), Type=c("A", "B", "D")) df3 <- data.frame(Value=c(40, 20, 10, 60), Type=c("A", "B", "C", "D")) df3$Type <- factor(df3$Type, levels=c("D", "C", "B", "A"), ordered=TRUE)  custom_plot(df1) custom_plot(df2) custom_plot(df3) 
like image 163
Kristoffer Winther Balling Avatar answered Sep 24 '22 00:09

Kristoffer Winther Balling


You could define your own custom scale, if you like. If you look at the source for scale_fill_manual,

scale_fill_manual #> function (..., values)  #> { #>     manual_scale("fill", values, ...) #> } #> <environment: namespace:ggplot2> 

it's actually quite simple:

library(ggplot2)  scale_fill_chris <- function(...){     ggplot2:::manual_scale(         'fill',          values = setNames(c('green', 'blue', 'red', 'orange'), LETTERS[1:4]),          ...     ) }  df1 <- data.frame(Value = c(40, 20, 10, 60),                    Type = c("A", "B", "C", "D"))  ggplot(df1, aes(x = Type, y = Value, fill = Type)) +      geom_col() +      scale_fill_chris() 

df2 <- data.frame(Value = c(40, 20, 60),                    Type = c("A", "B", "D"))  ggplot(df2, aes(x = Type, y = Value, fill = Type)) +      geom_col() +      scale_fill_chris() 

df3 <- data.frame(Value = c(40, 20, 10, 60),                    Type = c("A", "B", "C", "D")) df3$Type <- factor(df3$Type, levels = c("D", "C", "B", "A"), ordered = TRUE)  ggplot(df3, aes(x = Type, y = Value, fill = Type)) +      geom_col() +      scale_fill_chris() 

like image 28
alistaire Avatar answered Sep 25 '22 00:09

alistaire