四个不同的箱线图共享一个 y 轴
Share one y axis for four different boxplots
我使用 iris 数据集,目的是让 4 个箱线图彼此相邻,并使它们共享一个从 0 到 8 的 y 轴
par(mfrow=c(1,4))
boxplot(iris$Sepal.Length, width = 3)
boxplot(iris$Sepal.Width, width = 3)
boxplot(iris$Petal.Length, width = 3)
boxplot(iris$Petal.Width, width = 3)
我现在有 4 个相邻的地块,但它们都有自己的 y 轴,适合自己的最小值和最大值。
如何让它们共享一个 y 轴?
我还想要一个 x 轴,我可以在其中用“Sepal.Length”、“Sepal.Width”等标记它们。
如果可能的话,没有 ggplot 的解决方案,谢谢。
三个选项:
基本图形
在绘图前确定 y 范围。为此,有两个选项,请从下面的 ylim=
中选择一个:
### if you have no outliers or will not plot them
ylim <- range(boxplot.stats(as.matrix(iris[,-5]))$stats)
### if you have outliers and/or want to leave room for them, then
ylim <- range(as.matrix(iris[,-5]))
在这种情况下,总体范围不会改变,但 Sepal.Width
确实在图中包含一些异常值。
从这里开始,用 ylim=ylim
:
绘制它们
par(mfrow = c(1, 4))
boxplot(iris$Sepal.Length, width = 3, ylim = ylim)
boxplot(iris$Sepal.Width, width = 3, ylim = ylim)
boxplot(iris$Petal.Length, width = 3, ylim = ylim)
boxplot(iris$Petal.Width, width = 3, ylim = ylim)
基本图形,取2
整形后分组。
irislong <- reshape2::melt(iris, "Species", variable.name = "Var")
head(irislong)
# Species Var value
# 1 setosa Sepal.Length 5.1
# 2 setosa Sepal.Length 4.9
# 3 setosa Sepal.Length 4.7
# 4 setosa Sepal.Length 4.6
# 5 setosa Sepal.Length 5.0
# 6 setosa Sepal.Length 5.4
boxplot(value ~ Var, data = irislong)
(一个案例还使用 tidyr::pivot_longer
对其进行重塑,我将在下面的 ggplot2 部分中进行演示。)
ggplot2
(即使您要求使用非 ggplot2 也已提供,因为它有时会提供更多的功能,并且有些人认为它看起来更好,当然是主观的。)
ggplot(tidyr::pivot_longer(iris, -Species), aes(value)) +
geom_boxplot() +
facet_grid(name ~ .)
如果您更喜欢与基地地块同方向,那么
ggplot(tidyr::pivot_longer(iris, -Species), aes(value)) +
geom_boxplot() +
facet_grid(. ~ name) +
coord_flip()
一般来说,使用公式方法(在第二个基本图形图中)或分面法(在 ggplot2 中)允许您通过不止一个变量对箱线图进行分组。
我使用 iris 数据集,目的是让 4 个箱线图彼此相邻,并使它们共享一个从 0 到 8 的 y 轴
par(mfrow=c(1,4))
boxplot(iris$Sepal.Length, width = 3)
boxplot(iris$Sepal.Width, width = 3)
boxplot(iris$Petal.Length, width = 3)
boxplot(iris$Petal.Width, width = 3)
我现在有 4 个相邻的地块,但它们都有自己的 y 轴,适合自己的最小值和最大值。
如何让它们共享一个 y 轴? 我还想要一个 x 轴,我可以在其中用“Sepal.Length”、“Sepal.Width”等标记它们。
如果可能的话,没有 ggplot 的解决方案,谢谢。
三个选项:
基本图形
在绘图前确定 y 范围。为此,有两个选项,请从下面的 ylim=
中选择一个:
### if you have no outliers or will not plot them
ylim <- range(boxplot.stats(as.matrix(iris[,-5]))$stats)
### if you have outliers and/or want to leave room for them, then
ylim <- range(as.matrix(iris[,-5]))
在这种情况下,总体范围不会改变,但 Sepal.Width
确实在图中包含一些异常值。
从这里开始,用 ylim=ylim
:
par(mfrow = c(1, 4))
boxplot(iris$Sepal.Length, width = 3, ylim = ylim)
boxplot(iris$Sepal.Width, width = 3, ylim = ylim)
boxplot(iris$Petal.Length, width = 3, ylim = ylim)
boxplot(iris$Petal.Width, width = 3, ylim = ylim)
基本图形,取2
整形后分组。
irislong <- reshape2::melt(iris, "Species", variable.name = "Var")
head(irislong)
# Species Var value
# 1 setosa Sepal.Length 5.1
# 2 setosa Sepal.Length 4.9
# 3 setosa Sepal.Length 4.7
# 4 setosa Sepal.Length 4.6
# 5 setosa Sepal.Length 5.0
# 6 setosa Sepal.Length 5.4
boxplot(value ~ Var, data = irislong)
(一个案例还使用 tidyr::pivot_longer
对其进行重塑,我将在下面的 ggplot2 部分中进行演示。)
ggplot2
(即使您要求使用非 ggplot2 也已提供,因为它有时会提供更多的功能,并且有些人认为它看起来更好,当然是主观的。)
ggplot(tidyr::pivot_longer(iris, -Species), aes(value)) +
geom_boxplot() +
facet_grid(name ~ .)
如果您更喜欢与基地地块同方向,那么
ggplot(tidyr::pivot_longer(iris, -Species), aes(value)) +
geom_boxplot() +
facet_grid(. ~ name) +
coord_flip()
一般来说,使用公式方法(在第二个基本图形图中)或分面法(在 ggplot2 中)允许您通过不止一个变量对箱线图进行分组。