Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs
    • SQL FAQ’s

R ggplot2 Boxplot

by suresh

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()

OUTPUT

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")

OUTPUT

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)

OUTPUT

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)

OUTPUT

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")

OUTPUT

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")

OUTPUT

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)

OUTPUT

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()

OUTPUT

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()

OUTPUT

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)

OUTPUT

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")

OUTPUT

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")

OUTPUT

R ggplot2 Boxplot 12

Change R ggplot2 boxplot Theme

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

  • 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()

OUTPUT

R ggplot2 Boxplot 13

Placed Under: R Programming

  • R Software Download
  • Install R Packages
  • Install R Software
  • Download & Install R Studio
  • R Arithmetic Operators
  • R Comparison Operators
  • R Logical Operators
  • R If Statement
  • R If Else Statement
  • R Else If Statement
  • R Nested If Else
  • R Switch Statement
  • R Break Statement
  • R Next Statement
  • R For Loop
  • R While Loop
  • R Repeat
  • R Vector
  • R Data Frame
  • R List
  • R Arrays
  • R Matrix
  • SQL Server R Services
  • R Read CSV Function
  • R Read table Function
  • R Barplot
  • R Boxplot
  • R Histogram
  • R Pie Chart
  • R Stacked Barplot
  • R Stem and Leaf Plot
  • R Mosaic Plot
  • R ggplot2 Boxplot
  • R ggplot2 Density Plot
  • R ggplot2 Dot Plot
  • R ggplot2 Histogram
  • R ggplot2 Jitter
  • R ggplot2 Line Plot
  • R ggplot2 Scatter Plot
  • R ggplot2 Violin Plot
  • Save R ggplot using ggsave
  • R Lattice Bar chart
  • R Lattice Scatter Plot
  • R Lattice Histogram
  • R Functions
  • R Recursive Functions
  • R abs Function
  • R sqrt Function
  • R ceiling Function
  • R floor Function
  • R round Function
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy