Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Screen coordinates in R

Tags:

r

graphics

I would like to draw two perpendicular lines on the current device. They should both have the same apparent length on the device, irrespective of aspect ratio of the output and the device size.

Is this at all doable? In principle this is the problem of drawing a perfect circle on the screen.

like image 208
January Avatar asked Feb 26 '14 15:02

January


3 Answers

You could retrieve the current graphics window coordinates. Thanks to Josh O'Brian for providing me with this code a while back. First you create a window (or plot something), then run this line:

 myasp <- with(par(),(pin[2]/pin[1])/(diff(usr[3:4])/diff(usr[1:2])))

Then use that aspect information to adjust the lengths of the lines you're going to plot.

like image 62
Carl Witthoft Avatar answered Nov 05 '22 18:11

Carl Witthoft


there are several ways, i find it easier in grid graphics

library(grid)

grid.circle()
vp <- viewport(width=unit(0.5, "snpc"), height=unit(0.5, "snpc"))
grid.rect(vp=vp, gp=gpar(lty=2))
grid.segments(x0=c(0,0), x1=c(1,1), 
              y0=c(0,1), y1=c(1,0), 
              default.units="npc", vp=vp)
like image 38
baptiste Avatar answered Nov 05 '22 19:11

baptiste


You might be interested in the grconvertX and grconvertY functions. Using these you can specify coordinates in inches or other units and from that compute the user coordinates for drawing your line.

like image 1
Greg Snow Avatar answered Nov 05 '22 18:11

Greg Snow