ggplot2 pie and donut chart on same plot

前端 未结 6 1778
花落未央
花落未央 2020-11-28 03:35

I am trying to replicate this \"enter with R ggplot. I have exactly the same data:

<         


        
6条回答
  •  日久生厌
    2020-11-28 04:03

    you can get something similar using the package ggsunburst

    # using your data without "ymax" and "ymin"
    browsers <- structure(list(browser = structure(c(3L, 3L, 3L, 3L, 2L, 2L,
    2L, 1L, 5L, 5L, 4L), .Label = c("Chrome", "Firefox", "MSIE",
    "Opera", "Safari"), class = "factor"), version = structure(c(5L,
    6L, 7L, 8L, 2L, 3L, 4L, 1L, 10L, 11L, 9L), .Label = c("Chrome 10.0",
    "Firefox 3.5", "Firefox 3.6", "Firefox 4.0", "MSIE 6.0", "MSIE 7.0",
    "MSIE 8.0", "MSIE 9.0", "Opera 11.x", "Safari 4.0", "Safari 5.0"
    ), class = "factor"), share = c(10.85, 7.35, 33.06, 2.81, 1.58,
    13.12, 5.43, 9.91, 1.42, 4.55, 1.65)), .Names = c("parent", "node", "size")
    , row.names = c(NA, -11L), class = "data.frame")
    
    # add column browser to be used for colouring
    browsers$browser <- browsers$parent
    
    # write data.frame into csv file
    write.table(browsers, file = 'browsers.csv', row.names = F, sep = ",")
    
    # install ggsunburst
    if (!require("ggplot2")) install.packages("ggplot2")
    if (!require("rPython")) install.packages("rPython")
    install.packages("http://genome.crg.es/~didac/ggsunburst/ggsunburst_0.0.9.tar.gz", repos=NULL, type="source")
    library(ggsunburst)
    
    # generate data structure
    sb <- sunburst_data('browsers.csv', type = 'node_parent', sep = ",", node_attributes = c("browser","size"))
    
    # add name as browser attribute for colouring to internal nodes
    sb$rects[!sb$rects$leaf,]$browser <- sb$rects[!sb$rects$leaf,]$name
    
    # plot adding geom_text layer for showing the "size" value
    p <- sunburst(sb, rects.fill.aes = "browser", node_labels = T, node_labels.min = 15)
    p + geom_text(data = sb$leaf_labels,
        aes(x=x, y=0.1, label=paste(size,"%"), angle=angle, hjust=hjust), size = 2)
    

提交回复
热议问题