Modify Box & Fill Colors of ggplot2 Boxplot in R (2 Examples)

In this article, I’ll explain how to modify the colors of ggplot2 boxplots in R.

Preparing the Examples

data(iris)                       # Loading example data to RStudio
head(iris)
#   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1          5.1         3.5          1.4         0.2  setosa
# 2          4.9         3.0          1.4         0.2  setosa
# 3          4.7         3.2          1.3         0.2  setosa
# 4          4.6         3.1          1.5         0.2  setosa
# 5          5.0         3.6          1.4         0.2  setosa
# 6          5.4         3.9          1.7         0.4  setosa
install.packages("ggplot2")      # Install ggplot2 package
library("ggplot2")               # Load ggplot2 package
ggplot(iris, aes(x = Species,    # All ggplot2 boxplots without color
                 y = Petal.Width)) +
  geom_boxplot()

r graph figure 1 modify box fill colors ggplot2 boxplot r

Example 1: Different Filling Color for Each ggplot2 Boxplot

ggplot(iris, aes(x = Species,    # Modify filling colors
                 y = Petal.Width,
                 fill = Species)) +
  geom_boxplot()

r graph figure 2 modify box fill colors ggplot2 boxplot r

Example 2: Different Box Color for Each ggplot2 Boxplot

ggplot(iris, aes(x = Species,    # Modify box colors
                 y = Petal.Width,
                 col = Species)) +
  geom_boxplot()

r graph figure 3 modify box fill colors ggplot2 boxplot r

Further Resources & Related Tutorials

In the following, you can find some further resources on topics such as colors, lines, and plot legends:

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top