我想为线性模型显示95%和68%的阴影置信区间。另外,我想展示x1,95%CI和68%CI的相应图例。但相反,我得到的是一系列间隔。
这是我的数据
YEAR 2004 2005 2006 2007 2008
x1 -0.1 -1.8 -1.3 -1 -1.3
到目前为止代码:
library(ggplot2)
data=read.csv(file.choose())
s1=ggplot(data,aes (x = YEAR, y = x1))
p <-
s1+
geom_smooth(method="lm",color="black",aes(alpha=0.05),show.legend=TRUE)+
geom_smooth(method="lm",color="black",aes(alpha=0.32),show.legend=TRUE)+
scale_color_discrete(name='')+
geom_point(size=5.5)+
geom_line(linetype="dotted",size=2)+
xlab("Year")+
ylab("x1")+
guides(alpha = guide_legend(nrow = 16))+
annotate("text", x = 2007.4, y = 2, label = "Test",face="bold",size=16)+
geom_hline(linetype="dashed",aes(yintercept=0))
p
我得到的是这个:
答案 0 :(得分:1)
data <- data.frame(YEAR = 2004:2008,
x1 = c(0, -2, -1.5, -1, -1.5))
level
来表示置信区间。fill
内使用aes
表示我们想要为该关卡制作一个图例。yintercept = 0
应该在aes
之外,因为它是一个静态值。绘图代码:
ggplot(data,aes (x = YEAR, y = x1)) +
geom_smooth(method = "lm", aes(fill = "68%"), level = 0.68, col = 1) +
geom_smooth(method = "lm", aes(fill = "95%"), level = 0.95, col = 1) +
geom_point(size = 5.5) +
geom_line(linetype = "dotted", size = 2) +
geom_hline(linetype = "dashed", yintercept = 0) +
xlab("Year") +
scale_fill_grey(name = 'Confidence\n level') +
theme_classic()