在 y 轴上绘制两个变量

3

我一直在尝试在一个 y 轴上绘制两个变量。这是我的代码:

set.seed(20)
x1=rnorm(100)
x2=rnorm(100)
x3=rnorm(100)
t=data.frame(a=x1,b=x1+x2,c=x1+x2+x3)
plot(t$a,t$b,xlab="",ylab="",pch=0,col="red")
par(new=TRUE)
plot(t$a,t$c,xlab="a",ylab="b (blue) and c (red)",
col="blue",abline(c   (0,0),c (1,1),lty=5,col=320))

由于两个y轴使用不同的比例尺,因此y轴上有两个刻度。我该如何修改我的代码?非常感谢你的帮助!

1个回答

5
您可以在两个图中使用相同的“ylim”范围,这样它们看起来就很好:
set.seed(20)
x1=rnorm(100)
x2=rnorm(100)
x3=rnorm(100)
t=data.frame(a=x1,b=x1+x2,c=x1+x2+x3)
#setting ylim below to the range -4 , 4
plot(t$a,t$b,xlab="",ylab="",pch=0,col="red", ylim=c(-4,4))
par(new=TRUE)
#doing it again here
plot(t$a,t$c,xlab="a",ylab="b (blue) and c (red)",
     col="blue",abline(c   (0,0),c (1,1),lty=5,col=320), ylim=c(-4,4))

输出:

在这里输入图片描述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接