Open In App

Change Color of ggplot2 Boxplot in R

In this article, we are going to see how to change the color of boxplots using ggplot2 in R Programming Language. 

We have considered the built-in data frame “ChickWeight”. It contains information about the feed type and growth rate of chickens for six different types of foods like casein, soybean, etc.  It has two vectors feed which will be in the x-axis and weight which will be in the y-axis.



Setting Default colors

Here we will use the color keyword. Outliers are observations that are located outside the whiskers of a box plot. We will keep the default black color for them. Use the command outlier.color to add color to the outliers in the plot. Since we need the same color in the outlines, we will write the command inside the geom_boxplot( ).

Example:






# Same outline color
library(ggplot2)
 
ch <- ggplot(chickwts,aes(x=feed,y=weight))+
      geom_boxplot(color="red",outlier.color="black")
 
ch

Output:

Since the color is variable here, we will write the command color inside aes( ) of ggplot assigned to the x-axis value. The outliers will have the same black color, so write the command inside the geom_boxplot( ) only.

Example:




# Different outline color
library(ggplot2)
 
ch <- ggplot(chickwts,aes(x=feed,y=weight,color=feed))+
      geom_boxplot(outlier.color="black")
 
ch

Output: 

Here, we will use the keyword fill. Since we need the same color in the fill of boxplots, we will write the command inside the geom_boxplot( ). By default, fill for outliers is black.

Example:




# Same fill color
library(ggplot2)
 
ch <- ggplot(chickwts,aes(x=feed,y=weight))+
      geom_boxplot(fill="violet")
 
ch

Output: 

Since the color of fill is variable here, we will write the command fill inside aes( ) of ggplot assigned to the x-axis value.

Example:




# Different fill color
library(ggplot2)
 
ch <- ggplot(chickwts,aes(x=feed,y=weight,fill=feed))+
      geom_boxplot()
 
ch

Output: 

Setting color manually

1) For outlines

In this we use the following functions :

Syntax:

scale_color_manual( values)

Syntax:

scale_color_brewer(palette)

Example:




library(ggplot2)
 
ch <- ggplot(chickwts,aes(x=feed,y=weight,color=feed))+
      geom_boxplot(outlier.colour = "black")
 
# Assign custom color
ch+scale_color_manual(values=c("#999999",
                               "purple",
                               "#33FFFF",
                               "red",
                               "green",
                               "brown"))
# Assign brewer color
ch+scale_color_brewer(palette="Dark2")
 
# Assign gray scale
ch+scale_color_grey() + theme_classic()

Output:

2) For filling

Similarly, we can fill the box plot. The functions used for this are:

Syntax:

scale_fill_manual( values)

Syntax:

scale_fill_brewer(palette)

Example:




library(ggplot2)
 
ch <- ggplot(chickwts,aes(x=feed,y=weight,fill=feed))+
      geom_boxplot()
 
ch
 
# Assign custom color
ch+scale_fill_manual(values=c("#999999",
                               "purple",
                               "#33FFFF",
                               "red",
                               "green",
                               "brown"))
 
# Assign brewer color
ch+scale_fill_brewer(palette="Dark2")
 
# Assign gray scale
ch+scale_fill_grey() + theme_classic()

Output:


Article Tags :