如何使用 2 个不同的 y 轴进行绘图?
我想在 R 中叠加两个散点图,以便每组点都有自己的(不同的)y 轴(即,在图上的位置 2 和 4),但这些点看起来叠加在同一个图上。
可以用plot 来做到这一点吗?
编辑显示问题的示例代码
# example code for SO question
y1 <- rnorm(10, 100, 20)
y2 <- rnorm(10, 1, 1)
x <- 1:10
# in this plot y2 is plotted on what is clearly an inappropriate scale
plot(y1 ~ x, ylim = c(-1, 150))
points(y2 ~ x, pch = 2)
I would like superimpose two scatter plots in R so that each set of points has its own (different) y-axis (i.e., in positions 2 and 4 on the figure) but the points appear superimposed on the same figure.
Is it possible to do this with plot
?
Edit Example code showing the problem
# example code for SO question
y1 <- rnorm(10, 100, 20)
y2 <- rnorm(10, 1, 1)
x <- 1:10
# in this plot y2 is plotted on what is clearly an inappropriate scale
plot(y1 ~ x, ylim = c(-1, 150))
points(y2 ~ x, pch = 2)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
更新:复制 R wiki 上的材料 http://rwiki.sciviews.org/doku.php?id=tips:graphics-base:2yaxes,链接现已损坏:也可从 回程机
同一个图上有两个不同的 y 轴
(一些材料最初由 Daniel Rajdl 2006/03/31 15:26 提供)
请注意,有很少的适合在同一地块上使用两种不同尺度的情况。很容易误导图形的观看者。检查以下两个示例和有关此问题的评论(example1,example2 来自垃圾图表),以及Stephen Few 的这篇文章(结论“我当然不能一劳永逸地得出结论,具有双尺度轴的图表从来没有用;只是我无法考虑到其他更好的解决方案来保证它们的情况。”)另请参阅<中的第4点a href="http://www.smbc-comics.com/?id=3167#comic" rel="noreferrer">这部漫画 ...
如果您下定决心,基本秘诀就是创建您的第一个情节,设置
par(new=TRUE)
以防止 R 清除图形设备,使用axes=FALSE
创建第二个图(并设置xlab
和 < code>ylab 为空 -ann=FALSE
也应该有效),然后使用axis(side=4)
在右侧添加一个新轴 -手侧,以及mtext(...,side=4)
在右侧添加轴标签。下面是一个使用一点虚构数据的示例:plotrix
包中的twoord.plot()
可以自动执行此过程,doubleYScale()< 也是如此。 /code> 在
latticeExtra
包中。另一个示例(改编自 Robert W. Baer 的 R 邮件列表帖子):
可以使用类似的方法叠加不同类型的图——条形图、直方图等。
update: Copied material that was on the R wiki at http://rwiki.sciviews.org/doku.php?id=tips:graphics-base:2yaxes, link now broken: also available from the wayback machine
Two different y axes on the same plot
(some material originally by Daniel Rajdl 2006/03/31 15:26)
Please note that there are very few situations where it is appropriate to use two different scales on the same plot. It is very easy to mislead the viewer of the graphic. Check the following two examples and comments on this issue (example1, example2 from Junk Charts), as well as this article by Stephen Few (which concludes “I certainly cannot conclude, once and for all, that graphs with dual-scaled axes are never useful; only that I cannot think of a situation that warrants them in light of other, better solutions.”) Also see point #4 in this cartoon ...
If you are determined, the basic recipe is to create your first plot, set
par(new=TRUE)
to prevent R from clearing the graphics device, creating the second plot withaxes=FALSE
(and settingxlab
andylab
to be blank –ann=FALSE
should also work) and then usingaxis(side=4)
to add a new axis on the right-hand side, andmtext(...,side=4)
to add an axis label on the right-hand side. Here is an example using a little bit of made-up data:twoord.plot()
in theplotrix
package automates this process, as doesdoubleYScale()
in thelatticeExtra
package.Another example (adapted from an R mailing list post by Robert W. Baer):
Similar recipes can be used to superimpose plots of different types – bar plots, histograms, etc..
顾名思义,plotrix 封装具有两个纵坐标轴的图。
As its name suggests,
twoord.plot()
in the plotrix package plots with two ordinate axes.如果可以放弃刻度/轴标签,则可以将数据重新调整为 (0, 1) 间隔。例如,当您通常对轨迹之间的局部相关性感兴趣并且它们具有不同的尺度(覆盖范围以千为单位,Fst 0-1)时,这适用于染色体上不同的“摆动”轨迹。
然后,拥有一个包含
chrom
、position
、coverage
和fst
列的数据框,您可以执行以下操作:这样做的优点是您不限于两条轨道。
If you can give up the scales/axis labels, you can rescale the data to (0, 1) interval. This works for example for different 'wiggle' trakcs on chromosomes, when you're generally interested in local correlations between the tracks and they have different scales (coverage in thousands, Fst 0-1).
Then, having a data frame with
chrom
,position
,coverage
andfst
columns, you can do something like:The advantage of this is that you're not limited to two trakcs.
与 @BenBolker 接受的答案类似的另一种选择是在添加第二组点时重新定义现有图的坐标。
这是一个最小的例子。
数据:
情节:
Another alternative which is similar to the accepted answer by @BenBolker is redefining the coordinates of the existing plot when adding a second set of points.
Here is a minimal example.
Data:
Plot:
一种选择是并排绘制两个图。
ggplot2
通过facet_wrap()
提供了一个不错的选择:One option is to make two plots side by side.
ggplot2
provides a nice option for this withfacet_wrap()
:我也建议,
plotrix
包中的twoord.stackplot()
可以使用更多两个纵坐标轴进行绘图。I too suggests,
twoord.stackplot()
in theplotrix
package plots with more of two ordinate axes.