我想在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)
Run Code Online (Sandbox Code Playgroud)
Ben*_*ker 116
更新:这是对于R维基复制材料http://rwiki.sciviews.org/doku.php?id=tips:graphics-base:2yaxes,链接现在破:也可以从自由之路机
(一些材料最初由Daniel Rajdl 2006/03/31 15:26)
请注意,在同一地块上使用两个不同比例的情况很少.误导图形的观察者很容易.检查以下两个例子和关于这个问题的评论(例子1,来自Junk Charts的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, 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)
Run Code Online (Sandbox Code Playgroud)
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"))
Run Code Online (Sandbox Code Playgroud)

类似的配方可用于叠加不同类型的图 - 条形图,直方图等.
kmm*_*kmm 33
顾名思义,twoord.plot()在plotrix包装图中有两个纵坐标轴.
library(plotrix)
example(twoord.plot)
Run Code Online (Sandbox Code Playgroud)





与 @BenBolker 接受的答案类似的另一种选择是在添加第二组点时重新定义现有图的坐标。
这是一个最小的例子。
数据:
x <- 1:10
y1 <- rnorm(10, 100, 20)
y2 <- rnorm(10, 1, 1)
Run Code Online (Sandbox Code Playgroud)
阴谋:
par(mar=c(5,5,5,5)+0.1, las=1)
plot.new()
plot.window(xlim=range(x), ylim=range(y1))
points(x, y1, col="red", 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")
title("my plot", adj=0)
mtext("2nd y axis", side = 4, las=3, line=3, col="limegreen")
mtext("1st y axis", side = 2, las=3, line=3, col="red")
Run Code Online (Sandbox Code Playgroud)
一种选择是并排制作两个地块.ggplot2为此提供了一个很好的选择facet_wrap():
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")
Run Code Online (Sandbox Code Playgroud)
如果您可以放弃刻度/轴标签,则可以将数据重新调整为 (0, 1) 间隔。例如,这适用于染色体上不同的“摆动”轨迹,当您通常对轨迹之间的局部相关性感兴趣并且它们具有不同的尺度(以千为单位的覆盖范围,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
}
Run Code Online (Sandbox Code Playgroud)
然后,将具有与数据帧chrom,position,coverage和fst列,你可以这样做:
ggplot(d, aes(position)) +
geom_line(aes(y = rescale(fst))) +
geom_line(aes(y = rescale(coverage))) +
facet_wrap(~chrom)
Run Code Online (Sandbox Code Playgroud)
这样做的好处是您不仅限于两个 trakc。
小智 5
我也建议,twoord.stackplot()在plotrix包图中使用更多的两个纵坐标轴。
data<-read.table(text=\n"e0AL fxAL e0CO fxCO e0BR fxBR anos\n 51.8 5.9 50.6 6.8 51.0 6.2 1955\n 54.7 5.9 55.2 6.8 53.5 6.2 1960\n 57.1 6.0 57.9 6.8 55.9 6.2 1965\n 59.1 5.6 60.1 6.2 57.9 5.4 1970\n 61.2 5.1 61.8 5.0 59.8 4.7 1975\n 63.4 4.5 64.0 4.3 61.8 4.3 1980\n 65.4 3.9 66.9 3.7 63.5 3.8 1985\n 67.3 3.4 68.0 3.2 65.5 3.1 1990\n 69.1 3.0 68.7 3.0 67.5 2.6 1995\n 70.9 2.8 70.3 2.8 69.5 2.5 2000\n 72.4 2.5 71.7 2.6 71.1 2.3 2005\n 73.3 2.3 72.9 2.5 72.1 1.9 2010\n 74.3 2.2 73.8 2.4 73.2 1.8 2015\n 75.2 2.0 74.6 2.3 74.2 1.7 2020\n 76.0 2.0 75.4 2.2 75.2 1.6 2025\n 76.8 1.9 76.2 2.1 76.1 1.6 2030\n 77.6 1.9 76.9 2.1 77.1 1.6 2035\n 78.4 1.9 77.6 2.0 77.9 1.7 2040\n 79.1 1.8 78.3 1.9 78.7 1.7 2045\n 79.8 1.8 79.0 1.9 79.5 1.7 2050\n 80.5 1.8 79.7 1.9 80.3 1.7 2055\n 81.1 1.8 80.3 1.8 80.9 1.8 2060\n 81.7 1.8 80.9 1.8 81.6 1.8 2065\n 82.3 1.8 81.4 1.8 82.2 1.8 2070\n 82.8 1.8 82.0 1.7 82.8 1.8 2075\n 83.3 1.8 82.5 1.7 83.4 1.9 2080\n 83.8 1.8 83.0 1.7 83.9 1.9 2085\n 84.3 1.9 83.5 1.8 84.4 1.9 2090\n 84.7 1.9 83.9 1.8 84.9 1.9 2095\n 85.1 1.9 84.3 1.8 85.4 1.9 2100", header=T)\n\nrequire(plotrix)\ntwoord.stackplot(lx=data$anos, rx=data$anos, \n ldata=cbind(data$e0AL, data$e0BR, data$e0CO),\n rdata=cbind(data$fxAL, data$fxBR, data$fxCO),\n lcol=c("black","red", "blue"),\n rcol=c("black","red", "blue"), \n ltype=c("l","o","b"),\n rtype=c("l","o","b"), \n lylab="A\xc3\xb1os de Vida", rylab="Hijos x Mujer", \n xlab="Tiempo",\n main="Mortalidad/Fecundidad:1950\xe2\x80\x932100",\n border="grey80")\nlegend("bottomright", c(paste("Proy:", \n c("A. Latina", "Brasil", "Colombia"))), cex=1,\n col=c("black","red", "blue"), lwd=2, bty="n", \n lty=c(1,1,2), pch=c(NA,1,1) )\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
165753 次 |
| 最近记录: |