因此,我一直試圖做幾個小時的「是/否」計數盒圖。dplyr:跨多個變量的單個列的分類計數
我的數據集看起來像這樣
> 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
我想兩個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)
你能告訴我什麼,我做錯了。
'y = count'請註明我沒有在代碼中看到count變量 – Al14
,其中我陳述了「mutate(count = n)」,我沒有創建一個新的變量「count」? – Locean