There are several ways to bring colors to your plot in ggplot2. You may:

We will see how to modify colors in a plot using simple examples. Let’s start with the code for two reference plots, a density plot and a violin plot. These plots are stored in the objects baseplot1 and baseplot2 so that we can reuse them throughout the whole tutorial:

# code for violin plot
baseplot1 <- ggplot(df, aes(category, values, fill=category)) +
  geom_violin()

# code for density plot
baseplot2 <- ggplot(df2, aes(values1, values2)) +
  stat_density_2d(aes(fill = ..level..), geom = "polygon")



Color palette by default

ggplot2 automatically generates a palette based on the number of items to color (number of categories, etc.) and a color wheel. You may read more about this palette HERE. The aim is to provide you with a “well-balanced” set of colors which can easily be distinguished from each other without drawing to much attention onto one specific items. Here are our reference plots colored by ggplot2:

baseplot1        # left plot, violin plot
baseplot2        # right plot, density plot

The colors are “OK” as long as you do not have more than a handful of groups, but this reaches its limits in terms of aesthetics when handling a larger number of groups.

The viridis color palettes

The viridis package contains a series a colorful palettes which were created to bring more flashy colors to your plots, while providing better distinction between groups. The palettes were also designed so that the color differences would remain perceivable by colorblind people. You may read more about this palette HERE. To use viridis in your figures, simply add scale_fill_viridis_d() (when handling discrete predictor variables) and scale_fill_viridis_c() (when handling continuous predictor variables). Let’s look at our examples colored with viridis:

baseplot1 + 
  scale_fill_viridis_d()      # left plot, violin plot
baseplot2 + 
  scale_fill_viridis_c()      # right plot, density plot

Of course, if the code uses color= instead of fill= for grouping, use scale_fill_viridis_d() and scale_fill_viridis_c().

Note that the example above results of the default viridis palette. Other palettes are available and you just have to add their name with option= to the function to activate them. The following plots use the palette Inferno:

baseplot1 +
  scale_fill_viridis_d(option="inferno")        # left plot, violin plot
baseplot2 +
  scale_fill_viridis_c(option="inferno")        # right plot, density plot



Brewer/Distiller

Brewer and Distiller work in a similar way. They offer many sets of colors referenced HERE. Pick a set and add it to your plot with scale_fill_brewer or scale_color_brewer (when handling discrete variables) and scale_fill_distiller() or scale_color_distiller() (when handling continuous variables). Add palette=" " and the name of the palette between the parentheses.

baseplot1 + 
  scale_fill_brewer(palette="Set3")             # left plot, violin plot
baseplot2 +
  scale_fill_distiller(palette="Set3")          # right plot, density plot



Picking your own colors

If you are really picky about colors and have the time to select every single color for your graph, use scale_fill_manual(), scale_color_manual() or scale_fill_gradient() and then indicate between the parentheses either the R name or the hex code for each of them.

Note that scale_fill_manual() and scale_color_manual() shall be used with discrete variables. Always make sure that you have chosen a color for each of the groups or categories, otherwise, ggplot will return a warning/error message and no plot. scale_fill_gradient() shall be used with continuous variables and will create a gradient based on two given colors. Here you will find the R names and a tool to provide you with hex codes. Here are our two reference plots colored using R names:

baseplot1 +
  scale_fill_manual(values=c("darkblue", "aquamarine1", "lightsalmon2", "orange2"))    # left plot, violin plot
baseplot2 +
  scale_fill_gradient(low= "darkblue", high= "lightsalmon2")                           # right plot, density plot



And the same plots colored using the matching Hex codes (note that Hex codes start with # followed by 6 digits:

baseplot1 +
  scale_fill_manual(values=c("#00008B", "#7FFFD4", "#EE9572", "#EE9A00"))              # left plot, violin plot
baseplot2 +
  scale_fill_gradient(low= "#00008B", high= "#EE9572")                                 # right plot, density plot