2
votes

I'm currently creating several plots with ggplot where I fit a linear regression.

format.multi2<-theme_bw() + 
    theme(axis.line=element_line(colour="black"), 
        axis.text=element_text(size=14, colour="black"),
        axis.title=element_text(size=16, colour="black"),
        panel.grid=element_blank(),
        panel.border=element_blank(),
        plot.margin=unit(c(.3,.3,.4,.4), "cm"))

ggplot(bank.multi2, aes(x=d2H2, y=d2H1)) + 
     geom_point(size=2) + 
     geom_smooth(method='lm', se=FALSE, colour="black") +
     labs(y=expression(paste(paste(delta^2,"H")[t+x])), 
         x=expression(paste(paste(delta^2,"H")[t]))) + 
     xlim(min(bank.multi$d2H), max(bank.multi$d2H)) + 
     ylim(min(bank.multi$d2H), max(bank.multi$d2H)) +
     format.multi2

This results in a plot that looks like this:

I'd like to visually compare the slope of the regression to another line with a slope of 1 and an intercept of 0 with "error bars" (or a ribbon) on either side that covers slopes ranging from 0.9 to 1.1. Does anyone have an idea of how to add this to my plots?

2

2 Answers

0
votes

Use geom_abline for the line and geom_ribbon for the interval.

0
votes
library(ggplot2)
tmp = mtcars
tmp$gpm= (1/tmp$mpg)*100

# 1. make a plot of our data and a linear smoother
h = qplot(x= wt, y=gpm, data=tmp, xlim=c(0,5), ylim=c(0,5), geom=c("point", "smooth"), method="lm")

# 2. Add ribbon covering slopes ranging from 0.9 to 1.1 with intercept 0
h =h + geom_ribbon(aes(ymin = 0+ wt *.9, ymax = 0+ wt *1.1), fill = "grey70")

# 3. Highlight abline slope = 1 and intercept = 0
h = h + geom_abline(intercept=0, slope=1)

h

enter image description here