How to draw multiple boxplots together in R using ggplot2? | Iris data | StatswithR | Arnab Hazra
ฝัง
- เผยแพร่เมื่อ 15 ต.ค. 2024
- Here we explain how to generate a presentation/publication-quality multiple 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 #multiple #boxplot #rstudio #rcoding
library(datasets)
str(iris)
sl.setosa = iris$Sepal.Length[iris$Species == "setosa"]
sl.versicolor = iris$Sepal.Length[iris$Species == "versicolor"]
sl.virginica = iris$Sepal.Length[iris$Species == "virginica"]
head(sl.setosa)
head(sl.versicolor)
head(sl.virginica)
#-------------
first ggplot
p = ggplot() +
geom_boxplot(mapping = aes(x = "setosa", y = sl.setosa)) +
geom_boxplot(mapping = aes(x = "versicolor", y = sl.versicolor)) +
geom_boxplot(mapping = aes(x = "virginica", y = sl.virginica))
ggsave(p, filename = "multboxplot_ggplot1.pdf", height = 6, width = 6)
#-------------
second ggplot
p0 = ggplot() + stat_boxplot(geom = "errorbar", mapping = aes(x = "setosa", y = sl.setosa)) +
stat_boxplot(geom = "errorbar", mapping = aes(x = "versicolor", y = sl.versicolor)) +
stat_boxplot(geom = "errorbar", mapping = aes(x = "virginica", y = sl.virginica))
p = p0 +
geom_boxplot(mapping = aes(x = "setosa", y = sl.setosa)) +
geom_boxplot(mapping = aes(x = "versicolor", y = sl.versicolor)) +
geom_boxplot(mapping = aes(x = "virginica", y = sl.virginica))
ggsave(p, filename = "multboxplot_ggplot2.pdf", height = 6, width = 6)
#-------------
third ggplot
p1 = p0 +
geom_boxplot(mapping = aes(x = "setosa", y = sl.setosa, fill = "pink")) +
geom_boxplot(mapping = aes(x = "versicolor", y = sl.versicolor, fill = "lightblue")) +
geom_boxplot(mapping = aes(x = "virginica", y = sl.virginica, fill = "lightgreen")) +
scale_fill_identity(breaks = c("pink", "lightblue", "lightgreen"))
ggsave(p1, filename = "multboxplot_ggplot3.pdf", height = 6, width = 6)
#-------------
fourth ggplot
p = p1 + xlab("") + ylab("Sepal length")
ggsave(p, filename = "multboxplot_ggplot4.pdf", height = 6, width = 6)
#-------------
fifth ggplot
p = p1 + xlab("") + ylab("Sepal length") +
ggtitle("Sepal length variation across Iris species") +
theme(plot.title = element_text(hjust = 0.5))
ggsave(p, filename = "multboxplot_ggplot5.pdf", height = 6, width = 6)
#-------------
sixth ggplot
p = p1 + xlab("") + ylab("Sepal length") +
ggtitle("Sepal length variation across Iris species") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(axis.text=element_text(size=18),
axis.title=element_text(size=18),
plot.title = element_text(size=18))
ggsave(p, filename = "multboxplot_ggplot6.pdf", height = 6, width = 6)
Another video done from the playlist...thank you
Thank you! Keep watching...
Thank you so much! This was very helpful :)
I want to draw multiple box plot. But how can i arrange my data in excel sheet ???
could you tell me what song it is at 3:42?