R - Aggregate Percentage for Stacked Bar Charts using ggplot2

后端 未结 1 1244
北荒
北荒 2021-01-07 14:17

I have some data that looks like the below. I\'m aiming to generate stacked bar charts for them, but I need the values to be shown as percentages. I\'ve managed to get as fa

相关标签:
1条回答
  • 2021-01-07 14:32

    You can calculate proportion using your melt data. Then, you can draw a figure. Here, you can calculate proportion for each level of x using group_by in the dplyr package. You have other options as well. If you wanna read the mutate line, it is like "For each level of x, I want to get percent." In order to to remove the grouped variable, which is x, I added ungroup() in the end.

    library(dplyr)
    library(ggplot2)
    
    ### foo is your melt data
    ana <- mutate(group_by(foo, x), percent = value / sum(value) * 100) %>%
           ungroup()
    
    ### Plot once
    bob <- ggplot(data = ana, aes(x = x, y = percent, fill = variable)) +
           geom_bar(stat = "identity") +
           labs(y = "Percentage (%)")
    
    ### Get ggplot data
    caroline <- ggplot_build(bob)$data[[1]]
    
    ### Create values for text positions
    caroline$position = caroline$ymax + 1
    
    ### round up numbers and convert to character. Get unique values
    foo <- unique(as.character(round(ana$percent, digits = 2)))
    
    ### Create a column for text
    caroline$label <- paste(foo,"%", sep = "")
    
    ### Plot again
    bob + annotate(x = caroline$x, y = caroline$position,
                   label = caroline$label, geom = "text", size=3) 
    

    enter image description here

    DATA

    foo <-structure(list(x = c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), variable = structure(c(1L, 
    1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L), .Label = c("A", "B", "C"), class = "factor"), 
    value = c(9L, 9L, 9L, 14L, 14L, 14L, 28L, 28L, 28L)), .Names = c("x", 
    "variable", "value"), class = "data.frame", row.names = c("1", 
    "2", "3", "4", "5", "6", "7", "8", "9"))
    
    0 讨论(0)
提交回复
热议问题