r - 如何使用 2 个不同的 y 轴进行绘图?

标签 r plot yaxis

我想在 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)

最佳答案

更新:复制 R wiki http://rwiki.sciviews.org/doku.php?id=tips:graphics-base:2yaxes 上的 Material ,链接现已损坏:也可从 the wayback machine 获取

同一绘图上的两个不同 y 轴

(一些 Material 最初由 Daniel Rajdl 2006/03/31 15:26)

请注意,在极少数情况下适合在同一绘图上使用两种不同的比例。很容易误导图形的观看者。检查以下两个示例和对此问题的评论( example1example2 来自 Junk Charts ),以及 this article by Stephen Few (其结论是“我当然不能一劳永逸地得出结论,具有双尺度轴的图表永远没有用;只是我无法考虑到其他更好的解决方案来保证它们的情况。”)另见第#点4 于 this cartoon ...

如果您下定决心,基本方法就是创建您的第一个图,设置 par(new=TRUE)为了防止 R 清除图形设备,请使用 axes=FALSE 创建第二个图。 (并将 xlabylab 设置为空白 – ann=FALSE 也应该有效),然后使用 axis(side=4)在右侧添加一个新轴,并且 mtext(...,side=4)在右侧添加轴标签。这是一个使用一点虚构数据的示例:

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)

twoord.plot()plotrix软件包自动执行此过程,doubleYScale() 也是如此。在 latticeExtra包。

另一个示例(改编自 Robert W. Baer 的 R 邮件列表帖子):

## 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"))

enter image description here

类似的方法可用于叠加不同类型的图——条形图、直方图等。

关于r - 如何使用 2 个不同的 y 轴进行绘图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6142944/

相关文章:

python - Pandas 堆积条形图中元素的排序

python - 修复 x 轴缩放和自动缩放 y 轴

ios - 图表 3.0 - 无法在条形图中显示小数 - iOS Swift

R data.table fread 命令 : how to read large files with irregular separators?

r - 将矩阵列表转换为单个数据框

python - 使用具有 > 2 个参数的用户定义函数的 matplotlib 绘制 3d 曲面

octave - 如何使用 GNU Octave 中的范围设置 x 或 y 轴的控制?

r - 如何使用 R 的 xlsx 包对齐 XLSX 文件的单元格?

r - 用于运行多个变量的线性模型和方差分析并收集数据框中的 p 值的函数

python - 如何获取 matplotlib 中轴上单个单位的长度(以像素为单位)?