I'm creating a line chart with ggplot2, and I want to remove the last decimal place of 0 from the y axis. Executing the following code, you will see that the y axis has the ticks of 0.050, 0.010, 0.005, and 0.001. However, I want the ticks of 0.05, 0.01, 0.005, and 0.001, removing the final 0. How can I achieve this?
tibble(
timepoints = 1:100,
alpha = 1 - exp(log(0.95)/(1:100))
) %>%
ggplot(
.,
aes(
x = timepoints,
y = alpha
)
)
geom_point()
geom_line()
scale_x_continuous(
n.breaks = 6
)
scale_y_continuous(
breaks = c(
0.05,
0.01,
0.005,
0.001
)
)
theme(
text = element_text(size=30)
)
CodePudding user response:
As stated in the comments by @Limey, you can use the labels
argument to set the desired text. I would use a function to strip the tailing zeros from the decimals. This way, if you change your breaks in the future, the labels will still have you desired effect.
tibble(
timepoints = 1:100,
alpha = 1 - exp(log(0.95)/(1:100))
) %>%
ggplot(aes(x = timepoints,y = alpha ))
geom_point()
geom_line()
scale_x_continuous( n.breaks = 6)
scale_y_continuous(breaks = c(0.05,0.01,0.005,0.001),
labels = function(x) sub("0 $", "", x))
theme(text = element_text(size=30))