R ggplot2 Boxplot

The R ggplot2 boxplot is useful for graphically visualizing the numeric data group by specific data. Let us see how to Create an R ggplot2 boxplot, Format the colors, changing labels, drawing horizontal boxplots, and plot multiple boxplots using R ggplot2 with an example.

For this r ggplot2 Boxplot demo, we use two data sets provided by the R Programming, and they are: ChickWeight and diamonds data set

Create R ggplot2 Boxplot

In this example, we show how to create a Boxplot using the ggplot2 package in R. For this ggplot2 boxplot demo, we are going to use the ChickWeight data set, provided by the Studio.

# Create a R ggplot boxplot

# Importing the ggplot2 library
library(ggplot2)

# Create a Boxplot
ggplot(ChickWeight, aes(x = Diet, y = weight)) + 
  geom_boxplot()
R ggplot2 Boxplot 1

Change Colors of a ggplot2 Boxplot in R

In this example, we show how to change the R ggplot Boxplot box colors using fill argument

  • fill: Please specify the color you want to use for your Boxplot. Type colors() in your console to get the list of colors available in R programming.

TIP: ggplot2 package not installed by in R Programming default. Please refer Install R Packages article to understand installing the package.

# Add Color to R ggplot boxplot

# Importing the ggplot2 library
library(ggplot2)

# Create a Boxplot
ggplot(diamonds, aes(x = cut, y = price)) + 
  geom_boxplot(fill = "midnightblue")
R ggplot2 Boxplot 2

Change Colors of a ggplot2 Boxplot in R example 2

In this example, we change the R ggplot Boxplot box colors using column data. Here, we are using the cut column data to differentiate the colors

NOTE: If you require to import data from external files, then please refer to R Read CSV to understand the steps involved in CSV file import

# Add Color to R ggplot boxplot

# Importing the ggplot2 library
library(ggplot2)

# Create a Boxplot
ggplot(diamonds, aes(x = cut, y = price, fill = cut)) + 
  geom_boxplot(outlier.colour="black", outlier.shape=16, outlier.size=2)
R ggplot2 Boxplot 3

Change Outliners of R ggplot2 Boxplot

In this example, we show how to change the R ggplot boxplot outliners colors using the following arguments

  • outlier.color: Please specify the color you want to use for your outliner. Type colors() in your console to get the list of colors available in R programming
  • outlier.shape: Please specify the shape you want to use for the outliner for the R ggplot boxplot.
  • outlier.size: Specify the size of the outliner.
# Change Outliers in a R ggplot boxplot

# Importing the ggplot2 library
library(ggplot2)

# Create a Boxplot
ggplot(diamonds, aes(x = cut, y = price, fill = cut)) + 
  geom_boxplot(outlier.color = "seagreen", outlier.shape = 8, outlier.size = 2)
R ggplot2 Boxplot 4

Add Mean to R ggplot2 Boxplot

In this example, we add Mean value to R ggplot boxplot using the stat_summary argument

# Adding Mean to a R ggplot boxplot

# Importing the ggplot2 library
library(ggplot2)

# Create a Boxplot
ggplot(diamonds, aes(x = cut, y = price, fill = cut)) + 
  geom_boxplot() +
  stat_summary(fun.y = "mean", geom = "point", shape = 8, size = 2, color = "white")
R ggplot2 Boxplot 5

Alter Legend position of an R ggplot2 Boxplot

By default, ggplot position the legend at the right side of a Boxplot in R. In this example, we change the legend position from right to the top. Note, You can use legend.position = “none” to completely remove the legend.

# Alter lengend Position in a a R ggplot boxplot

# Importing the ggplot2 library
library(ggplot2)

# Create a Boxplot
ggplot(diamonds, aes(x = cut, y = price, fill = cut)) + 
  geom_boxplot() +
  theme(legend.position = "top")
R ggplot2 Boxplot 6

Notch argument in R Boxplot

Here, we draw a line on each side of the boxes using notch argument in R ggplot boxplot

  • notch: It is a Boolean argument. If it is TRUE, a notch drawn on each side of the box.

TIP: If the notches of 2 plots overlapped, then we can say that the medians of them are the same. Otherwise, they are different.

# Notch argument in R ggplot boxplot

# Importing the ggplot2 library
library(ggplot2)

# Create a Boxplot
ggplot(ChickWeight, aes(x = Diet, y = weight)) + 
  geom_boxplot(fill = "chocolate", notch = TRUE)
R ggplot2 Boxplot 7

Horizontal ggplot Boxplot in R

In this example, we change the default vertical boxplot into a horizontal boxplot in R using coord_flip() function

# Create Horizontal R ggplot boxplot

# Importing the ggplot2 library
library(ggplot2)

# Create a Boxplot
ggplot(ChickWeight, aes(x = Diet, y = weight)) + 
  geom_boxplot() +
  coord_flip()
R ggplot2 Boxplot 8

Multiple ggplot Boxplot in R

In this example, we show how to group multiple ggplot boxplots in R programming

# Multiple R ggplot boxplot

# Importing the ggplot2 library
library(ggplot2)

# Create a Boxplot Importing
ggplot(diamonds, aes(x = cut, y = price, fill = clarity)) + 
  geom_boxplot()
R ggplot2 Boxplot 9

How to use Facets in R ggplot2 Boxplot

This R ggplot boxplot example, we draw multiple boxplots by dividing the data based on a column value. Here, we are using the clarity column data to divide the boxplots.

# Use Facet in R ggplot boxplot

# Importing the ggplot2 library
library(ggplot2)

# Create a Boxplot Importing
ggplot(diamonds, aes(x = cut, y = price, fill = clarity)) + 
  geom_boxplot() +
  facet_wrap(~ clarity)
R ggplot2 Boxplot 10

How to use Facets in R ggplot2 Boxplot example 2

By default, facet_wrap() assign the same y-axis to all the boxplot. However, you can change it (giving independent axis) to each R ggplot boxplot by adding one more attribute called scale.

# Use Facet in R ggplot boxplot

# Importing the ggplot2 library
library(ggplot2)

# Create a Boxplot Importing
ggplot(diamonds, aes(x = cut, y = price, fill = clarity)) + 
  geom_boxplot() +
  facet_wrap(~ clarity, scale = "free")
R ggplot2 Boxplot 11

Assigning names to R ggplot Boxplot

In this example, we assign names to ggplot2 boxplot, X-Axis, and Y-Axis using labs function

# Add Labels to R ggplot boxplot

# Importing the ggplot2 library
library(ggplot2)

# Create a Boxplot Importing
ggplot(diamonds, aes(x = cut, y = price, fill = cut)) + 
  geom_boxplot() +
  labs(title="GGPLOT BOXPLOT", x="Price in Dollars", y="Cut")
R ggplot2 Boxplot 12

Change R ggplot2 box plot Theme

In this example, we change the default theme of an R ggplot2 box plot

  • theme_dark(): We are using this function to change the ggplot2 boxplot default theme to dark. Type theme_, then R Studio intelligence shows the list of available options. For example, theme_grey().
# Change theme of an R ggplot boxplot

# Importing the ggplot2 library
library(ggplot2)

# Create a Boxplot Importing
ggplot(diamonds, aes(x = cut, y = price, fill = cut)) + 
  geom_boxplot() +
  labs(title="GGPLOT BOXPLOT", x="Price in Dollars", y="Cut") +
  theme_dark()
R ggplot2 Boxplot 13