我有以下数据
a<- rnorm(10)*10+100
b<- c(0,0,0,1,1,0,1,0,1,1)
首先,我想画一条线
plot(a)
然后我想画出b系列中哪里有1的点。
请帮我。我真的被困住了。
使用ifelse
您可以在向量“ a”中选择要绘制的值:
ifelse(b==1,a,0)
要在第一个绘图中获得线(不是默认点),应使用type="l"
。
代码可以是这样的:
set.seed(1) ## I set the seed here to get a reproducible example
a<- rnorm(10)*10+100
b<- c(0,0,0,1,1,0,1,0,1,1)
plot(a,type='l')
points(ifelse(b==1,a,0),col='red',pch=20,cex=2)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句