• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Data Viz with Python and R

Learn to Make Plots in Python and R

  • Home
  • Python Viz
  • Seaborn
  • Altair
  • R Viz
  • ggplot2
  • About
    • Privacy Policy
  • Show Search
Hide Search

How to Color Boxplots with R Colorbrewer Color Palettes?

datavizpyr · March 27, 2020 ·

Adding right colors to the plots greatly help convey the main message of the plot. Adding colors manually can be a bit challenging. Color palettes available from RColorBrewer package make it easy add right set of colors to plots made with ggplo2.

In this tutorial, we will see examples of adding colors to Boxplots using color palettes available from RColorBrewer.

Let us load tidyverse and RColorbrewer package.

library(tidyverse)
library(RColorBrewer)
theme_set(theme_bw(base_size = 16))

We will use the fast food data from tidytuesday project.

data_url <-"https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2018/2018-09-04/fastfood_calories.csv"
fast_food <- read_csv(data_url)

Let us select just two variables; fast food restaurant and amount of cholesterol in their food products. We will also use forcats’ fct_reorder to level the factors by cholesterol values.

fast_food <- fast_food %>%
  mutate(restaurant=fct_reorder(restaurant, cholesterol)) %>%
  select(restaurant,cholesterol)

This is how our fast food data looks like.

head(fast_food)

## # A tibble: 6 x 2
##   restaurant cholesterol
##   <fct>            <dbl>
## 1 Mcdonalds           95
## 2 Mcdonalds          130
## 3 Mcdonalds          220
## 4 Mcdonalds          155
## 5 Mcdonalds          120
## 6 Mcdonalds           80

Let us first make simple boxplot and color the boxes uses restaurant. ggplot2 automatically chooses the colors for us and colors the boxplot with jitter. Here we also make sure x-axis label text do not overlap using ggplot2 v2.3.0’s guide_axis() function

fast_food %>%
  ggplot(aes(x=restaurant, y=cholesterol, color=restaurant)) +
  geom_boxplot()+
  geom_jitter(width=0.15, alpha=0.5)+
  scale_y_log10()+
  labs(x="Restaurant") +
  scale_x_discrete(guide = guide_axis(n.dodge=2))+
  theme(legend.position="none")
ggsave("Coloring_Boxplots_with_ggplot2.jpg")
Coloring Boxplots with ggplot2
Coloring Boxplots with R Color Brewer

Coloring Boxplots with ggplot2’s scale_fill_brewer() using RColorBrewer

The colors on boxplot look good. However, sometime you might want to use color palettes available from RColorbrewer package. ggplot2 has two functions available for using color palettes in ggplot2, one is scale_fill_brewer() and scale_color_brewer().

scale_fill_brewer() function is useful when you want to use color palette from RColorBrewer and fill boxplots with colors color palette.

Here we have used Orange and Red color palette using ‘palette=”OrRd”‘ inside scale_fill_brewer(). Check out this post to learn more about the available color palettes in RColorBrewer.

fast_food %>%
  ggplot(aes(x=restaurant, y=cholesterol, fill=restaurant)) +
  geom_boxplot()+
  geom_jitter(width=0.15, alpha=0.5)+
  scale_y_log10()+
  labs(x="Restaurant") +
  theme(legend.position="none")+
  scale_x_discrete(guide = guide_axis(n.dodge=2))+
  scale_fill_brewer(palette="OrRd")
ggsave("Coloring_Boxplots_with_RColorBrewer_palette_OrRd_ggplot2.jpg")
Coloring Boxplots with RColorBrewer: scale_fill_brewer()
Coloring Boxplots with RColorBrewer: scale_fill_brewer()

Coloring Boxplots with ggplot2’s scale_color_brewer() using RColorBrewer

scale_color_brewer() function is useful when you want to use color palette from RColorBrewer and just color boxplots using specific color palette.

Here we have used Paired color palette that uses pair of colors using palette=”Paired” inside scale_color_brewer()

fast_food %>%
  mutate(restaurant=fct_reorder(restaurant, cholesterol)) %>%
  ggplot(aes(x=restaurant, y=cholesterol, color=restaurant)) +
  geom_boxplot()+
  geom_jitter(width=0.15, alpha=0.5)+
  scale_y_log10()+
  labs(x="Restaurant") +
  theme(legend.position="none")+
  scale_x_discrete(guide = guide_axis(n.dodge=2))+
  scale_color_brewer(palette="Paired")
Coloring Boxplots with RColorBrewer:  Scale Color Brewer
Coloring Boxplots with RColorBrewer: Scale Color Brewer

Related posts:

Boxplots with Jittered Data points with outlier shapeA mistake to Avoid while making boxplot with datapoints in ggplot2 How to Make Heatmap with ggplot2?How To Make Simple Heatmaps with ggplot2 in R? Stacked Barplots Side By Side with ggplot2 in RHow to Make Horizontal Stacked Barplots with ggplot2 in R? Scatter Plot R: Fill color by variableHow To Color Scatter Plot by Variable in R with ggplot2?

Filed Under: Color Palettes in RColorBrewer, Coloring Boxplots with RColorBrewer, R Tagged With: ggplot2 Boxplot, RColorBrewer

Primary Sidebar

Tags

Altair barplot Boxplot boxplot python boxplot with jiitered text labels Bubble Plot Color Palette Countplot Density Plot Facet Plot gganimate ggplot2 ggplot2 Boxplot ggplot2 error ggplot boxplot ggridges ggtext element_markdown() Grouped Barplot R heatmap heatmaps Histogram Histograms Horizontal boxplot Python lollipop plot Maps Matplotlib Pandas patchwork pheatmap Pyhon Python R RColorBrewer reorder boxplot ggplot Ridgeline plot Scatter Plot Scatter Plot Altair Seaborn Seaborn Boxplot Stock Price Over Time Stripplot UpSetR Violinplot Violin Plot World Map ggplot2

Buy Me a Coffee

Copyright © 2025 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version