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 :
The melt method in R is used to melt an R object, say a dataframe into a form suitable for easy casting.
melt(data, id.vars, measure.vars)
- 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())
- 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.
geom_boxplot( mapping = aes(x , y , color ))
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 :