【问题标题】:Dual Y-Axis Base R Plot with different x-ticks具有不同 x 刻度的双 Y 轴基础 R 图
【发布时间】:2019-06-04 20:24:45
【问题描述】:

我想使用双 y 轴绘制 4 组数据点的图。前两个在左侧 y 轴上,后两个在右侧 y 轴上。前两个属于从 5000 到 50,000 的一组数字。第二组数据属于1-100的范围。我想绘制它以便很容易辨别这两个轴不仅在不同的比例上,而且来自两个不同范围的不同集合的点之间的高度显然很大。我不希望能够绘制一条水平线,这表明左侧 y 轴上的某个数字可以双射映射到右侧 y 轴上的某个数字。我想让它使得通过左 y 轴和右 y 轴的任何点的水平线仅属于与左轴或右轴相关的一组。

How can I plot with 2 different y-axes?。有

我会使用 twoord.plot 从 plotrix v3.7-5 由 Jim Lemon 撰写,但这比 base R 有缺点,因为我不能将 4 组数据添加到一个图中。我只能使用 2 组 (x,y) 对和 2--ord 图。我理论上可以使用基数 R 绘制 n 组 (x,y) 对。

无

以下是不起作用的:

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

【问题讨论】:

    标签: r plot


    【解决方案1】:

    不太清楚你在追求什么,但你可以使用lines 添加额外的“每幅图线”。

    我已经编辑了你的代码

        ## 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")
    lines(seq(0, 1, 0.02), type = 'o')
    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")
    lines(seq(0, 5000, 10), type = 'o', 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)
    

    产生这个:

    如果这不是您想要的,请告诉我。

    【讨论】:

    • 我追求的是 beta gal 真的很短,细胞密度真的很长。
    • 好吧,一个上限为 0.4,一个上限为 6000,我想你已经得到了你想要的。如果将它们放在同一个图中,您的细胞密度图将保持原样,而 β 凝胶只是一个小点。
    猜你喜欢
    • 2013-01-22
    • 1970-01-01
    • 2016-12-12
    • 1970-01-01
    • 2019-01-05
    • 2012-04-24
    • 1970-01-01
    • 2017-10-12
    • 1970-01-01
    相关资源
    最近更新 更多