R lattice title font size

ε祈祈猫儿з 提交于 2020-01-14 06:06:13

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!