[英]Is there a way of mixing boxplots for different subgroups from the same dataset?
I have this dataframe我有这个 dataframe
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
Imagine that I want to create on the same plot, different boxplots, grouping B, C and D columns .想象一下,我想在相同的 plot、不同的箱线图、分组 B、C 和 D 列上创建。
So a total of 6 boxplots (John, Anne, cheap, expensive, USA and Mexico) .所以总共有 6 个箱线图(约翰、安妮、便宜、昂贵、美国和墨西哥) 。 Taking into account the the group A values , of course.
当然,考虑到 A 组的价值观。
The problem here is that each subgroup have a different total of samples to plot , which makes me very confused.这里的问题是每个子组对 plot 的样本总数不同,这让我很困惑。
This problem is a matter of reshaping the data to long format .这个问题是将数据重新整形为长格式的问题。 Then it becomes a standard boxplot.
然后它变成一个标准的箱线图。
library(ggplot2)
library(magrittr)
library(tidyr)
dataframe %>%
pivot_longer(-A) %>%
ggplot(aes(value, A)) +
geom_boxplot()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.