Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

base::plot -- can I retrieve the as-plotted aspect ratio?

Tags:

plot

r

I know I can specify the aspect ratio when plotting, e.g. plot(x,y,asp=5) . Is there any way to retrieve the aspect ratio after allowing autoscale (as in plot(x,y)) ? The reason I ask is that I was playing with text(x,y,'mystring',srt=local_slope) , where I calculate the local_slope based on the underlying curve and the x value of interest. Trouble is, for asp!=1 this rotates the text at a different angle from the displayed slope of my plotted data set. Sample:

x<- -10:10
y<- x^2
plot(x,y,t='l',asp=0.1) 
# the slope at x=1 is 2 but the default plot aspect ratio is far from 1:1
text(1,1,'foo',srt= 180/pi*atan(2) )  #ugly-looking
text(-1,1,'bar',srt= (180/pi*atan(2/10))) #better
like image 835
Carl Witthoft Avatar asked Mar 21 '23 21:03

Carl Witthoft


1 Answers

x<- -10:10
y<- x^2
plot(x,y,t='l',asp=0.1) 
### the slope at x=1 is 2 but the default plot aspect ratio is far from 1:1
text(1,1,'foo',srt= 180/pi*atan(2) )  #ugly-looking
text(-1,1,'bar',srt= (180/pi*atan(2/10))) #better

Get width and height of plotting region in inches ...

ff <- par("pin")
ff[2]/ff[1]  ## 1.00299

Now resize the plot manually ...

ff <- par("pin")
ff[2]/ff[1]  ## 0.38

You can also use par("usr") to sort out the aspect ratio in user units, but I haven't figured out quite the right set of ratios ... the guts of MASS::eqscplot might be enlightening too.

like image 159
Ben Bolker Avatar answered Mar 23 '23 13:03

Ben Bolker