简体   繁体   中英

R lattice title font size

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

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.

While I strongly prefer MrFlick solution, I want to add another possibility based on the fact that main= can also accept expression arguments

xyplot(1:10~1:10, 
  main = expression(atop(bold(Main~Title), italic(scriptstyle(Sub~Title))))
)

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