我有Observed
和估算(Est
)值的回归,如下面的head
所示。
data <- structure(list(IndID = structure(c(1L, 2L, 3L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L,
22L, 23L), .Label = c("CAL_F01", "CAL_F17", "CAL_F19", "CAL_F23",
"CAL_F43", "CAL_M33", "CAL_M36", "COL_P01", "COL_P03", "COL_P05",
"COL_P06", "COL_P07", "COL_P08", "COL_P09", "COL_P10", "COL_P12",
"COL_P13", "PAT_F03", "PAT_F04", "PAT_F05", "PAT_M02", "PAT_M03",
"PAT_M04"), class = "factor"), StudyArea = structure(c(1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L,
3L, 3L, 3L, 3L), .Label = c("Cali", "Colo", "Pata"), class = "factor"),
Observed = c(22L, 50L, 8L, 54L, 30L, 11L, 90L, 53L, 9L, 42L,
72L, 40L, 60L, 58L, 20L, 37L, 50L, 67L, 20L, 19L, 58L, 5L
), variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "PredKills", class = "factor"),
Est = c(28, 52, 6, 35, 31, 13, 80, 62, 4, 43, 66, 43, 55,
42, 20, 47, 36, 84, 20, 17, 36, 6), SE = c(3.50031581162016,
4.8742514125436, 1.20589766104628, 4.79430832229519, 3.87541734990744,
2.36031827307993, 6.35148447967163, 5.52456747941261, 1.60267125934065,
4.53967516735091, 6.61559705260502, 5.35175112687543, 5.89582419295991,
5.18042529534246, 3.43767468948519, 4.69809433696684, 3.80733165582324,
5.85520173339347, 3.151903629499, 2.64621136787301, 4.64130814363024,
1.41537000011436)), .Names = c("IndID", "StudyArea", "Observed",
"variable", "Est", "SE"), row.names = c(1L, 2L, 3L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
21L, 22L, 23L), class = "data.frame")
> head(data)
IndID StudyArea Observed variable Est SE
1 CAL_F01 Cali 22 PredKills 28 3.500316
2 CAL_F17 Cali 50 PredKills 52 4.874251
3 CAL_F19 Cali 8 PredKills 6 1.205898
5 CAL_F43 Cali 54 PredKills 35 4.794308
6 CAL_M33 Cali 30 PredKills 31 3.875417
7 CAL_M36 Cali 11 PredKills 13 2.360318
此代码如下图所示。
p2 <- ggplot(data, aes(x=Observed, y=Est, color=StudyArea))
p2+ geom_point(shape="*", size = 12) +
geom_abline(intercept =0, slope = 1, size = 1)+
geom_errorbar(aes(x= Observed, ymin=Est-SE, ymax=Est+SE, color=StudyArea),width = 0.5,cex=1, lty=2)+
scale_color_manual(values=c("red","blue","darkgreen"))+
ylim(2,92)+ xlim(2,92)
实线表示一对一的拟合。即如果Obs
= Est
,那么积分就会落在线上。线上的残差显然是误差。
问题:如何将阴影区域添加到代表10%错误的45度线。我认为我需要按照建议at this SO post使用geom_ribbon
,但无法生成正确的结果。
ADDITION 阴影区域不应随x轴或y轴的变化而变化,但应在45度线上保持不变。
我想保持x轴值相同,并且通过更改y轴值阴影区域,其中y比Est
大0.9并且小于Est
。
以下是我一直在使用的代码,用于添加比Est
低0.9的额外行。
p2+ geom_point(shape="*", size = 12) +
geom_errorbar(aes(x= ObsKills, ymin=value-SE, ymax=value+SE, color=StudyArea),width = 0.5,cex=1, lty=2)+
coord_cartesian(ylim=c(2,92), xlim=c(2,92))+
scale_color_manual(values=c("red","blue","darkgreen"))+
geom_abline(intercept =0, slope = 1, size = 1, col="red")+
geom_abline(data=data.frame(x=seq(1,92,1),y=seq(1,92,1)), aes(x=x, y=y*0.9),lty=2, cex=1)
使用最后一行代码(从@BrodieG获取提示),我创建了一个新的data.frame
,其中seq
的值反映了Observed
和Est
数据。在aes
函数中我多y
乘以0.9。在我看来,这一行应该比Est
低0.9而不是顶部。
我希望在它们之间添加一条较低和较高的线然后遮挡,尽管可能有更好的方法来做到这一点。
希望这更清楚......
提前致谢!
答案 0 :(得分:1)
这是一个实现,虽然我不确定我做的正是你想要的:
p2 <- ggplot(data, aes(x=Observed, y=Est, color=StudyArea))
p2+
geom_ribbon(data=data.frame(x=c(0,100)), aes(x=x, ymin=x * .9, ymax=x * 1.1), fill="gray", inherit.aes=F, alpha=0.5) +
geom_abline(intercept =0, slope = 1, size = 1)+
geom_point(shape="*", size = 12) +
geom_errorbar(aes(x= Observed, ymin=Est-SE, ymax=Est+SE, color=StudyArea),width = 0.5,cex=1, lty=2)+
scale_color_manual(values=c("red","blue","darkgreen"))+
coord_cartesian(ylim=c(2,92), xlim=c(2,92))