我如何绘制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) 

更新 :复制R维基上的资料http://rwiki.sciviews.org/doku.php?id=tips:graphics-base:2yaxes ,现在链接已经断开:也可以从wayback机器

两个不同的y轴在同一个阴谋

(一些材料最初由丹尼尔Rajdl 2006/03/31 15:26)

请注意,在同一地块内使用两种不同比例的情况很less。 误导graphics的观众是很容易的。 请检查以下两个示例和关于这个问题的评论(例1 ,来自Junk Charts的 example2 )以及Stephen Few撰写的这篇文章 (其结论是“我当然不能总结一下,双倍坐标轴图有用的;只是我不能想到一个情况,保证他们在其他更好的解决scheme。“)另见这个卡通的点#4 …

如果你确定了,基本的方法是创build你的第一个图,设置par(new=TRUE)来防止R清除graphics设备,创build第二个plot 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()软件包中的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")) 

在这里输入图像描述

类似的配方可以用来叠加不同types的图 – 条形图,直方图等。

顾名思义, twoord.plot()软件包中的twoord.plot()用两个坐标轴绘制。

 library(plotrix) example(twoord.plot) 

在这里输入图像描述

在这里输入图像描述

在这里输入图像描述

在这里输入图像描述

在这里输入图像描述

一个select是并排两个地块。 ggplot2facet_wrap()提供了一个很好的select:

 dat <- data.frame(x = c(rnorm(100), rnorm(100, 10, 2)) , y = c(rnorm(100), rlnorm(100, 9, 2)) , index = rep(1:2, each = 100) ) require(ggplot2) ggplot(dat, aes(x,y)) + geom_point() + facet_wrap(~ index, scales = "free_y") 

这是一个常见问题。 这是我六年前提供给R图库的一个较老的解决scheme

你可以看看例如在一个图表中结合了绝对和相对尺度的plotVolumeBars()函数。

如果您可以放弃刻度/轴标签,则可以将数据重新缩放至(0,1)间隔。 例如对于染色体上不同的“摆动”trakcs,当你通常对轨迹之间的局部相关性感兴趣时,它们具有不同的尺度(覆盖范围为数千,Fst 0-1)。

 # rescale numeric vector into (0, 1) interval # clip everything outside the range rescale <- function(vec, lims=range(vec), clip=c(0, 1)) { # find the coeficients of transforming linear equation # that maps the lims range to (0, 1) slope <- (1 - 0) / (lims[2] - lims[1]) intercept <- - slope * lims[1] xformed <- slope * vec + intercept # do the clipping xformed[xformed < 0] <- clip[1] xformed[xformed > 1] <- clip[2] xformed } 

然后,使用chrompositioncoveragefst列的数据框,可以执行如下操作:

 ggplot(d, aes(position)) + geom_line(aes(y = rescale(fst))) + geom_line(aes(y = rescale(coverage))) + facet_wrap(~chrom) 

这样做的好处是你不仅限于两个trakcs。

我也build议, twoord.stackplot()软件包中的twoord.stackplot()绘制更多的两个坐标轴。

 data<-read.table(text= "e0AL fxAL e0CO fxCO e0BR fxBR anos 51.8 5.9 50.6 6.8 51.0 6.2 1955 54.7 5.9 55.2 6.8 53.5 6.2 1960 57.1 6.0 57.9 6.8 55.9 6.2 1965 59.1 5.6 60.1 6.2 57.9 5.4 1970 61.2 5.1 61.8 5.0 59.8 4.7 1975 63.4 4.5 64.0 4.3 61.8 4.3 1980 65.4 3.9 66.9 3.7 63.5 3.8 1985 67.3 3.4 68.0 3.2 65.5 3.1 1990 69.1 3.0 68.7 3.0 67.5 2.6 1995 70.9 2.8 70.3 2.8 69.5 2.5 2000 72.4 2.5 71.7 2.6 71.1 2.3 2005 73.3 2.3 72.9 2.5 72.1 1.9 2010 74.3 2.2 73.8 2.4 73.2 1.8 2015 75.2 2.0 74.6 2.3 74.2 1.7 2020 76.0 2.0 75.4 2.2 75.2 1.6 2025 76.8 1.9 76.2 2.1 76.1 1.6 2030 77.6 1.9 76.9 2.1 77.1 1.6 2035 78.4 1.9 77.6 2.0 77.9 1.7 2040 79.1 1.8 78.3 1.9 78.7 1.7 2045 79.8 1.8 79.0 1.9 79.5 1.7 2050 80.5 1.8 79.7 1.9 80.3 1.7 2055 81.1 1.8 80.3 1.8 80.9 1.8 2060 81.7 1.8 80.9 1.8 81.6 1.8 2065 82.3 1.8 81.4 1.8 82.2 1.8 2070 82.8 1.8 82.0 1.7 82.8 1.8 2075 83.3 1.8 82.5 1.7 83.4 1.9 2080 83.8 1.8 83.0 1.7 83.9 1.9 2085 84.3 1.9 83.5 1.8 84.4 1.9 2090 84.7 1.9 83.9 1.8 84.9 1.9 2095 85.1 1.9 84.3 1.8 85.4 1.9 2100", header=T) require(plotrix) twoord.stackplot(lx=data$anos, rx=data$anos, ldata=cbind(data$e0AL, data$e0BR, data$e0CO), rdata=cbind(data$fxAL, data$fxBR, data$fxCO), lcol=c("black","red", "blue"), rcol=c("black","red", "blue"), ltype=c("l","o","b"), rtype=c("l","o","b"), lylab="Años de Vida", rylab="Hijos x Mujer", xlab="Tiempo", main="Mortalidad/Fecundidad:1950–2100", border="grey80") legend("bottomright", c(paste("Proy:", c("A. Latina", "Brasil", "Colombia"))), cex=1, col=c("black","red", "blue"), lwd=2, bty="n", lty=c(1,1,2), pch=c(NA,1,1) )