简体   繁体   中英

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM