There are multiple methods you can use tu can make boxplots in R. Here, I will demonstrate how to make a boxplot using the ggpubr package.
We’ll use the “palmerpenguins” packages (https://allisonhorst.github.io/palmerpenguins/) to address this question. You’ll need to install the package with install.packages(“palmerpenguins”) if you have not done so before, call library(““palmerpenguins”), and load the data with data(penguins)
#install.packages("palmerpenguins")
library(palmerpenguins)
data(penguins)
First, you need to install and load the ggpubr package. I commented out the first line in this code chunk since I’ve already installed ggpubr.
#install.packages("ggpubr")
library(ggpubr)
## Loading required package: ggplot2
Next, I am using class() to check and make sure that the data set penguins is a dataframe, because the function ggboxplot() can only receive a datafame as its input. Since penguins is a dataframe, the only arguments I need to add are the specific columns I want to use to make the boxplot. Here, I am plotting body mass against flipper length.
class(penguins)
## [1] "tbl_df" "tbl" "data.frame"
ggboxplot(y = "flipper_length_mm",
x = "body_mass_g",
data = penguins)
## Warning: Removed 2 rows containing non-finite values (stat_boxplot).
For more information on this topic, see https://rpkgs.datanovia.com/ggpubr/reference/ggboxplot.html