Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent scales::percent from adding decimal

Tags:

This started happening a few days ago, that scales::percent would add a decimal place in its labels, and I can't seem to disable this decimal to display integer values on y-axis. enter image description here

library(dplyr)
library(ggplot2)

mtcars %>% 
  count(cyl) %>% 
  mutate(prop = n / sum(n)) %>% 
  ggplot(aes(x = cyl, y = prop)) + 
  geom_point() + 
  scale_y_continuous(labels = scales::percent)
like image 599
Joe Avatar asked Oct 30 '18 20:10

Joe


People also ask

Can percentages have decimal points?

The % is a percent sign, meaning divided by 100. So 25% means 25/100, or 1/4. To convert a percentage to a decimal, divide by 100. So 25% is 25/100, or 0.25.

How do I change a scale to a percentage in R?

Now use scales: : percent to convert the y-axis labels into a percentage. This will scale the y-axis data from decimal to percentage. It simply multiplies the value by 100. The scaling factor is 100.


1 Answers

Perhaps not a direct answer to your question, but I have used scales::percent_format and its accuracy argument ("Number to round to") in similar settings.

mtcars %>% 
    count(cyl) %>% 
    mutate(prop = n / sum(n)) %>% 
    ggplot(aes(x = cyl, y = prop)) + 
    geom_point() + 
    scale_y_continuous(labels = scales::percent_format(accuracy = 5L))

enter image description here


I think the behaviour of percent was changed in scales 1.0.0. See NEWS and updates in code here.

like image 136
Henrik Avatar answered Oct 12 '22 17:10

Henrik