简体   繁体   English

ggplot的置信区间

[英]Confidence intervals for ggplot

If you have two sets of data that you want to plot on the same graph, is there any way to get confidence intervals for just one of the datasets and not the other? 如果要在同一图上绘制两组数据,是否有任何方法可以仅获取其中一个数据集而不获取另一个数据集的置信区间? eg, regressions only show up significant for data group A but not B, but you still want to visually depict the data for both A & B in the same graph, with confidence intervals around the significant group A only. 例如,回归仅显示对数据组A有意义,而对B则不显着,但是您仍然希望在同一图中直观地描绘A和B的数据,而置信区间仅在重要组A周围。

You can selectively choose which data to pass to the regression plotter. 您可以有选择地选择要传递给回归绘图仪的数据。

Consider this example: 考虑以下示例:

set.seed(10)

#Make sample data
df <- data.frame(
  group=rep(c("A","B"), each=10),
  X = rep(1:10, 2))
df$Y <- 2*df$X + runif(20, -20, 20)  #Create y values with lots of noise

#Reduce the noise for group A
df[df$group == "A", "Y"] <- 2*df[df$group == "A", "X"] + rnorm(10)

#Compare regression p-values
coef(summary(lm(Y ~ X, data=df[df$group == "A", ])))[, 4] #p < 0.05 for group A
# (Intercept)            X 
#1.577943e-01 5.411004e-09 

coef(summary(lm(Y ~ X, data=df[df$group == "B", ])))[, 4] #p > 0.05 for group B
#(Intercept)           X 
#  0.7338232   0.1309030

#Graph all points, coloring by group.  Add a regression line for group A only.
ggplot(df, aes(x=X, y=Y, colour= group)) + theme_bw() + 
   geom_point(size=2.5) + 
   geom_smooth(data = df[df$group == "A",], method="lm")

仅对A组进行回归

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

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