Home > front end >  How to show integers when using ggplot2::geom_smooth()
How to show integers when using ggplot2::geom_smooth()

Time:04-20

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)

CodePudding user response:

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)))
  • Related