Search code examples
rggplot2decimalgeom-bar

change decimal on y axis, geom_bar, "accuracy" not working


I would like to design a bar plot that shows the frequencies of age classes in percent on the y-axis with no decimal. I use ggplot and geom_bar. I have read many posts but I can't remove the one decimal from the y-axis label (see pic: at the moment it is 50.0% but i want just 50%). I tried accuracy=1, accuracy=0.01 etc., but it is not working.

I have the following code to design the bar plot:

myplot_age <- ggplot(data_new_fewmissing_out, aes(age)) + 
geom_bar(aes(y = (..count..)/sum(..count..)), fill=c("lightgreen", "blue", "black", lightblue")) + 
scale_y_continuous(labels=scales::percent) +
ylab("relative frequencies")+ 
theme(axis.title.x = element_text(size = 15),
axis.title.y = element_text(size = 15),
axis.text = element_text(size = 12, color = "black"))+
scale_x_discrete(labels=c("young", "middle", "old" ,"no answer"))

The results looks like the following

bar plot with one decimal

If I apply the same code to another of my variables (e.g. farm system) it gives me no decimals so I would be grateful for a solution. Thanks!


Solution

  • use

    scales::percent_format(accuracy = 1)

    rather than

    scales::percent