如何限制饼图中的类别数量

时间:2013-04-20 14:35:14

标签: r ggplot2

下面的代码通过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")

1 个答案:

答案 0 :(得分:2)

有很多方法可以解决,但基本的想法是你需要

  1. 确定您要选择的AlertId。这涉及计算每个id的行数。
  2. ggplot发送一个data.frame(或data.table),其中只包含您要绘制的行。
  3. 以下是使用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")
    

    Pie Chart