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

ravi rv15i at yahoo.se
Sat Aug 22 10:50:00 CEST 2015


Hi,I havetried to find a simple way for my overlay plot. A simple summary of myapproach would to first plot the raster image and then overlay the second pot with :
par(new=TRUE,plt=c(x1,x2,y1,y2)) # not sure if the plt or usr argument should be used# have some difficulty in knowing the units for x1,x2,y1 and y2
plot(1:10,1:10)#justan example plot
But my problem is that I do not understand the unit conversions for graphics. What exactly are "npc" and "native"?Let me explain by starting with the code again:#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))
plot(x, y, ann=FALSE,xaxs="i", yaxs="i")
rasterImage(image,xrange[1], yrange[1],xrange[2], yrange[2],interpolate=FALSE)# let's say that I want to draw an overlay plot with the follwing line as x-axis
lines(c(-10,10),c(-12,-12),col="red",lwd=2)

lim<-par("usr")
lim
limplt<-par("plt")
limplt

#convertX(unit(0:1,"npc"),"native")
convertX(unit(-10,"native"),"npc")
convertX(unit(10,"native"),"npc")
convertY(unit(-12,"native"),"npc")
> lim<-par("usr")
> lim
[1] -15  15 -15  15
> limplt<-par("plt")
> limplt
[1] 0.09132251 0.95322506 0.18000000 0.85529412
> 
> #convertX(unit(0:1,"npc"),"native")
> convertX(unit(-10,"native"),"npc")
[1] -0.0116009280742459npc
> convertX(unit(10,"native"),"npc")
[1] 0.0116009280742459npc
> convertY(unit(-12,"native"),"npc")
[1] 1.02205882352941npc


These converted values and the values for the limplt variable do not seem to be in agreement. How do I convert, for example, the point (10,-12) to "npc" units?

I hope that I have explained my problem sufficiently well. If I can get help with an approach involving viewports also, I would be grateful for any help that I can get.
Thanks,
Ravi

      From: ravi <rv15i at yahoo.se>
 To: R-help <r-help at r-project.org> 
 Sent: Friday, 21 August 2015, 16:30
 Subject: plotting over a raster image with control over location and orientation
   
Hi,I would like to get some help in plotting over an image. I have a png image over which I would like to have a plot. I would like to be able to control the location, area and orientation of the plot on the image.
I have taken help from the following references :http://journal.r-project.org/archive/2011-1/RJournal_2011-1_Murrell.pdfhttp://stackoverflow.com/questions/12918367/in-r-how-to-plot-with-a-png-as-background
In order to give a reproducible example, I set up my image with the help of some code from the the first reference above.

#Setting up the initial example raster image
x <- y <- seq(-4*pi, 4*pi, len=27)
r <- sqrt(outer(x^2, y^2, "+"))
z <- cos(r^2)*exp(-r/6)
image <- (z - min(z))/diff(range(z))
step <- diff(x)[1]
xrange <- range(x) + c(-step/2, step/2)
yrange <- range(y) + c(-step/2, step/2)
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)

# the explanation of my problem starts here
# First, I want to mark out a particular line
lines(c(10,10.5),c(-10.5,10),col="red",lwd=2)
#In my problem, I have to locate these points graphically from the image
calpoints <- locator(n=2,type='p',pch=4,col='blue',lwd=2)
# this gives the line corresponding to the x-axis for my overlay plot
# I don't want the red line on my plot
#the red line plotted earlier is just to show the example location
newOrigin<-calpoints[1]
xLimit<-calpoints[2]#xlimit marks the limit of the x-axis on the image# on this new line as the x-axis, I want to make a new plot# the y-axis should be perpendicular to the x-axis. I would like to be able to specify the width of coverage over the image#example
xx<-1:10
yy<-xx^2
plot(xx,yy,xlim=range(xx),ylim=range(yy),col="blue",type="b",xlab="x",ylab="square of x")
# I would prefer to have the image more transparent just under the x and y labels and axis labelsThanks, Ravi



   
	[[alternative HTML version deleted]]



More information about the R-help mailing list