I want to create a diagram with numbers formatted as percent on the y-axis. The data has only 3 decimals precision so I want numbers formatted as percent with 1 digits preceision. 0.034 should be formatted as 3.4%. But when I use scales::percent as a formatter function in ggplot2 the number is formatted with 2 decimals precision (0.034 = 3.40%).
The scales::percent function has an accuracy argument. Is there any way I can send an argument from ggplot to the scales::percent function?
library(ggplot2)
library(scales)
set.seed(12)
#Create a dataset with 3-digits precision:
df <- data_frame(xvar = LETTERS[1:5],yvar = round(runif(5, min = 0, max = 0.080), 3))
df$xvar <- as.factor(df$xvar)
print(df)
# A tibble: 5 x 2
# xvar yvar
# <fct> <dbl>
# 1 A 0.006
# 2 B 0.065
# 3 C 0.075
# 4 D 0.022
# 5 E 0.014
# The diagram shows percent with 2 decimals, but it should only be 1
ggplot(df, aes(xvar, yvar)) +
geom_bar(stat = "identity") +
scale_y_continuous(breaks = pretty_breaks(8), labels = percent)
# If a try to send an accuracy argument to scales:percent from ggplot it generates an error
ggplot(df, aes(xvar, yvar)) +
geom_bar(stat = "identity") +
scale_y_continuous(breaks = pretty_breaks(8), labels = percent(accuracy = 0.1))
# Error in number(x = x, accuracy = accuracy, scale = scale, prefix = prefix, :
# argument "x" is missing, with no default