使用draw.colorkey更改colorkey标签的字体

时间:2018-02-17 18:40:59

标签: r ggplot2

我有以下示例代码:

require(latticeExtra)
require(gridExtra)
require(ggplot2)
require(grid)


lay <- matrix(c(1,1,1,2), 1,4,byrow = FALSE)

z<-sort(rnorm(100))
x<- rep(seq(1,10), times=10)
y<-rep(seq(1,10), each=10)
p<- levelplot(z ~ x * y,
          par.settings=list(axis.text=list(fontfamily="serif"),),
          colorkey = list(labels=list(par.settings=list(fontfamily='serif'))))

leg.list <- p$legend$right$args$key
leg <-  draw.colorkey(leg.list)
grid.arrange(grobs = list(p, leg), layout_matrix = lay)

当我运行此代码时,我得到以下图像: output of above code

我希望little colorkey(levelplot的一部分)中的标签字体与大colorkey中的标签字体相匹配(使用draw.colorkey()制作)。我怎么能这样做?

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:1)

试试这个:

leg <-  draw.colorkey(leg.list)
leg$children[[4]]$children[[1]]$gp$cex <- 0.75
grid.arrange(grobs = list(p, leg), layout_matrix = lay)

enter image description here

为两个颜色键设置相同的字体系列,我们得到了更好的结果:

leg <-  draw.colorkey(leg.list)
leg$children[[4]]$children[[1]]$gp$cex <- 0.8
leg$children[[4]]$children[[1]]$gp$fontfamily <- "serif"
grid.arrange(grobs = list(p, leg), layout_matrix = lay)

enter image description here