In this section, you will learn the ggplot2 codes for producing boxplots. While the syntax and default appearance may differ, these plots aim to compare distributions and identify outliers. If you need, you can add a few lines of code to make the base-R and ggplot2 graphs look the same. The choice of which plotting system to use is yours now.
This R tutorial describes how to create a box plot using R software and ggplot2 package.
The function geom_boxplot() is used. A simplified format is :
geom_boxplot(outlier.colour="black", outlier.shape=16, outlier.size=2, notch=FALSE)
ToothGrowth data sets are used :
# Convert the variable dose from a numeric to a factor variable ToothGrowth$dose <- as.factor(ToothGrowth$dose) head(ToothGrowth)
## len supp dose ## 1 4.2 VC 0.5 ## 2 11.5 VC 0.5 ## 3 7.3 VC 0.5 ## 4 5.8 VC 0.5 ## 5 6.4 VC 0.5 ## 6 10.0 VC 0.5
Make sure that the variable dose is converted as a factor variable using the above R script.
Source: STHDA, http://www.sthda.com/english/wiki/ggplot2-box-plot-quick-start-guide-r-software-and-data-visualization
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License.