[R] plotting over a raster image with control over location and orientation

ravi rv15i at yahoo.se
Sat Aug 22 14:32:03 CEST 2015


Hi,I am sorry for not having control checked the code before posting (in my earlier mail). I have done this now. 
I just have a simple question now. Why is the overlay plot (the final plot ) not falling into the intended position (as defined by the viewport vp2)?#Setting up the initial example raster image
library(grid)
x <- y <- c(-15,seq(-4*pi, 4*pi, len=27),15)
r <- sqrt(outer(x^2, y^2, "+"))
z <- cos(r^2)*exp(-r/6)
image <- (z - min(z))/diff(range(z))
xrange <- range(x)
yrange <- range(y)

vp1<-viewport(x = unit(0.5, "npc"), y = unit(0.5, "npc"),
         width = unit(1, "npc"), height = unit(1, "npc"),
         default.units = "npc", just = c("left","bottom"),name="vp1")
pushViewport(vp1)
plot(x, y, ann=FALSE,xlim=xrange, ylim=yrange,xaxs="i", yaxs="i")
rasterImage(image,xrange[1], yrange[1],xrange[2], yrange[2],interpolate=FALSE)
lines(c(-10,4),c(-12,-12),col="red",lwd=2)

lim<-par("usr")
lim
limplt<-par("plt")
limplt
#par(new=TRUE,plt=c(limplt[1],limplt[2],limplt[3]))

#convertX(unit(0:1,"npc"),"native")
x1<-convertX(unit(-10,"native"),"npc")
x2<-convertX(unit(5,"native"),"npc")
y1<-convertY(unit(-12,"native"),"npc")
y2<-convertY(unit(3,"native"),"npc")

wx<-(5-(-10))/(15-(-15))
wy<-(3-(-12))/(15-(-15))

xw<-convertWidth(unit(wx,"native"),"npc")
yw<-convertHeight(unit(wy,"native"),"npc")


vp2<-viewport(x = unit(x1, "npc"), y = unit(y1, "npc"),
              width = unit(xw, "npc"), height = unit(yw, "npc"),
              default.units = "npc", name="vp2")
pushViewport(vp2)
#data with a different scale
xs<-seq(0.01,0.1,0.01)ys<-xs^2
#points(xs,ys,type='b',col='red',newpage=FALSE)

plot(xs,ys,type='b',col='red') #fills the whole screen instead of just the intended viewport


Thanks,Ravi


   

  
	[[alternative HTML version deleted]]



More information about the R-help mailing list