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

Paul Murrell paul at stat.auckland.ac.nz
Tue Aug 25 03:36:45 CEST 2015


Hi

The main problem with the code you have below is that it mixes drawing 
based on the 'graphics' package with drawing based on the 'grid' package 
and those packages do not mix easily.

I am not clear on what you want as your final result, but I wonder 
whether you need to just set up the plotting region how you would like 
it (plot.new(), plot.window()) and then position the raster within that 
(with rasterImage()) and then draw some more stuff on top (lines(), 
text(), ...).

Paul

On 08/23/15 00:32, ravi wrote:
> 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]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

-- 
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/



More information about the R-help mailing list