Home > Mobile >  How do I get rid of the percentages in this bar_plot?
How do I get rid of the percentages in this bar_plot?

Time:04-28

I wanted to have a dodge barplot in ggplot2 that show whether a person has savings or not and all this by another variable, region. Since there are missing data I want to include them as another category in the plot and in that way every region would have three bars: one for having, one for not having and one for missing data. Now I made a plot that shows exactly what I want for which I picked up some code on the internet because I really couldn't do it correctly. The only problem is that now it shows the percentages on the tops of the bars which feature I don't want. How do I get rid of it? Or how can I round it up at least?original plot

  1. If you don't want the literal percent signs, remove scales::percent (and see numbers like 0.72727272727272727) or round(100*., 1) it:

    ggplot(mtcars, aes(x=as.factor(cyl), fill=as.factor(gear))) 
      geom_bar(aes( y=..count../tapply(..count.., ..x.. ,sum)[..x..]), position="dodge" )  
      geom_text(aes(label=round(100*(..count../tapply(..count.., ..x.. ,sum)[..x..]), 1),
        y=..count../tapply(..count.., ..x.. ,sum)[..x..]  ),
                stat="count", position=position_dodge(0.9), vjust=-0.5) 
      scale_y_continuous(labels = scales::percent)
    

    no percent sign

  2. If you don't want the numbers at all, remove the geom_text(.) completely.

    ggplot(mtcars, aes(x=as.factor(cyl), fill=as.factor(gear))) 
      geom_bar(aes( y=..count../tapply(..count.., ..x.. ,sum)[..x..]), position="dodge" )  
      scale_y_continuous(labels = scales::percent)
    

    no numbers at all

  • Related