Create Boxplot of Multiple Column Values using ggplot2 in R
Last Updated :
28 Nov, 2021
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:
- data - The data set to melt
- id.vars - Id variables.
- measure.vars - Measured variables.
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:
- data - The dataframe to be used as input in the ggplot method
- mapping - Default list of aesthetic mappings to use for plot.
- Additional components can be added to the ggplot object. The geom_boxplot() method is used to draw a boxplot() in R.
Syntax:
geom_boxplot( mapping = aes(x , y , color ))
Example:
R
# 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 :
R
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:
Similar Reads
Draw Multiple Boxplots in One Graph using R In this article, we are learning about Drawing Multiple BoxPlots in One Graph in the R programming language. Method 1: Multiple BoxPlot in One Graph Using Base R In this method to plot multiple boxplots in one graph, the user needs a box and whisker plot in base R can be plotted with the boxplot fun
3 min read
How to create a plot using ggplot2 with Multiple Lines in R ? In this article, we will discuss how to create a plot using ggplot2 with multiple lines in the R programming language. Method 1: Using geom_line() function In this approach to create a ggplot with multiple lines, the user need to first install and import the ggplot2 package in the R console and then
3 min read
Create a Scatter Plot with Multiple Groups using ggplot2 in R In this article, we will discuss how to create a scatter plot with multiple groups in R Programming Language. Geoms can be added to the plot to compute various graphical representations of the data in the plot (points, lines, bars). The geom_point() method is used to create scatter plots in R. The g
2 min read
Create Boxplot with respect to two factors using ggplot2 in R Boxplots are an effective way to visualize the distribution of data, especially when comparing multiple variables. The ggplot2 package in R allows us to easily create grouped boxplots, which are helpful when we have multiple subgroups within a variable.The function used for creating boxplots in ggpl
3 min read
Multiple Density Plots and Coloring by Variable with ggplot2 in R In this article, we will discuss how to make multiple density plots with coloring by variable in R Programming Language. To make multiple density plots with coloring by variable in R with ggplot2, we first make a data frame with values and categories. Then we draw the ggplot2 density plot using the
3 min read