我正在 ggplot2 中制作饼图。我有一个数据集,如下所示:
category<- c("a", "b", "c", "d", "e")
count<- c(1,1,4,5,1)
df<- cbind(category, count)
创建一个输出,如:|category|count| |--------|-----| |一个 |1 | |b |1 | |c |4 | |d |5 | |e |1 |
我目前的饼图代码:
ggplot(Distincts, aes(y=count, x="", fill= category )) + geom_bar(stat="identity") +
coord_polar("y", start = 0)
我想创建一个饼图,其中包含每个相应类别的计数。但是,我想将计数小于一个数字(即 <2)的所有类别分组为一个名为“唯一”的类别 - 是否有任何关于如何执行此操作的输入?我希望饼图看起来像:
再次感谢!
也许您正在寻找forcats::fct_lump_min
哪个允许您根据频率将类别归为“其他”类别。集总后,您可以使用 count 来计算集总类别的聚合计数:
category<- c("a", "b", "c", "d", "e")
count<- c(1,1,4,5,1)
df<- data.frame(category, count)
library(ggplot2)
library(forcats)
library(dplyr)
df <- df %>%
mutate(category = forcats::fct_lump_min(category, 2, count, other_level = "Unique")) %>%
count(category, wt = count, name = "count")
ggplot(df, aes(y=count, x="", fill= category)) +
geom_bar(stat="identity") +
coord_polar("y", start = 0)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句