将箭头放在R中的现有背景图片上

时间:2014-03-31 08:14:26

标签: r plot

我想在现有的jpg上绘制拟合模型的结果。结果在0和1之间。我希望图形(作为指针)沿jpg轴很好地移动(就像下图所示)。

enter image description here

2 个答案:

答案 0 :(得分:2)

我不确定我喜欢使用现成比例并添加指针的方法;但是,如果你必须,这是一种方式。棘手的部分是让您的点绘图与您的jpeg刻度中显示的轴对齐:

实施例

首先,我做了一个例子jpeg:

library(biOps)

#make jpeg of scale
jpeg("scale.jpeg", width=4, height=1, units="in", res=200, quality = 100)
par(mar=c(4,0,0,0), ps=10)
z <- seq(0,1,by=0.1)
breaks <- c(0,0.33,0.67,1)
image(x=z, z=matrix(z, nrow=length(z), ncol=1), xlim=c(0,1), axes=FALSE, xlab="", ylab="", breaks=breaks, col=c("red","yellow", "green"))
axis(1, at=seq(0.1,0.9,0.1))
box()
dev.off()

enter image description here

然后,我加载它并使用layout将其放置在设备的下面板中,并在给定位置添加一个点(例如0.2):

#add pointer above figure
pic <- readJpeg("scale.jpeg") # load jpeg

lo <- matrix(2:1, nrow=2, ncol=1) # layout info
WIDTHS <- c(4)
HEIGHTS <- c(0.2,1)

#make new jpeg
jpeg("scale_w_pointer.jpeg", width=4, height=1.2, units="in", res=200, quality = 100)
#quartz() # or x11() to preview
layout(lo, widths=WIDTHS, heights=HEIGHTS, respect=TRUE)
layout.show(2)

#plot 1
par(mar=c(0,0,0,0))
plot(pic)

#plot 2
par(mar=c(0,0,0,0))
plot(1, xlim=c(0,1), ylim=c(0,1), t="n", xaxs="i", yaxs="i", xlab="", ylab="", axes=FALSE)
points(0.2, 0.30, pch=25, bg=1, col=1)

dev.off()

enter image description here

答案 1 :(得分:1)

这有点粗糙,但应该让你开始。使用scan,tcltk或shiny,您可以根据需要进行交互/响应和/或写入文件。

col <- c("green", "yellow", "red")
x <- matrix(col, nrow = 1L) 

plot(0, xlim = c(0, 1), ylim = c(0, 0.5), asp = 1, 
  xlab = "", ylab = "", axes = FALSE, type = "n")
axis(1, at = seq(0, 1, length = 6), line = -6, lwd = 0, lwd.tick = 1)

rasterImage(x, 0, 0, 1, 0.05, interpolate = FALSE)

p   <- cbind(c(0, 0.015, -0.015, 0) + runif(1), c(0.05, 0.065, 0.065, 0.05))
polygon(p, col = "black")