繁体   English   中英

使用 ggplot2::geom_smooth() 时如何显示整数

[英]How to show integers when using ggplot2::geom_smooth()

在下面的示例中,如何将 x label 舍入为偶数? 我不能先将它们转换为因子,因为那样geom_smooth就不起作用

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)

如果我们想要偶数,一个选项是在 scale_x_continuous 中将labels添加为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)))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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