有没有一种方法可以混合来自同一数据集的不同子组的箱线图?

Is there a way of mixing boxplots for different subgroups from the same dataset?

我有这个数据框

A <- c(100,101,102,98,97,93,96)
B <- c("John","Anne","John", "Anne","John","Anne","John")
C <- c("cheap", "cheap", "expensive", "cheap", "expensive", "cheap", "expensive")
D <- c("USA", "Mexico", "Mexico","USA", "Mexico","USA", "Mexico")

dataframe <- data.frame(A, B, C, D)

   A    B         C      D
1 100 John     cheap    USA
2 101 Anne     cheap Mexico
3 102 John expensive Mexico
4  98 Anne     cheap    USA
5  97 John expensive Mexico
6  93 Anne     cheap    USA
7  96 John expensive Mexico

假设我想在同一个图上创建不同的箱线图,分组 B、C 和 D 列

所以 总共有 6 个箱线图(约翰、安妮、便宜的、昂贵的、美国和墨西哥)考虑到A组的价值观,当然。

这里的问题是每个子组要绘制的样本总数不同,这让我很困惑。

这个问题是 reshaping the data to long format 的问题。然后就变成标准箱线图了。

library(ggplot2)
library(magrittr)
library(tidyr)

dataframe %>%
  pivot_longer(-A) %>%
  ggplot(aes(value, A)) +
  geom_boxplot()