【问题标题】:Merge two different plots: one in the X-axis and the other in the Y-axis合并两个不同的图:一个在 X 轴,另一个在 Y 轴
【发布时间】:2014-08-09 12:44:07
【问题描述】:

我使用 R 独立地表示了这两个图:

#PLOT 1
    x<-250:2500
    #Hsap. Northern European
        a<-dnorm(x,1489,167)
    #Hsap. South African
        b<-dnorm(x,1472,142)
    
    plot(x,a, type="l", lwd=3, ylim=c(0,1.2*max(a,b,c)), ylab="Probability Density", xlab="Microns")
    lines(x,b, type="l", lwd=3, col="Red")

情节 2

    #CUSPAL ENAMEL FORMATION TIME
    x<-0:800
    #Hsap. Northern European
        a<-dnorm(x,447,37)
    #Hsap. South African
        b<-dnorm(x,444,33)
    
    plot(x,a, type="l", lwd=3, ylim=c(0,1.2*max(a,b,c)), ylab="Probability Density", xlab="Days")
    lines(x,b, type="l", lwd=3, col="Red")
![enter image description here][2]

我想使用 R 合并两者并获得类似于下图所示的图像。有趣的是,我也想突出 +- 1SD 的间隔,以查看两个图中的重叠区域。

R 中实现我的目标的确切代码是什么?

更新

现在,根据我的数据,我得到了下一个数字:

如您所见,重叠的标准差并不是最佳位置。我希望这些重叠区域高于 X 轴的正态分布。这样我就可以看清楚了。

所以问题是,你能写一些例子,这样我就可以学习如何移动这些秤来避免这种情况?

在我的示例中,我想向上移动右正态分布(Y 轴)。

x1<-30:200
a1<-dnorm(x1,87,15)
b1<-dnorm(x1,89,13)
c1<-dnorm(x1,92,16)
d1<-dnorm(x1,104,15)

x2<-000:1600
a2<-dnorm(x2,724,66)
b2<-dnorm(x2,724,50)
d2<-dnorm(x2,835,117)

scale<-range(pretty(range(a1,a2,b1,b2,c1,d1,d2)))

remap<-function(x, to, from=range(x)) {
    (x-from[1]) / (from[2]-from[1]) * (to[2]-to[1]) + to[1] 
}

plot(NA, NA, xaxt="n", yaxt="n", type="n", xlim=scale, ylim=scale)
rect(remap(87-15, scale, range(x1)), scale[1],
     remap(87+15, scale, range(x1)), scale[2], col="#ff606025", lty=1)
rect(remap(89-13, scale, range(x1)), scale[1],
     remap(89+13, scale, range(x1)), scale[2], col="#ff606025", lty=2)
rect(remap(92-16, scale, range(x1)), scale[1],
     remap(92+16, scale, range(x1)), scale[2], col="#3dae0025", lty=0)
rect(remap(104-15, scale, range(x1)), scale[1],
     remap(104+15, scale, range(x1)), scale[2], col="#005ccd25", lty=0)

rect(scale[1], remap(724-66, scale, range(x2)),
     scale[2], remap(724+66, scale, range(x2)), col="#ff606025", lty=1)
rect(scale[1], remap(724-50, scale, range(x2)),
     scale[2], remap(724+50, scale, range(x2)), col="#ff606025", lty=2)
rect(scale[1], remap(835-117, scale, range(x2)),
     scale[2], remap(835+117, scale, range(x2)), col="#005ccd25", lty=0)

lines(remap(x1,scale), a1, col="darkred", lwd=3)
lines(remap(x1,scale), b1, col="darkred", lwd=3, lty=3)
lines(remap(x1,scale), c1, col="darkgreen", lwd=3)
lines(remap(x1,scale), d1, col="darkblue", lwd=3)

lines(scale[2]-a2, remap(x2,scale), col="darkred", lwd=3)
lines(scale[2]-b2, remap(x2,scale), col="darkred", lwd=3, lty=3)
lines(scale[2]-d2, remap(x2,scale), col="darkblue", lwd=3)


axis(2); axis(3)
axis(1, at=remap(pretty(x1), scale), pretty(x1))
axis(4, at=remap(pretty(x2), scale), pretty(x2))

谢谢

【问题讨论】:

    标签: r normal-distribution data-visualization standard-deviation


    【解决方案1】:

    棘手的是每个图只能保存一个坐标系。所以我们需要做的只是将所有值缩放到相同的范围,然后我们可以在轴上做一些捏造以返回到正确的比例。这是一个完整的解决方案

    x1<-250:2500
    a1<-dnorm(x1,1489,167)
    b1<-dnorm(x1,1472,142)
    
    x2<-0:800
    a2<-dnorm(x2,447,37)
    b2<-dnorm(x2,444,33)
    
    scale<-range(pretty(range(a1,a2,b1,b2)))
    
    remap<-function(x, to, from=range(x)) {
        (x-from[1]) / (from[2]-from[1]) * (to[2]-to[1]) + to[1] 
    }
    
    
    plot(NA, NA, xaxt="n", yaxt="n", type="n", xlim=scale, ylim=scale, xlab="", ylab="")
    rect(remap(1489-167, scale, range(x1)), scale[1],
        remap(1489+167, scale, range(x1)), scale[2], col=rgb(0,0,0,.25), lty=0)
    rect(remap(1472-142, scale, range(x1)), scale[1],
        remap(1472+142, scale, range(x1)), scale[2], col=rgb(1,0,0,.25), lty=0)
    
    rect(scale[1], remap(447-37, scale, range(x2)),
        scale[2], remap(447+37, scale, range(x2)), col=rgb(0,0,0,.25), lty=0)
    rect(scale[1], remap(444-33, scale, range(x2)),
        scale[2], remap(444+33, scale, range(x2)), col=rgb(1,0,0,.25), lty=0)
    
    lines(remap(x1,scale), a1, col="black", lwd=3)
    lines(remap(x1,scale), b1, col="red", lwd=3)
    lines(scale[2]-a2, remap(x2,scale), col="black", lwd=3)
    lines(scale[2]-b2, remap(x2,scale), col="red", lwd=3)
    axis(2); axis(3)
    axis(1, at=remap(pretty(x1), scale, range(x1)), pretty(x1))
    axis(4, at=remap(pretty(x2), scale, range(x2)), pretty(x2))
    

    产生

    【讨论】:

    • 哇!。这才是我真正想要的!非常感谢弗里克先生。只有一件事,我怎么写轴的名字?例如下轴的变量 1 和左轴的变量 2?
    • 还有一件事。希望你能在我上一条评论的同时做到这一点,就在这条评论之上。想象在 x1 中我们有其他人口,但在这种情况下它只有一个数字,因此我们没有样本可以说它表现为正态分布。那么,怎么可能画一条垂直线,比如x轴的值为1000呢?
    • 要绘制轴标签,请查看?title 帮助页面;要绘制直线,请查看?abline 帮助页面。
    • 我的第一条评论是自己解决的。我只需要看到代码和标签为空...
    • 我会查看abline的帮助页面。然而。如何在图表中放置一个点,该点由 X 轴的一个值(变量 1)和 Y 轴的一个值(变量 2)定义。以及如何将其命名为接近图表中的点?感谢您的帮助 MrFlick。
    猜你喜欢
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    • 2017-08-01
    • 1970-01-01
    • 2017-03-28
    • 2016-10-27
    • 2022-09-26
    • 1970-01-01
    相关资源
    最近更新 更多