【问题标题】:Box plot with numeric and categorical variables带有数字和分类变量的箱线图
【发布时间】: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 轴填充其他类别,我希望它填充每个客户端的分布。

  1. 这可能吗?

  2. 如何在 R 中做到这一点?

  3. 还有其他可视化方法可以做到这一点吗?

【问题讨论】:

  • “但不是 x 轴被其他类别填充,我希望它被填充每个客户端的分布。”你是什么意思?

标签: r statistics data-visualization data-science data-analysis


【解决方案1】:

您是否正在寻找这样的东西?

prueba2 <- prueba %>% 
  pivot_longer(cols = starts_with("client"), names_to = "client")

  ggplot(data = prueba2, aes(x = type, 
                             y = value, 
                             fill = client)) +
  geom_boxplot() 

如果是这样,首先将所有 client# 列放入一个“client”列,并将相应的值放入另一个带有 pivot_longer 的“value”列(来自 tidyr 包,已经在 tidyverse 中)。然后让 ggplot 完成剩下的工作 - 我们只需要告诉它:x 轴是“类型”,y 轴是“值”,“客户端”是填充颜色。

【讨论】:

  • 我正在处理的数据集在变量名称中没有赞助人,有必要吗?无论如何,我必须阅读文档以获得完整的理解
  • 哦,那是我引用 3 列的懒惰方式:“client1”、“client2”和“client3”。使用 # 表示数字。
【解决方案2】:

我不确定我是否理解正确,但如果您想要每个级别的客户端而不是每个级别的 cat,那么您必须将所有内容转换为长格式:

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)

library(reshape2)

prueba_long <- melt(prueba,  id.vars = c('type', 'cat'))

ggplot(prueba_long, aes(x = type, y = value, colour = variable)) +
  geom_boxplot()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    相关资源
    最近更新 更多