ravi
2015-Aug-21 14:30 UTC
[R] 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]]
ravi
2015-Aug-22 08:50 UTC
[R] plotting over a raster image with control over location and orientation
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]]
ravi
2015-Aug-22 12:32 UTC
[R] plotting over a raster image with control over location and orientation
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]]
Greg Snow
2015-Aug-24 17:31 UTC
[R] plotting over a raster image with control over location and orientation
For base plotting functions (not grid) then you may be interested in the updateusr function in the TeachingDemos package. If you can find the current coordinates of 2 points on the 1st plot (the background image) that are not in the same horizontal or vertical line (use the locator function if nothing else) and you know what you would like the coordinates of the 2 points to be, then you just call updateusr with the current coordinates and the desired coordinates and it resets them for you so that you can add to the current plot with lines or other low level functions. On Fri, Aug 21, 2015 at 8:30 AM, ravi <rv15i at yahoo.se> wrote:> 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]] > > ______________________________________________ > 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.-- Gregory (Greg) L. Snow Ph.D. 538280 at gmail.com