Open In App

Create Boxplot of Multiple Column Values using ggplot2 in R

In this article, we will discuss how to create a boxplot of multiple column values using ggplot2 in R Programming Language.

A dataframe can be created by containing values organized in the form of rows and columns. The values may belong to different data types. The reshape2 package is used to aggregate data by using the functions, melt and cast. The library can be installed and loaded into the working space using the following command : 



install.packages("reshape2")

The melt method in R is used to melt an R object, say a dataframe into a form suitable for easy casting.

Syntax:



melt(data, id.vars, measure.vars)

Parameters:

The ggplot method in this package is used to declare and define a ggplot object. It takes as input a dataframe and defines a set of aesthetic mappings to be intended for the plot. 

ggplot(data = NULL, mapping = aes())

Parameters: 

Syntax:

geom_boxplot( mapping = aes(x , y , color  ))

Example:




# importing required libraries
library(reshape2)
library(ggplot2)
  
# creating a dataframe
data_frame < - data.frame(col1=rep(1: 5, each=2),
                          col2=1: 10,
                          col3=11: 20,
                          col4=21: 30)
  
# creating the modified dataframe
data_mod < - melt(data_frame, id.vars='col1'
                  measure.vars=c('col2', 'col3', 'col4'))
# creating a plot
p < - ggplot(data_mod) +
geom_boxplot(aes(x=col1, y=value, color=variable))
  
# printing the plot
print(p)

Output

Example 2:

The following code snippet illustrates the plotting the values belonging to col2 and col3 on the x axis and its corresponding data items on the y axes : 




library(reshape2)
library(ggplot2)
  
# creating a dataframe
data_frame < - data.frame(col1=rep(1: 5, each=2),
                          col2=1: 10,
                          col3=11: 20,
                          col4=21: 30)
  
  
# creating the modified dataframe
data_mod < - melt(data_frame, id.vars='col1'
                  measure.vars=c('col2', 'col3'))
  
# creating a plot
p < - ggplot(data_mod) +
geom_boxplot(aes(x=col1, y=value, color=variable))
  
# printing the plot
print(p)

Output:


Article Tags :