我想在R中制作交互图,如示例
http://www.ling.upenn.edu/~joseff/rstudy/week4.html#interactionplot
除了代替我想要通过除数字之外的某些属性为点着色的线。所需的功能看起来像
blood_pressure = ... # this will be the x-axis
age_bucket = ... # this will be the interaction-level
gender = ... # color boys blue, girls red
stroke_rate = ... # y-axis
interaction.plot(blood_pressure, age_bucket, stroke_rate, col=gender, type="b"...
除了color =性别实际上做了我想要的
答案 0 :(得分:1)
问题在于interaction.plot
实际上依赖于tapply()
来计算汇总的汇总度量(平均值或中位数)。由于interaction.plot
只是调用matplot
,您可以根据需要直接使用后者,或者使用plyr汇总数据,并使用ggplot2绘制结果(更灵活)。< / p>
# consider the 64x4 dataset OrchardSprays and create a fake
# two-levels factor, say grp, which is in correspondence to rowpos odd/even values
grp <- gl(2, 1, 8, labels=letters[1:2])
# the following won't work (due to color recycling)
with(OrchardSprays,
interaction.plot(treatment, rowpos, decrease,
type="b", pch=19, lty=1, col=as.numeric(colpos), legend=F))
# what is used to draw the 8 lines is computed from
with(OrchardSprays,
tapply(decrease, list(treatment=treatment, rowpos=rowpos), mean))
# the following will work, though
with(OrchardSprays,
interaction.plot(treatment, rowpos, decrease,
type="b", pch=19, lty=1, col=as.numeric(grp), legend=F))
简而言之,假设您可以在gender
与跟踪因子(age_bucket
)之间找到足够的映射,则需要构建大小为nlevels(age_bucket)
的颜色矢量。