[英]Restrict regression lines to data range for multiple lines on single plot in R
我对以下数据执行了ANCOVA(位置是5向变量):
id location sex pc1 pc1_haw condition ap
115 49 Hawaii M NA 0.8966151 -7.067692 103.014
116 27 Hawaii M NA 2.5289696 82.053163 197.674
117 33 Hawaii M NA 1.5887016 18.134019 130.765
118 34 Hawaii M NA NA 21.040414 117.580
119 35 Hawaii M NA 2.5356646 -42.053211 93.099
120 55 Hawaii M NA 0.7416007 -61.093436 132.507
summary(aov(ap~condition*location))
Df Sum Sq Mean Sq F value Pr(>F)
condition 1 6523 6523 8.553 0.00405 **
location 4 88048 22012 28.864 < 2e-16 ***
condition:location 4 8014 2003 2.627 0.03729 *
Residuals 135 102954 763
我想以图形方式表示重要的互动。 因此,一个散点图包含了每个“位置”的回归线,该回归线在x轴上仅限于该“位置”的值范围。
我敢肯定有一种更快的方法来制作图形本身,但这是我到目前为止所做的:
##make different subsets of data for each location
h=subset(males, location=="Hawaii")
j=subset(males, location=="Jamaica")
i=subset(males, location=="India")
s=subset(males, location=="STX")
m=subset(males, location=="Mauritius")
## make different regression lines for each location
reg_i=lm(i$ap~i$condition)
reg_j=lm(j$ap~j$condition)
reg_s=lm(s$ap~s$condition)
reg_h=lm(h$ap~h$condition)
reg_m=lm(m$ap~m$condition)
## made the initial plot and abline with just one set of data
plot(i$ap~i$condition, xlab="Condition", ylab="Anal Pad (mm^2)", xlim=c(-175,175), ylim=c(0,300), pch=1, frame.plot=F)
abline(reg_i)
## then added the rest to the existing plot with different colors and shapes
points(j$condition,j$ap, pch=16, col=2)
abline(reg_j, col=2)
points(h$condition,h$ap, pch=15, col=4)
abline(reg_h, col=4)
points(s$condition,s$ap, pch=17, col=5)
abline(reg_s, col=5)
points(m$condition,m$ap, pch=18, col=6)
abline(reg_m, col=6)
我有图,我只想能够将回归线限制在其各自值的范围内。
这是dput(i)的“ i”数据子集:
结构(list(id = c(51L,50L,49L,48L,47L,46L,42L,40L,39L,38L,36L,34L,32L,29L,27L,26L,23L,22L,21L,18L,17L, 15L,31L),位置=结构(c(2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L ,2L,2L,2L),.Label = c(“ Hawaii”,“ India”,“ Jamaica”,“ Mauritius”,“ STX”),class =“ factor”),sex = structure(c(2L,2L ,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L),. Label = c(“ F“,” M“),类=”因数“),pc1 = c(0.836604139,1.321670843,0.16817092,0.039442128,0.5223987,0.410205403,0.994359741,0.973975983,1.742148099,-0.94672848,0.798914775,-0.253556712,2.316481173,-0.247525226, 1.389016661,2.178869889,-0.517312934,0.875533845,-0.333274608,NA,-1.899439969,0.800857011,1.26994084),pc1_haw = c(0.474631073,1.484601953,0.147219614,0.278126658,0.318994781,0.608755224,1.037980473,1.087243461,1.413423380 0.045763413、1.720985159、0.278497659、1.085098831、1.5 13901703,0.074479968,0.676913565,0.172891392,0.71098318,-1.373299481,-0.094551205,0.909750434),条件= c(53.5063398,-39.34044114,-78.65535012,-120.2468368,-25.24683677,-12.5191675790,-5.927692357,--15.6340615 76.94897573,-48.08938228,-44.90217355、76.79996541,-106.7234357、0.093591019,-6.042580098,-43.54046238,-7.744719058,-25.20003459,-24.45109345,-106.6043125,NA,-3.880890175),ap = c(223.8217.5667.240.9) ,214.7616667、152.1496667、249.5276667、261.6316667、250.2583333、169.238、156.11、174.5236667、266.704、162.64、244.4686667、254.796、194.3983333、235.491、198.9133333、159.2223333、146.703、148.5333333、254.0833333 =“,” “位置”,“性别”,“ pc1”,“ pc1_haw”,“条件”,“ ap”),row.names = 149:171,class =“ data.frame”)
参见?abline
,发现它在当前图上产生一条线。 使用lines
代替
例如:
x1 = min(i$condition, na.rm = TRUE)
x2 = max(i$condition, na.rm = TRUE)
plot(i$ap~i$condition, xlab="Condition", ylab="Anal Pad (mm^2)",
xlim=c(-175,175), ylim=c(0,300), pch=1, frame.plot=F)
lines(x = c(x1, x2), y = c(x1 * reg_i$coefficients[2] + reg_i$coefficients[1],
x2 * reg_i$coefficients[2] + reg_i$coefficients[1]))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.