dplyr:跨多个变量的单列分类计数

dplyr: categorical counts from a single column across multiple variables

所以我几个小时以来一直在尝试绘制 "yes/no" 计数的箱线图。

我的数据集是这样的

> stack
         Site Plot Treatment Meters Retrieved
2   Southern    18   Control  -5.00         y
3   Southern    18   Control   9.55         y
4   Southern    18   Control   4.70         y
5   Southern    27   Control  -5.00         y
6   Southern    27   Control  20.00         n
9   Southern    18   Control  -0.10         y
17  Southern    18   Control  20.00         y
23  Southern    31   Control 100.00         y
53  Southern    25        Mu   3.55         n
54  Southern    20        Mu   5.90         y
55  Southern    25        Mu  -0.10         y
56  Southern    29        Mu   9.55         y
58  Southern    25        Mu   4.70         y
60  Southern    20        Mu   2.90         y
61  Southern    24        Mu   5.90         n
62  Southern    24        Mu   3.55         y
63  Southern    20        Mu   3.55         y
65  Southern    24        Mu   0.55         y
66  Southern    29        Mu   8.90         y
68  Southern    25        Mu   8.90         y
69  Southern    29        Mu   0.55         y
70  Southern    24        Mu   1.70         y
72  Southern    29        Mu  -5.00         y
76  Southern    29        Mu   1.70         y
77  Southern    25        Mu   9.55         y
78  Southern    25        Mu  13.20         y
79  Southern    29        Mu   3.55         y
80  Southern    25        Mu  15.00         y
81  Southern    25        Mu  -5.00         n
84  Southern    24        Mu   8.90         y
85  Southern    20        Mu   6.55         y
86  Southern    29        Mu   2.90         y
92  Southern    24        Mu  -0.10         y
93  Southern    20        Mu 100.00         y

我想在为 "Treatment" 和 "Meters" 分组时获取变量 "Retrieved" 的 y(是)和 n(否)的计数。

所以它应该看起来像这样

 Treatment Meters        Yes   No
     Control  -5.00         2   0
     Control   9.55         1   2
     Control   4.70         1   1
     Control  20.00         0   2
         Mu   3.55         4   0
         Mu   5.90         0   1
         Mu  -0.10         2   2
         Mu   9.55         1   0

有了这些数据,我想用 x= 米、y= 计数和处理为网格或其他东西来做一个堆叠箱线图。 like this

这是我的代码,但它不起作用

plot_data <- stack %>% 
  count(Retrieved, Treatment, Meters) %>% 
  group_by(Treatment, Meters) %>% 
  mutate(count= n)

plot_data

ggplot(plot_data, aes(x = Meters, y = count, fill = Treatment)) + 
  geom_col(position = "fill") + 
  geom_label(aes(label = count(count)), position = "fill", color = "white", vjust = 1, show.legend = FALSE) +
  scale_y_continuous(labels = count) 

你能告诉我我做错了什么吗?

geom_bar 正是针对这种情况,您甚至不需要使用 group_bycount。 (来自文档:“geom_bar 使条形图的高度与每组中的案例数成正比。”)

这应该能满足您的需求:

ggplot(stack, aes(x = Meters, fill = Treatment)) +
  geom_bar(position = "stack")

但是,条形会很窄,因为 "Meters" 是连续的并且范围很大。您可以通过将其转换为一个因素来解决这个问题。一种方法是先执行此操作:

data <- data %>%
  mutate(Meters = as.factor(Meters))

如果您想以您提到的格式获取计数(除了创建图表),您可以这样做:

data %>%
  count(Treatment, Meters, Retrieved) %>%
  spread(Retrieved, n, fill = 0) %>% 
  rename(Yes = y, No = n)

count 为您完成 group_by,因此我不需要从您的代码中继承它。然后,spreadyn 创建单独的列。最后,我将这些列重命名为 YesNo