在ggplot&中绘制具有已知斜率的线。添加" CI"

时间:2017-12-01 19:10:01

标签: r plot ggplot2 linear-regression

我目前正在使用ggplot创建几个图,我拟合线性回归。

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

这会产生如下情节:

我希望在视觉上将回归的斜率与斜率为1且截距为0的另一条线进行比较,其中&#34;误差条&#34; (或带子)两侧覆盖0.9至1.1的斜坡。有没有人知道如何将这个添加到我的情节中?

2 个答案:

答案 0 :(得分:0)

使用geom_abline作为行,geom_ribbon作为行。

答案 1 :(得分:0)

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