问题描述
我想在R中的同一个图上显示两张图,其中两张图的尺寸差异很大,即一张图从-0.001到0.0001,另一张图从0.05到0.2。
我发现这个链接
它表示如何在同一个图上显示两个y轴,但我遇到了麻烦。
$
plot(rateOfChangeMS [,1],type =l,ylim = c (-0.01,.2),轴线= F)
线(比率[,1])$ b $ bx = seq(-0.001,0.0001,0.0001)
x2 = seq(0.05,0.2, 0.01)
轴(2,x)
轴(4,x2)
我遇到的问题是,虽然R显示了两个坐标轴,但它们并不像我想的那样彼此相邻,并附带了结果图。左轴正在测量小范围的图形,而右侧正在测量0.05到0.2的图形。事实上,第二张图是在图上,但缩放比例太小,以至于看不到它。
不确定是否有一些礼仪规则I '违反,从未上传图片之前,所以不太确定如何最好地做到这一点。
任何帮助将不胜感激!
谢谢
Mike
由于您没有提供可重复性示例或代表数据集,这是部分答案。
set.seed(1)
df< - data.frame(x = 1:100,
y1 = -0.001 + 0.002 /(1:100)+ rnorm(100,0,5e-5),
y2 = 0.05 + 0.0015 *( 0:99)+ rnorm(100,0,1e-2))
ticks.1< - seq(-0.001,0.001,0.0001)
ticks.2< - seq (0.05,0.2,0.01)
plot(df $ x,df $ y1,type =l,yaxt =n,xlab =X,ylab =,col = blue)
轴(2,at = ticks.1,col。 ticks =blue,col.axis =blue)
par(new = T)
plot(df $ x,df $ y2,type =l,yaxt =n, xlab =,ylab =,col =red)
轴(4,at = ticks.2,col.ticks =red,col.axis =red)
左轴被压缩的原因是两个轴的尺寸相同。你可以通过基本叠加两个完全不同的地块来解决这个问题(毕竟,这是两个轴所做的)。顺便说一句,像这样的双轴不是一种可视化数据的好方法。它会造成严重误导性的视觉印象。
I'm looking to display two graphs on the same plot in R where the two graphs have vastly different scales i.e. the one goes from -0.001 to 0.0001 and the other goes from 0.05 to 0.2.
I've found this link http://www.statmethods.net/advgraphs/axes.html
which indicates how to display two y axes on the same plot, but I'm having trouble.
My code reads as follows:
plot(rateOfChangeMS[,1],type="l",ylim=c(-0.01,.2),axes = F)
lines(ratios[,1])
x = seq(-0.001,0.0001,0.0001)
x2 = seq(0.05,0.2,0.01)
axis(2,x)
axis(4,x2)
The problem I'm having is that, although R shows both axes, they are not next to each other as I would like, with the resulting graph attached. The left axis is measuring the graph with the small range, while the right is measuring the graph from 0.05 to 0.2. The second graph is, in fact, on the plot, but the scaling is so small that you can't see it.
Not sure if there is some etiquette rule I'm violating, never uploaded an image before so not quite sure how best to do it.
Any help would be greatly appreciated!
Thanks
Mike
Since you don't provide a reproducible example, or a representative dataset, this is a partial answer.
set.seed(1)
df <- data.frame(x=1:100,
y1=-0.001+0.002/(1:100)+rnorm(100,0,5e-5),
y2=0.05+0.0015*(0:99)+rnorm(100,0,1e-2))
ticks.1 <- seq(-0.001,0.001,0.0001)
ticks.2 <- seq(0.05,0.2,0.01)
plot(df$x, df$y1, type="l", yaxt="n", xlab="X", ylab="", col="blue")
axis(2, at=ticks.1, col.ticks="blue", col.axis="blue")
par(new=T)
plot(df$x, df$y2, type="l", yaxt="n", xlab="", ylab="", col="red")
axis(4, at=ticks.2, col.ticks="red", col.axis="red")
The reason your left axis is compressed is that both axes are on the same scale. You can get around that by basically superimposing two completely different plots (which is what having two axes does, after all). Incidentally, dual axes like this is not a good way to visualize data. It creates a grossly misleading visual impression.
这篇关于R:使用两个y轴创建图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!