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. And format the colors, changing labels, drawing horizontal, and 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 demo, we are going to use the ChickWeight data set, provided by the Studio.

library(ggplot2)

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. 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.

library(ggplot2)

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

Change Colors 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 Read CSV to understand the steps involved in CSV file import

library(ggplot2)

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.
  • outlier.size: Specify the size of the outliner.
library(ggplot2)

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

library(ggplot2)

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. 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.

library(ggplot2)

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.

library(ggplot2)

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 into a horizontal boxplot in R using coord_flip() function

library(ggplot2)

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

library(ggplot2)

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 example, we draw multiple boxplots by dividing the data based on a column value. Here, we are using the clarity column data to divide them.

library(ggplot2)

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

How to use Facets example 2

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

library(ggplot2)

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

Assigning names

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

library(ggplot2)

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 default theme to dark. Type theme_, then R Studio intelligence shows the list of available options. For example, theme_grey().
library(ggplot2)

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