【发布时间】:2020-07-22 11:45:04
【问题描述】:
我想创建一个箱线图,以可视化具有相同比例的多个数值变量相对于一个分类变量的分布,以便查看针对某个特定因子水平的不同度量之间的行为。
例如,我想查看 3 位客户订购的货物数量(以千美元计)与产品类型的差异有多大。以这个示例数据为例:
prueba <- data.frame("client1" = truncnorm::rtruncnorm(n = 60, a = 1, b = 9.8, mean = 6.5, sd = 1),
"client2" = truncnorm::rtruncnorm(n = 60, a = 1, b = 9.8, mean = 6.9, sd = 2),
"client3" = truncnorm::rtruncnorm(n = 60, a = 1, b = 9.8, mean = 5, sd = 3),
"type" = as.factor(sample(LETTERS[1:3], 60, replace = T, prob = c(0.4,0.35,0.25))),
"cat" = as.factor(sample(LETTERS[20:22], 60, replace = T, prob = c(0.5, 0.1,0.4))))
prueba[,1:3] <- round(prueba[,1:3], 1)
head(prueba)
# client1 client2 client3 type cat
#1 6.3 7.2 7.0 B T
#2 7.2 6.5 3.5 C T
#3 8.0 6.4 8.0 A V
#4 8.0 7.4 7.0 A V
#5 7.5 7.6 2.5 B V
#6 7.0 9.0 3.7 A V
使用 ggplot 我可以做到这一点:
library(tidyverse)
library(patchwork)
uno <- prueba %>% ggplot(aes(x = type,
y = client1)) +
geom_boxplot()+scale_y_continuous(limits = c(0,10))
dos <- prueba %>% ggplot(aes(x = type,
y = client2)) +
geom_boxplot()
tres <- prueba %>% ggplot(aes(x = type,
y = client3)) +
geom_boxplot()
uno+dos+tres+plot_layout(byrow = F)
但不是 x 轴填充其他类别,我希望它填充每个客户端的分布。
这可能吗?
如何在 R 中做到这一点?
还有其他可视化方法可以做到这一点吗?
【问题讨论】:
-
“但不是 x 轴被其他类别填充,我希望它被填充每个客户端的分布。”你是什么意思?
标签: r statistics data-visualization data-science data-analysis