Show percent % instead of counts in charts of categorical variables

前端 未结 8 2375
梦如初夏
梦如初夏 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:

    0 讨论(0)
  • 2020-11-22 06:36

    If you want percentages on the y-axis and labeled on the bars:

    library(ggplot2)
    library(scales)
    ggplot(mtcars, aes(x = as.factor(am))) +
      geom_bar(aes(y = (..count..)/sum(..count..))) +
      geom_text(aes(y = ((..count..)/sum(..count..)), label = scales::percent((..count..)/sum(..count..))), stat = "count", vjust = -0.25) +
      scale_y_continuous(labels = percent) +
      labs(title = "Manual vs. Automatic Frequency", y = "Percent", x = "Automatic Transmission")
    

    When adding the bar labels, you may wish to omit the y-axis for a cleaner chart, by adding to the end:

      theme(
            axis.text.y=element_blank(), axis.ticks=element_blank(),
            axis.title.y=element_blank()
      )
    

    0 讨论(0)
提交回复
热议问题