Rspacer
Rspacer

Reputation: 2429

How to plot raw datapoints with estimated means and confidence interval from bootstrapped model in ggplot2 R?

I have data with several factors. I have run a bootstrapped model with means and confidence intervals. I want to plot both the datapoints along with the model results means and confidence intervals. How can I achieve that?

yval <- c(0.10,0.12,0.50,0.13,0.20,0.15,0.68,0.71,0.90,0.10,0.11,0.12,0.14,0.15,0.16,0.20,0.14,0.15,0.66,0.222)
type <- c("A","S","A","S","A","S","A","S","A","S","A","S","A","S","A","S","A","S","A","S")
status <- c("L","H","L","H","L","H","L","H","L","H","L","H","L","H","L","H","L","H","L","H")


data <- data.frame(yval, type, status)

I then tried a long complicated model which gives this matrix

                      est        2.5%     97.5%
    (Intercept)  0.14593333 -0.10347804 0.3817681
    typeA        0.26295217 -0.01156827 0.5503199
    statusH      0.09181279 -0.18906489 0.3748384

I want to plot the original data as points with appropriate mean and confidence interval from the model. Please do not suggest the in-built boostrapped confidence interval in ggplot2. I want to specifically use the ones above.

ggplot(data, aes(x=status, y= yval, color = status)) + theme_bw()  +  geom_point(position = pd, size = 1.5) + facet_grid(.~type)

Upvotes: 0

Views: 540

Answers (1)

CrunchyTopping
CrunchyTopping

Reputation: 814

Are you looking for geom_errorbar?

data2<-data.frame(yval= c(0.10,0.12,0.50,0.13,0.20,0.15,0.68,0.71,0.90,0.10,0.11,0.12,0.14,0.15,0.16,0.20,0.14,0.15,0.66,0.222),
                  typeA =c("A","S","A","S","A","S","A","S","A","S","A","S","A","S","A","S","A","S","A","S"),
                  statusH=c("L","H","L","H","L","H","L","H","L","H","L","H","L","H","L","H","L","H","L","H")

)
data2<-data2%>%
  mutate(.,type=mapvalues(data2$type, from = c("A", "S"), to = c("typeA", "statusH")))

data<-read.table(text="
type est        l2.5     u97.5
  (Intercept)  0.14593333 -0.10347804 0.3817681
typeA        0.26295217 -0.01156827 0.5503199
statusH      0.09181279 -0.18906489 0.3748384
",header=T)
pd=position_dodge(10)
ggplot(data, aes(x=type, y= est, color = type)) + 
  theme_bw()  +
  geom_point(size = 3.5,position=pd) + 
geom_errorbar(aes(ymin=l2.5, ymax=est+u97.5), width=0.1,position = pd)+
    geom_point(data=data2, aes(x=type,y=yval),size=1)

I might be missing something but it sounds like you can use the second matrix directly.

enter image description here

Upvotes: 1

Related Questions