【问题标题】:Double box plots in ggplot2ggplot2中的双箱线图
【发布时间】:2018-02-14 12:33:05
【问题描述】:

我想使用跨越 x 轴和 y 轴的箱线图来描述两个变量的分布。

网站linked here 有一些很好的例子(如下),它有使用基础图的包 - boxplotdbl

我想知道ggplot2 中是否有类似的情节。以下图为例和iris数据,如何绘制Sepal.LengthSepal.Width的箱线图,并按Species着色?

我惊讶地发现以下代码很接近,但希望沿 x 轴延伸胡须而不是盒子。

library(ggplot2)
ggplot(iris) + 
  geom_boxplot(aes(x = Sepal.Length, y = Sepal.Width, fill = Species), alpha = 0.3) +
  theme_bw()

【问题讨论】:

  • 感谢您指出这一点。我编辑了问题,使其更具体到ggplot
  • 没问题,我也会将 CRAN 的链接添加到未来的读者。为什么不使用基图?
  • 你可以使用 bag-plots (2d box-plots),我也认为它看起来更好。值得一读这个答案stackoverflow.com/questions/29501282/…
  • 你的代码+ coord_flip()
  • @zx8754 我有许多不同的组 (~10),需要 ggplot 的附加功能,包括 facet_wrap,以帮助我的真实数据清晰。

标签: r ggplot2


【解决方案1】:

您可以计算每个箱线图所需的相关数字,并使用不同的几何构造二维箱线图。

第 1 步。分别绘制每个维度的箱线图:

plot.x <- ggplot(iris) + geom_boxplot(aes(Species, Sepal.Length))
plot.y <- ggplot(iris) + geom_boxplot(aes(Species, Sepal.Width))

grid.arrange(plot.x, plot.y, ncol=2) # visual verification of the boxplots

第 2 步。在 1 个数据框中获取计算得到的箱线图值(包括异常值):

plot.x <- layer_data(plot.x)[,1:6]
plot.y <- layer_data(plot.y)[,1:6]
colnames(plot.x) <- paste0("x.", gsub("y", "", colnames(plot.x)))
colnames(plot.y) <- paste0("y.", gsub("y", "", colnames(plot.y)))
df <- cbind(plot.x, plot.y); rm(plot.x, plot.y)
df$category <- sort(unique(iris$Species))

> df
  x.min x.lower x.middle x.upper x.max x.outliers y.min y.lower
1   4.3   4.800      5.0     5.2   5.8              2.9   3.200
2   4.9   5.600      5.9     6.3   7.0              2.0   2.525
3   5.6   6.225      6.5     6.9   7.9        4.9   2.5   2.800
  y.middle y.upper y.max    y.outliers   category
1      3.4   3.675   4.2      4.4, 2.3     setosa
2      2.8   3.000   3.4               versicolor
3      3.0   3.175   3.6 3.8, 2.2, 3.8  virginica

第 3 步。为异常值创建单独的数据框:

df.outliers <- df %>%
  select(category, x.middle, x.outliers, y.middle, y.outliers) %>%
  data.table::data.table()
df.outliers <- df.outliers[, list(x.outliers = unlist(x.outliers), y.outliers = unlist(y.outliers)), 
                           by = list(category, x.middle, y.middle)]

> df.outliers
    category x.middle y.middle x.outliers y.outliers
1:    setosa      5.0      3.4         NA        4.4
2:    setosa      5.0      3.4         NA        2.3
3: virginica      6.5      3.0        4.9        3.8
4: virginica      6.5      3.0        4.9        2.2
5: virginica      6.5      3.0        4.9        3.8

第 4 步。将所有内容放在一个情节中:

ggplot(df, aes(fill = category, color = category)) +

  # 2D box defined by the Q1 & Q3 values in each dimension, with outline
  geom_rect(aes(xmin = x.lower, xmax = x.upper, ymin = y.lower, ymax = y.upper), alpha = 0.3) +
  geom_rect(aes(xmin = x.lower, xmax = x.upper, ymin = y.lower, ymax = y.upper), 
            color = "black", fill = NA) +

  # whiskers for x-axis dimension with ends
  geom_segment(aes(x = x.min, y = y.middle, xend = x.max, yend = y.middle)) + #whiskers
  geom_segment(aes(x = x.min, y = y.lower, xend = x.min, yend = y.upper)) + #lower end
  geom_segment(aes(x = x.max, y = y.lower, xend = x.max, yend = y.upper)) + #upper end

  # whiskers for y-axis dimension with ends
  geom_segment(aes(x = x.middle, y = y.min, xend = x.middle, yend = y.max)) + #whiskers
  geom_segment(aes(x = x.lower, y = y.min, xend = x.upper, yend = y.min)) + #lower end
  geom_segment(aes(x = x.lower, y = y.max, xend = x.upper, yend = y.max)) + #upper end

  # outliers
  geom_point(data = df.outliers, aes(x = x.outliers, y = y.middle), size = 3, shape = 1) + # x-direction
  geom_point(data = df.outliers, aes(x = x.middle, y = y.outliers), size = 3, shape = 1) + # y-direction

  xlab("Sepal.Length") + ylab("Sepal.Width") +
  coord_cartesian(xlim = c(4, 8), ylim = c(2, 4.5)) +
  theme_classic()

我们可以通过将其与原始数据集在相同二维上的散点图进行比较,直观地验证 2D 箱线图是否合理:

# p refers to 2D boxplot from previous step
p + geom_point(data = iris, 
               aes(x = Sepal.Length, y = Sepal.Width, group = Species, color = Species),
               inherit.aes = F, alpha = 0.5)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    • 2016-11-19
    相关资源
    最近更新 更多