How to create a nice boxplot in R using ggplot2? | StatswithR | Arnab Hazra

Опубликовано: 11 Июнь 2026
на канале: StatswithR
389
8

Here we explain how to generate a presentation/publication-quality boxplot in R/R-studio using ggplot2. The codes for the steps explained in the video are as follows. Copy and paste them into R, run them one-by-one and try to understand what each argument is doing.

#datascience #datavisualization #visualization #ggplot2 #tidyverse #boxplot #rstudio #rcoding #normal

Step 0a: If you don't know how to load data into R, simulate a vector X using

X = rnorm(1e4, mean = 175, sd = 10)

Step 0b: if you have not installed the package ggplot2, run

install.packages("ggplot2"); library(ggplot2)

Step 1: ggplot() + geom_boxplot(mapping = aes("", y = X))

Step 2: ggplot() + geom_boxplot(mapping = aes("", X), fill = "gray")

Step 3: ggplot() + geom_boxplot(mapping = aes("", X), fill = "gray") +
ggtitle("Boxplot of heights")

Step 4: ggplot() + geom_boxplot(mapping = aes("", X), fill = "gray") +
ggtitle("Boxplot of heights") +
theme(plot.title = element_text(hjust = 0.5))

Step 5: ggplot() + geom_boxplot(mapping = aes("", X), fill = "gray") +
ggtitle("Boxplot of heights") +
theme(plot.title = element_text(hjust = 0.5)) +
xlab("") + ylab("Height (cm)")

Step 6: ggplot() + geom_boxplot(mapping = aes("", X), fill = "gray") +
ggtitle("Boxplot of heights") +
theme(plot.title = element_text(hjust = 0.5)) +
xlab("") + ylab("Height (cm)") +
theme(axis.text=element_text(size=20),
axis.title=element_text(size=20),
plot.title = element_text(size=25))