我对以下数据执行了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
我想以图形方式表示重要的互动。因此,散点图包含每个&#34;位置的回归线&#34;这在x轴上只限于那个&#34;位置的值范围。&#34;
我确信有一种更快的制作图表的方法,但这是我迄今为止所做的:
##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)
我有情节,我只想将回归线限制为各自的值&#39;的范围内。
以下是&#34; i&#34;,dput(i)的数据子集:
结构(列表(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(&#34;夏威夷&#34;,&#34;印度&#34;,&#34;牙买加&#34;,&#34;毛里求斯&#34;,&#34; STX&#34 ; ),class =&#34; factor&#34;),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),。标签= c(&#34; F&#34;,&#34; M&#34;),类=&#34;因子&#34;),pc1 = c(0.836604139, 1.321670843,0.16817092,0.039442128,0.5223987,0.410205403, 0.994359741,0.9973975983,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.413423102,-0.534238012,1.37545187,-0.045763413,1.720985159, 0.278497659,1.085098831,1.513901703,0.074479968,0.667613565, 0.172891392,0.71098318,-1.373299481,-0.094551205,0.909750434 ),condition = c(53.5063398,-39.34044114,-78.65535012,-120.2468368, -25.24683677,-12.51917901,-5.927692357,-57.63406675,-15.70215231, -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.8746667, 240.9093333,222.204,217.5926667,214.7616667,152.1496667, 249.5276667,261.6316667,250.2583333,169.238,156.111,174.5236667, 266.704,162.64,244.4686667,254.796,194.3983333,235.491, 198.9133333,159.2223333,146.703,148.5333333,254.0833333)),. Name = c(&#34; id&#34;, &#34; location&#34;,&#34; sex&#34;,&#34; pc1&#34;,&#34; pc1_haw&#34;,&#34; condition&#34;,&#34; ap& #34;),row.names = 149:171,class =&#34; data.frame&#34;)
答案 0 :(得分:0)
请参阅?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]))