使用缩放轴和偏移轴在绘图中添加单个点?

时间:2015-02-20 08:49:14

标签: r plot point

我有下一个代表情节的代码。

x1<-0:200
a1<-dnorm(x1,88.24,15)
b1<-dnorm(x1,92,16)
c1<-dnorm(x1,104,15)

x2<-400:1200
a2<-dnorm(x2,747.88,65.9792)
c2<-dnorm(x2,835,117)

scale<-range(pretty(range(a1,a2,b1,c1,c2)))

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="X", ylab="Y")

rect(remap(83, scale, range(x1)), scale[1],
     remap(93, scale, range(x1)), scale[2], col="#ff606025", lty=0)
rect(remap(82, scale, range(x1)), scale[1],
     remap(102, scale, range(x1)), scale[2], col="#3ca90025", lty=0)
rect(remap(67, scale, range(x1)), scale[1],
     remap(141, scale, range(x1)), scale[2], col="#3847ea25", lty=0)
rect(scale[1], remap(729, scale, range(x2)),
     scale[2], remap(767, scale, range(x2)), col="#ff606025", lty=0)
rect(scale[1], remap(544, scale, range(x2)),
     scale[2], remap(1126, scale, range(x2)), col="#3847ea25", lty=0)

lines(remap(x1,scale), a1, col="#ff6060", lwd=2)
lines(remap(x1,scale), b1, col="#3ca900", lwd=2)
lines(remap(x1,scale), c1, col="#3847ea", lwd=2)

lines(scale[2]-a2, remap(x2,scale), col="#ff6060", lwd=2)
lines(scale[2]-c2, remap(x2,scale), col="#3847ea", lwd=2)

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

您可以看到下面显示的内容: enter image description here

如何在此图中添加单个点?例如,当点为X = 100且Y = 600时。

提前致谢

2 个答案:

答案 0 :(得分:1)

试试这个:

points( x=100*diff(scale)/diff(range(x1)), #could also use remap(100, scale, range(x1))
        y=remap(600, scale, range(x2) ), 
    col="red", cex=3)

答案 1 :(得分:0)

使用单个点或对现有数据集的查询创建单独的数据集,该数据集返回数据集中的单个点,并将其传递给points函数。

请在此处查看点数功能:http://www.inside-r.org/r-doc/graphics/points