如何用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 Wiki上已复制的资料,网址为http://rwiki.sciviews.org/doku.php?id=tips:graphics-base:2yaxes,现在链接断开:也可以从Wayback机器上获得 同一图上的两个不同的y轴 (某些资料最初由Daniel Rajdl撰写,2006/03/31 15:26) 请注意,在极少数情况下,在同一绘图上使用两个不同的比例是合适的。容易误导图形的观看者。请检查以下两个示例和有关此问题的评论(“垃圾图表”中的example1,example2),以及Stephen Few的这篇文章(结论是“我当然不能一劳永逸地得出结论:双刻度轴的图永远不会有用;只是我无法根据其他更好的解决方案来考虑这种情况。”)另请参阅本动画片中的第4点... 如果确定的话,基本方法是创建第一个绘图,设置
par(new=TRUE)
以防止R清除图形设备,使用
axes=FALSE
创建第二个绘图(并将
xlab
ylab
设置为空白–
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) + 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()
会自动执行此过程。 另一个示例(改编自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,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,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,7000),col=\"red\")
## a little farther out (line=4) to make room for labels
mtext(\"Cell Density\",col=\"red\",line=4) 
axis(4,col.axis=\"red\",las=1)

## Draw the time axis
axis(1,pretty(range(time),10))
mtext(\"Time (Hours)\",side=1,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\"))
相似的配方可用于叠加不同类型的图-条形图,直方图等。     ,顾名思义,plotrix包中的“ 10”具有两个纵坐标轴。
library(plotrix)
example(twoord.plot)
    ,一种选择是并排绘制两个图。
ggplot2
facet_wrap()
为此提供了一个不错的选择:
dat <- data.frame(x = c(rnorm(100),rnorm(100,10,2)),y = c(rnorm(100),rlnorm(100,9,index = rep(1:2,each = 100)
  )

require(ggplot2)
ggplot(dat,aes(x,y)) + 
geom_point() + 
facet_wrap(~ index,scales = \"free_y\")
    ,如果可以放弃刻度/轴标签,则可以将数据重新缩放为(0,1)间隔。例如,当您通常对轨道之间的局部相关性感兴趣并且它们具有不同的比例(覆盖率成千上万,Fst 0-1)时,这适用于染色体上的不同“摆动” traks。
# 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
}
然后,使用具有
chrom
position
coverage
fst
列的数据帧,您可以执行以下操作:
ggplot(d,aes(position)) + 
  geom_line(aes(y = rescale(fst))) + 
  geom_line(aes(y = rescale(coverage))) +
  facet_wrap(~chrom)
这样做的好处是您不仅限于两个traccs。     ,我也建议,
plotrix
包中的plot26ѭ具有更多的两个纵坐标轴。
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\",ltype=c(\"l\",\"o\",\"b\"),rtype=c(\"l\",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,lwd=2,bty=\"n\",lty=c(1,1,2),pch=c(NA,1) )
    ,类似于@BenBolker接受的答案的另一种替代方法是在添加第二组点时重新定义现有图的坐标。 这是一个最小的例子。 数据:
x  <- 1:10
y1 <- rnorm(10,100,20)
y2 <- rnorm(10,1)
情节:
par(mar=c(5,5,5)+0.1,las=1)

plot.new()
plot.window(xlim=range(x),ylim=range(y1))
points(x,y1,pch=19)
axis(1)
axis(2,col.axis=\"red\")
box()

plot.window(xlim=range(x),ylim=range(y2))
points(x,y2,col=\"limegreen\",pch=19)
axis(4,col.axis=\"limegreen\")
    

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...