[R] Coordinate or top left corner + offset

Ben Bolker bbolker at gmail.com
Tue Feb 10 00:42:38 CET 2015


David L Carlson <dcarlson <at> tamu.edu> writes:

> 
> This is more complicated, but it could be rolled up into a function.
Replace your mtext() call with the following:
> 
> # Set character expansion size
> cx <- 2.5
> # Get the plot coordinates and the character size
> ur <- par("usr")[c(1, 4)]
> chr <- par("cxy")
> rect(ur[1]+chr[1]/10, ur[2]-chr[2]*cx, ur[1]+chr[1]*cx, ur[2]-chr[1]/10, 
>      border=NA, col="white")
> text(ur[1]+chr[1]*cx/2, ur[2]-chr[2]*cx/2, "a", font=2, cex=2.5, col="red")
> 
> 1) Assign to cx the cex= value that you are using in text().
> 2) Then get the upper right corner of the plot window and the size of the
default character width in user
> coordinate units.
> 3) Draw a white rectangle the size of the character you are plotting (in
this case cex=2.5). Shrink the left
> and top edge so that the box around the plot area is not obscured.
> 4) Plot your character in the center of the box.
> 

  There are two more tricks you can use here:

  (1) cheat by using legend()

plot(0:10,0:10)
legend("topleft",legend=NA,title="hello",bty="n")

  (2) use plotrix::corner.label



More information about the R-help mailing list