代码之家  ›  专栏  ›  技术社区  ›  Matt Parker

牛眼图

  •  3
  • Matt Parker  · 技术社区  · 16 年前

    ggplot2 .

    here

    数据如下:

        Zoo Animals Bears Polar Bears
    1 Omaha      50    10           3
    

    dput :

    structure(list(Zoo = "Omaha", Animals = "50", Bears = "10", `Polar Bears` = "3"), .Names = c("Zoo", 
    "Animals", "Bears", "Polar Bears"), row.names = c(NA, -1L), class = "data.frame")
    

    3 回复  |  直到 13 年前
        1
  •  1
  •   Ben Bolker    13 年前

    当我们在等待更好的答案时,我想我应该发布你提到的(次优)解决方案。 dat

    d <- data.frame(animal=factor(sapply(list(dat[2:length(dat)]),
                    function(x) rep(names(x),x))))
    cxc <- ggplot(d, aes(x = animal)) +  geom_bar(width = 1, colour = "black") 
    cxc + coord_polar() 
    
        2
  •  3
  •   Christopher DuBois    16 年前

    您可以使用 inverse.rle 为了重新创建数据,

    dd = list(lengths = unlist(dat[-1]), values = names(dat)[-1])
    class(dd) = "rle"
    inverse.rle(dd)
    

    l = plyr::dlply(dat, "Zoo", function(z)  
          structure(list(lengths = unlist(z[-1]), values = names(z)[-1]), class = "rle"))
    
    reshape2::melt(llply(l, inverse.rle))
    
        3
  •  2
  •   baptiste    13 年前

    stat="identity" geom_bar .

    它有助于让数据帧包含数值而不是字符串作为开头:

    dat <- data.frame(Zoo = "Omaha",
                   Animals = 50, Bears = 10, `Polar Bears` = 3)
    

    我们确实需要 reshape2::melt 要正确组织数据:

    library(reshape2)
    d3 <- melt(dat,id.var=1)
    

    现在创建绘图(与其他答案相同):

    library(ggplot2)
    ggplot(d3, aes(x = variable, y = value)) +
        geom_bar(width = 1, colour = "black",stat="identity") +
        coord_polar()
    
    推荐文章