简体   繁体   中英

How to create lattice plot title with new line, italics and variable names in R

I've been trying to add a title (via main) to a map created with spplot that consists of two lines of text, but with the second line italicised. The text in each line is drawn from a variable name. I've variously tried using bquote and expression, but with no luck.

A basic example of my starting point is below, although in my actaul usage the text for a and b are drawn iteratively from a vector as part of a loop. Any suggestions as to how to keep the two lines split as per the example, but with the second line of text italicised would be much appreciated. I hope the example is clear. Thanks in advance.

require(sp)
data(meuse)
coordinates(meuse)<-~x+y

a<-"line 1"
b<-"line 2"
title<-paste(a,'\n',b,sep="")

spplot(meuse,c("ffreq"),col.regions="black",main=title)
spplot(meuse, c("ffreq"), col.regions="black",
               main= expression( atop(line~1, italic(line~2) ) )  )

If you really wanted to isolate the a="line 1" and b="line 2" variables and "pass them in", you could use this form:

spplot(meuse, c("ffreq"), col.regions="black",
             main= as.expression( bquote( atop(.(a), italic(.(b) ) ) ) ) )

I will note that the value returned by bquote() is not actually an expression but rather a "language"-object. Deepayan Sarkar, lattice's author, is of the opinion that these do not deserve being considered as expressions in the lattice framework. Using bquote without the as.expression wrapper is generally acceptable in base graphics and in ggplot2, but will fail in lattice calls. I initially (unsuccessfully) tried:

 spplot(meuse, c("ffreq"), col.regions="black",
             main=  bquote( atop(.(a), italic(.(b) ) ) ) ) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM