Hello list,
I've always wanted to figure out how to put legend captions in my
levelplots, and so when I came across this example today (from
http://r.789695.n4.nabble.com/scale-caption-on-levelplot-td3072400.html):
library(grid)
x <- sort(rnorm(100,50,10))
y <- sort(runif(100,0,20))
d <- expand.grid(x=x, y=y)
d$z <- x + y
graphics.off()
p <- levelplot(z ~ x*y, d,
par.settings=list(
layout.widths=list(right.padding=4),
layout.heights=list(top.padding=6)),
colorkey = TRUE)
print(p)
## now add the text
grid.text('here and there', x=.98, y=.5, rot=-90,
gp = gpar(col=4,
fontfamily="HersheyGothicEnglish", cex=2))
This works and is fine, except in my code I use the layer() function and
save to a png, which changes the code as such:
library(grid)
library(latticeExtra)
x <- sort(rnorm(100,50,10))
y <- sort(runif(100,0,20))
d <- expand.grid(x=x, y=y)
d$z <- x + y
graphics.off()
p <- levelplot(z ~ x*y, d,
par.settings=list(
layout.widths=list(right.padding=4),
layout.heights=list(top.padding=6)),
colorkey = TRUE)
#print(p)
## now add the text layer
p <- p + layer(grid.text('here and there', x=.98, y=.5, rot=-90,
gp = gpar(col=4,
fontfamily="HersheyGothicEnglish", cex=2)))
png("output.png")
plot(p)
dev.off()
Now, with these changes, the text moves inside the plot region, instead of
outside in the margins. It seems like once the grid.text is converted by
the layer(), the x and y positions become relative to the plot region
instead of the whole graph, and any values of x and y larger than 1 will
move the text out of view.
So, is there a way I can use layer() to add text in the margins outside the
plot region? Or are there suggestions on other ways in which I could add
text to a plot margin?
Thanks,
Jooil
--
#############################################
Jooil Kim
Postdoc Fellow
Scripps Institution of Oceanography, UC San Diego
Mailing address:
9500 Gilman Drive # 0244
La Jolla, CA 92093-0244, USA
For FedEx/UPS:
8675 Discovery Way, Vaughan Hall Rm. 447
La Jolla, CA 92037, USA
jooilkim at ucsd.edu (kji2080 at gmail.com)
tel)+1-858-534-2599
fax)+1-858-455-8306
[[alternative HTML version deleted]]