'How to show integers when using ggplot2::geom_smooth()

In the example below, how can I round the x label to even numbers? I cant convert them as factors first, because then geom_smooth does not work

library(ggplot2)

set.seed(32)


df <- data.frame(a = as.integer(rnorm(250, 2, 0.1)))
df$b <- df$a + rnorm(250)
df$id = 1

df_2 <- df
df_2$id <- 2

df_tot <- rbind(df, df_2)

ggplot(df_tot, aes(x = a, y = b)) +
  geom_smooth() +
  facet_wrap(~id)


Solution 1:[1]

If we want even numbers, an option is to add labels as a function in scale_x_continuous

library(ggplot2)
ggplot(df_tot, aes(x = a, y = b)) +
  geom_smooth() +
  facet_wrap(~id) + 
  scale_x_continuous(labels = function(x) seq(2, length.out = length(x)))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 akrun