简体   繁体   English

合并两个不同的图:一个在X轴上,另一个在Y轴上

[英]Merge two different plots: one in the X-axis and the other in the Y-axis

I have the represented independently these two plots using R: 我使用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")

在此处输入图片说明

PLOT 2 情节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]

I would like to merge both using R and obtain an image similar to that shown below. 我想使用R合并两者,并获得类似于下面所示的图像。 It is interesting to say that I would like as well to stand out the intervals of +- 1SD, to see the overlapping area in the two plots. 有趣的是,我也想突出±1SD的间隔,以查看两个图中的重叠区域。 在此处输入图片说明

Which is the exact code in R to get into success my purpose? R上成功实现目标的确切代码是什么?

UPDATE UPDATE

Now, with my data I get the next figure: 现在,根据我的数据,我得到下一个数字:

在此处输入图片说明

As you can see, the overlapping standard deviations are not in the best place. 如您所见,重叠的标准偏差不在最佳位置。 I'd like these overlapping areas were above the normal distributions placed at the X-axis. 我希望这些重叠区域位于X轴上的正态分布之上。 In this way I could see clearly them. 这样,我可以清楚地看到它们。

So the question is, can you write some examples and in that way I learn how I can move these scales to avoid that situation? 所以问题是,您能写一些例子吗,以这种方式我学会了如何移动这些秤以避免这种情况?

In my example I would like to move upwards the right-normal distributions (Y-axis). 在我的示例中,我想向上移动右正态分布(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))

Thanks 谢谢

The tricky thing is each plot can only hold one coordinate system. 棘手的是,每个图只能容纳一个坐标系。 So what we need to do are just scale all the values to the same range, then we can do some fudging on the axis to return to the correct scale. 因此,我们要做的只是将所有值缩放到相同的范围,然后我们可以在轴上进行一些修整操作以返回到正确的缩放比例。 Here's a complete solution 这是一个完整的解决方案

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

which produces 产生

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在具有一个x轴和两个y轴的图中将误差线缩放到右y轴的尺寸 - How to scale the errorbars to the dimensions of the right y-axis in a plot with one x-axis and two y-axis 相同的y轴,但不同的x轴图- - same y-axis but different x-axis graph - 一个 y 轴与 R 中的共享 x 轴 - one y-axis with shared x-axis in R R基图:在1个坐标图的x轴和y轴上绘制2个密度图 - R baseplot :plotting 2 density plots in 1 plot cand hanging scales for x-Axis and y-Axis 在ggplot列表中循环遍历x轴和y轴变量并打印图 - Loop over both the x-axis and y-axis variables in ggplot list and print plots ggplot - 具有 2 和 3 y 轴的对齐图的相同 x 轴长度 - ggplot - Same x-axis length for aligned plots with 2 and 3 y-axis R 多个绘图,具有通用 y 轴缩放和共享 x 轴 label - R multiple plots with common y-axis scaling and shared x-axis label 是否有一个 function 绘制了一个条形图,x 轴上有年份,y 轴上有值? - Is there a function that plots a barplot having years on the x-axis and values on the y-axis? 我想做一个条形图 plot 在 y 轴上有两个不同的值,在 x 轴上有两个不同的值 - I would like to do a bar plot with two different values in the y-axis and those being dodged in the x-axis 如何在 ggplot 上制作一个箱线图,其中我的 x 轴有两列,我的 y 轴有两列? - How to make a boxplot on ggplot with two columns for my x-axis and two columns for my y-axis?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM