[R] Coordinate or top left corner + offset
David L Carlson
dcarlson at tamu.edu
Mon Feb 9 21:49:15 CET 2015
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.
-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352
-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Pascal A. Niklaus
Sent: Monday, February 9, 2015 10:27 AM
To: r-help at r-project.org
Subject: [R] Coordinate or top left corner + offset
Dear all,
I am struggling to add annotations to panels of a series of plots
arranged on a page.
Basically, I'd like to add letters enumerating the panels
("a","b","c",...), at a fixed distance from the top left corner of the
plot's "box".
I succeeded partly with "mtext" (see below), but the "at" option is in
user coordinates, which makes is difficult to specify a given offset
from the corner (e.g. 1cm from top and left).
I tried grid's "npc" but these coordinates refer to the entire plot
instead of the current inner plotting region.
Phrased differently, I'd like to place text (and ideally also be able to
plot, e.g. a white disc to cover background items) at position
(top-1cm,left+1cm)
Here is a minimum working example illustrating what I try to achieve:
pdf("example.pdf",width=15,height=15)
m <- rbind( c(0.1,0.9,0.1,0.6),
c(0.1,0.9,0.6,0.9)
);
split.screen(m)
screen(1);
par(mar=c(0,0,0,0));
plot(rnorm(10),rnorm(10),xlim=c(-5,5),xaxt="n",yaxt="n");
mtext(quote(bold(a)),side=3,line=-2.5,at=-5,cex=2.5)
screen(2);
par(mar=c(0,0,0,0));
plot(rnorm(10),rnorm(10),xlim=c(-3,3),xaxt="n",yaxt="n");
mtext(quote(bold(a)),side=3,line=-2.5,at=-3,cex=2.5)
close.screen(all.screens=TRUE)
dev.off()
Thanks for your help
Pascal Niklaus
______________________________________________
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.
More information about the R-help
mailing list