简体   繁体   中英

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.

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组进行回归

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