Show percent % instead of counts in charts of categorical variables

前端 未结 8 2402
梦如初夏
梦如初夏 2020-11-22 06:06

I\'m plotting a categorical variable and instead of showing the counts for each category value.

I\'m looking for a way to get ggplot to display the perc

8条回答
  •  情深已故
    2020-11-22 06:33

    Here is a workaround for faceted data. (The accepted answer by @Andrew does not work in this case.) The idea is to calculate the percentage value using dplyr and then to use geom_col to create the plot.

    library(ggplot2)
    library(scales)
    library(magrittr)
    library(dplyr)
    
    binwidth <- 30
    
    mtcars.stats <- mtcars %>%
      group_by(cyl) %>%
      mutate(bin = cut(hp, breaks=seq(0,400, binwidth), 
                   labels= seq(0+binwidth,400, binwidth)-(binwidth/2)),
             n = n()) %>%
      group_by(cyl, bin) %>%
      summarise(p = n()/n[1]) %>%
      ungroup() %>%
      mutate(bin = as.numeric(as.character(bin)))
    
    ggplot(mtcars.stats, aes(x = bin, y= p)) +  
      geom_col() + 
      scale_y_continuous(labels = percent) +
      facet_grid(cyl~.)
    

    This is the plot:

提交回复
热议问题