我正在尝试绘制一个既涉及geom_point()
函数又涉及函数的geom_smooth()
图形。
我想geom_smooth()
在某个 x 值处裁剪该函数,但仅在其所有值都已用于计算平滑曲线之后(即我不想使用xlim()
,这将删除用于绘图的值)。
可重现的例子:
library(dplyr)
library(ggplot2)
set.seed(42)
test <- data.frame(replicate(2,sample(0:10,100,rep=TRUE)))
g <- ggplot() + geom_point(data = test, aes(x = X1, y = X2))
t_i <- test
t_i$group <- as.factor(as.numeric(cut(t_i$X1, 25)))
summar_t <- t_i %>%
group_by(group) %>%
summarise(y_mean=mean(X2),
y_sd=sd(X2),
c_mean =mean(X1,na.rm=T),
n =n()
)
summar_t$t_2sd <- summar_t$y_mean + summar_t$y_sd*2
g2 <- g + geom_smooth(data = summar_t, aes(x=c_mean, y = t_2sd), se=FALSE, method = lm, formula=y~poly(x,2), color = "black", linetype=3)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句