Create a Horizontal boxplot using coord_flip()
In this method to create the horizontal boxplot in ggplot2, the user needs to install and import the ggplot2 package in the working R console, here the ggplot2 package is responsible to plot the boxplot and provide other functionalities of the package. Then the user needs to simply call the geom_boxplot() function which will be simply plotting the boxplot in the vertical way of the given data and with it the user also needs to call the coord_flip() which will be flipping the axis and the final boxplot will be resulting in the horizontal boxplot in the R programming language.
Syntax to import and install ggplot2 package in R language:
install.package("ggplot2") library("ggplot2")
coord_flip() function: This function is used to flip cartesian coordinates so that horizontal becomes vertical, and vertical, horizontal.
Syntax: coord_flip(…)
Parameters:
- …: Other arguments passed
Example: In this example, we will be using the geom_boxplot() function with the coord_flip() function from the ggplot2 package to plot the horizontal boxplot of the given data in the R programming language.
R
# Import ggplot2 package library (ggplot2) # Create Data gfg<- data.frame (x= c (6,8,9,6,4,7,6,3,4,9,6,3), grp= rep ( c ( 'A' , 'B' , 'C' , 'D' ), each=3)) # Create horizontal boxplot ggplot (gfg, aes (x=grp,y=x)) + geom_boxplot () + coord_flip () |
Output:
How to Create Horizontal Boxplots in R?
In this article, we will discuss how to create horizontal boxplots in the R programming language.