下面的代码通过AlertTypeId生成饼图。但是,AlertTypeId太多了,我想将饼图中的切片数量限制为X最常见的警报,其余的则进入“其他”类别。我怎么能用ggplot2做到这一点?
a = c(0, 0, 0, 1, 2, 3, 3, 3)
b = c(1, 1, 0, 0, 1, 1, 1, 1)
c = c(1, 4, 2, 2, 2, 1, 1, 3)
sa2 = data.frame(WeekOfYear = a, UrgentState = b, AlertTypeId = c, IsUrgent = b)
ggplot(sa2, aes(x = factor(1), fill = factor(AlertTypeId))) +
geom_bar(width = 1) +
coord_polar(theta = "y")
答案 0 :(得分:2)
有很多方法可以解决,但基本的想法是你需要
ggplot
发送一个data.frame(或data.table),其中只包含您要绘制的行。 以下是使用data.table
的示例:
library(data.table)
sa2.DT <- data.table(sa2, key="AlertTypeId")
# we can count the rows per id, by taking the length of any other column
ATid.Counts <- sa2.DT[, list(AT.count=length(UrgentState)), by=AlertTypeId]
# then order Id's by their counts. We will then take the `head( )`
# of this vector to identify the group being kept
ATid.Ordered <- ATid.Counts[order(AT.count, decreasing=TRUE), AlertTypeId]
ATid.Ordered
是按其频率计数排序的ID列表
取head(ATid.Ordered, n)
将获得最多n
个。{
由于我们将密钥设置为sa2.DT
作为这些ID,因此我们可以使用
有序列表(或其中的一部分)以data.table
# select only those rows which have an AlertTypeId in the top n many
dat <- sa2.DT[.(head(ATid.Ordered, n=3)) ] # <~~ note the dot in `.( )`
dat
是我们将在data.table
data.frame
(或ggplot
)
# use that selection to plot
ggplot(dat, aes(x = factor(1), fill = factor(AlertTypeId))) +
geom_bar(width = 1) +
coord_polar(theta = "y")