问题
I need my title on the graph to look like the following
Main
Submain
where main is of a different font size than submain, right now I have it as main="Main \n submain" but how do I specify different sizes. If not, how do I add another title
回答1:
Neither of those tasks are super easy with lattice; but that doesn't mean it's impossible. In addition to accepting character values for the main=
parameter, it can also accept an arbitrary grid
object (or grob
). Lattice is build on the grid
graphics library so you can customize many things before plotting. Here's one solution that uses a helper function to create grid
object with two rows of text.
library(lattice)
library(grid)
doubleTitle <- function(a,b) {
gTree(children=gList(
textGrob(a, gp=gpar(fontsize=15, fontface=2), y=0,
vp=viewport(layout.pos.row=1, layout.pos.col=1)),
textGrob(b, gp=gpar(fontsize=13, fontface=3), y=0,
vp=viewport(layout.pos.row=2, layout.pos.col=1))
), vp=viewport(layout=grid.layout(nrow=2, ncol=1)), cl="doubletitle")
}
heightDetails.doubletitle <- function(x, recording=T) {
Reduce(`+`, lapply(x$children, grid:::heightDetails.text)) * 2
}
And we can use it with
xyplot(1:10~1:10, main=doubleTitle("Main","Submain"))
You can clearly see the fontsize=
options. For fontface=
, 1 is normal, 2 is bold, 3 is italic, and 4 is bold+italic. To change the spacing between rows, adjust the value of 2
in the heightDetails.doubletitle
function.
来源:https://stackoverflow.com/questions/25019075/r-lattice-title-font-size