Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?enter image description here


ggplot(adatok, aes(x=as.factor(region), fill=as.factor(savings)))+
  geom_bar(aes( y=..count../tapply(..count.., ..x.. ,sum)[..x..]), position="dodge" ) +
  geom_text(aes(label=scales::percent(..count../tapply(..count.., ..x.. ,sum)[..x..]),
    y=..count../tapply(..count.., ..x.. ,sum)[..x..]  ),
            stat="count", position=position_dodge(0.9), vjust=-0.5)+
  scale_y_continuous(labels = scales::percent)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’ll use mtcars (with its cyl and gear) to demonstrate two options.

Starting point:

library(ggplot)
data("mtcars")
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=scales::percent(..count../tapply(..count.., ..x.. ,sum)[..x..]),
    y=..count../tapply(..count.., ..x.. ,sum)[..x..]  ),
            stat="count", position=position_dodge(0.9), vjust=-0.5)+
  scale_y_continuous(labels = scales::percent)

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading