ggplot2 pie and donut chart on same plot

前端 未结 6 1775
花落未央
花落未央 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:15

    I used floating.pie instead of ggplot2 to create two overlapping pie charts:

    library(plotrix)
    
    # browser 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"
        )
    
    # aggregate data for the browser pie chart
    browser_data <-
        aggregate(browsers$share,
                  by = list(browser = browsers$browser),
                  FUN = sum)
    
    # order version data by browser so it will line up with browser pie chart
    version_data <- browsers[order(browsers$browser), ]
    
    browser_colors <- c('#85EA72', '#3B3B3F', '#71ACE9', '#747AE6', '#F69852')
    
    # adjust these as desired (currently colors all versions the same as browser)
    version_colors <-
        c(
            '#85EA72',
            '#3B3B3F',
            '#3B3B3F',
            '#3B3B3F',
            '#71ACE9',
            '#71ACE9',
            '#71ACE9',
            '#71ACE9',
            '#747AE6',
            '#F69852',
            '#F69852'
        )
    
    # format labels to display version and % market share
    version_labels <- paste(version_data$version, ": ", version_data$share, "%", sep = "")
    
    # coordinates for the center of the chart
    center_x <- 0.5
    center_y <- 0.5
    
    plot.new()
    
    # draw version pie chart first
    version_chart <-
        floating.pie(
            xpos = center_x,
            ypos = center_y,
            x = version_data$share,
            radius = 0.35,
            border = "white",
            col = version_colors
        )
    
    # add labels for version pie chart
    pie.labels(
        x = center_x,
        y = center_y,
        angles = version_chart,
        labels = version_labels,
        radius = 0.38,
        bg = NULL,
        cex = 0.8,
        font = 2,
        col = "gray40"
    )
    
    # overlay browser pie chart
    browser_chart <-
        floating.pie(
            xpos = center_x,
            ypos = center_y,
            x = browser_data$x,
            radius = 0.25,
            border = "white",
            col = browser_colors
        )
    
    # add labels for browser pie chart
    pie.labels(
        x = center_x,
        y = center_y,
        angles = browser_chart,
        labels = browser_data$browser,
        radius = 0.125,
        bg = NULL,
        cex = 0.8,
        font = 2,
        col = "white"
    )
    

提交回复
热议问题