如何使用 2 个不同的 y 轴进行绘图? [英] How can I plot with 2 different y-axes?
问题描述
我想在 R 中叠加两个散点图,以便每组点都有自己(不同的)y 轴(即,在图中的位置 2 和 4),但这些点叠加在同一图中.
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.
是否可以用 plot
做到这一点?
Is it possible to do this with 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)
推荐答案
更新:复制了 R wiki 上的材料 http://rwiki.sciviews.org/doku.php?id=tips:graphics-base:2yaxes,现在链接损坏:也可从 回归机器
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
(Daniel Rajdl 原创的一些材料 2006/03/31 15:26)
(some material originally by Daniel Rajdl 2006/03/31 15:26)
请注意,在极少数情况下,在同一张图上使用两种不同的尺度是合适的.很容易误导图形的查看者.检查以下两个示例和对此问题的评论(example1、
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 ...
如果你确定了,基本的方法是创建你的第一个绘图,设置 par(new=TRUE)
以防止 R 清除图形设备,用 轴创建第二个绘图=FALSE
(并将 xlab
和 ylab
设置为空白 - ann=FALSE
也应该有效)然后使用 axis(side=4)
在右侧添加一个新轴,mtext(...,side=4)
在右侧添加一个轴标签边.这是一个使用一些虚构数据的示例:
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 with axes=FALSE
(and setting xlab
and ylab
to be blank – ann=FALSE
should also work) and then using axis(side=4)
to add a new axis on the right-hand side, and mtext(...,side=4)
to add an axis label on the right-hand side. Here is an example using a little bit of made-up data:
set.seed(101)
x <- 1:10
y <- rnorm(10)
## second data set on a very different scale
z <- runif(10, min=1000, max=10000)
par(mar = c(5, 4, 4, 4) + 0.3) # Leave space for z axis
plot(x, y) # first plot
par(new = TRUE)
plot(x, z, type = "l", axes = FALSE, bty = "n", xlab = "", ylab = "")
axis(side=4, at = pretty(range(z)))
mtext("z", side=4, line=3)
plotrix
包中的
twoord.plot()
自动执行此过程,latticeExtra
中的 doubleYScale()
也是如此代码> 包.
twoord.plot()
in the plotrix
package automates this process, as does doubleYScale()
in the latticeExtra
package.
另一个例子(改编自 Robert W. Baer 的 R 邮件列表帖子):
Another example (adapted from an R mailing list post by Robert W. Baer):
## set up some fake test data
time <- seq(0,72,12)
betagal.abs <- c(0.05,0.18,0.25,0.31,0.32,0.34,0.35)
cell.density <- c(0,1000,2000,3000,4000,5000,6000)
## add extra space to right margin of plot within frame
par(mar=c(5, 4, 4, 6) + 0.1)
## Plot first set of data and draw its axis
plot(time, betagal.abs, pch=16, axes=FALSE, ylim=c(0,1), xlab="", ylab="",
type="b",col="black", main="Mike's test data")
axis(2, ylim=c(0,1),col="black",las=1) ## las=1 makes horizontal labels
mtext("Beta Gal Absorbance",side=2,line=2.5)
box()
## Allow a second plot on the same graph
par(new=TRUE)
## Plot the second plot and put axis scale on right
plot(time, cell.density, pch=15, xlab="", ylab="", ylim=c(0,7000),
axes=FALSE, type="b", col="red")
## a little farther out (line=4) to make room for labels
mtext("Cell Density",side=4,col="red",line=4)
axis(4, ylim=c(0,7000), col="red",col.axis="red",las=1)
## Draw the time axis
axis(1,pretty(range(time),10))
mtext("Time (Hours)",side=1,col="black",line=2.5)
## Add Legend
legend("topleft",legend=c("Beta Gal","Cell Density"),
text.col=c("black","red"),pch=c(16,15),col=c("black","red"))
类似的配方可用于叠加不同类型的图——条形图、直方图等.
Similar recipes can be used to superimpose plots of different types – bar plots, histograms, etc..
这篇关于如何使用 2 个不同的 y 轴进行绘图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!