Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R, ggplot: Decimals on y-axis [duplicate]

Tags:

r

ggplot2

I want to produce a bar plot, similar to this MWE:

library(tidyverse)
library(ggplot2)

mtcars %>% 
  mutate(mpg=mpg/1000) %>% 
  ggplot(aes(x=cyl, y=mpg)) +
  geom_bar(stat="identity") +
  scale_y_continuous(labels = scales::percent)

What I get is the following (keep in mind that it is nonsense, but serves illustration purposes): enter image description here Now, I want the decimals replaced from the percentages on the y-axis ("30%" instead of "30.0%"). What can I do?

I have found a similar question here, but couldn't make the function NRPercent does not work (and can't comment there).

like image 469
Lukas Avatar asked Sep 05 '18 13:09

Lukas


1 Answers

With the new version of scales you can use:

scale_y_continuous(labels = scales::percent_format(accuracy = 1))
like image 131
Axeman Avatar answered Oct 21 '22 04:10

Axeman