【问题标题】:Adding uncertainty bands to a smooth spline in a scatterplot将不确定带添加到散点图中的平滑样条曲线
【发布时间】:2016-04-17 00:03:58
【问题描述】:

我有一个带有平滑样条线的散点图

a<-rep(1:50,len=500)
b<-sample(0:5000,500)
c<-round(seq(0,600,len=500))
data_frame<-as.data.frame(cbind(a,b,c))
names(data_frame)<-c("ID","toxin_level","days_to_event")

plot(data_frame$days_to_event,data_frame$toxin_level, xlim=c(600,0),xlab="days before the event",ylab="Toxin level",type="p")
abline(v=0,col="red")

x <- data_frame$days_to_event
y <- data_frame$toxin_level

fit.sp = smooth.spline(y ~ x, nknots=20)
lines(fit.sp, col="blue")

这是结果图

我想知道是否有可能以某种方式在这条曲线上添加置信带?我确实希望它是透明的蓝色,但任何颜色都可以,包括灰色。

【问题讨论】:

标签: r scatter-plot spline


【解决方案1】:

更新:使用scale_x_reverse 更精确地匹配您的图表...

使用ggplot2 怎么样?

library(ggplot2)

ggplot(data_frame, aes(x = days_to_event, y = toxin_level)) + geom_point() +
  geom_vline(xintercept = 0, color = "red") + scale_x_reverse() + 
  xlab("Days before the event") + ylab("Toxin Level") + 
  geom_smooth(method = lm, se = TRUE)

这给出了这个:

或者更符合你的问题:

ggplot(data_frame, aes(x = days_to_event, y = toxin_level)) + geom_point(shape = 1) +
  geom_vline(xintercept = 0, color = "red") + scale_x_reverse() + 
  xlab("Days before the event") + ylab("Toxin Level") + 
  geom_smooth(method = lm, se = TRUE, color = "blue", fill = "lightblue") + 
  theme_bw()

【讨论】:

猜你喜欢
  • 2018-03-07
  • 2014-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-20
  • 1970-01-01
  • 1970-01-01
  • 2018-12-21
相关资源
最近更新 更多