R-如何将图例标题添加到保存到变量的关卡中? [英] R - How to add legend title to levelplot saved to a variable?

查看:125
本文介绍了R-如何将图例标题添加到保存到变量的关卡中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在保存到变量的水平图图中添加图例的标题.

例如,此代码有效:

library(lattice)
library(grid)
x = 1:10
y = rep(x,rep(10,10))
x = rep(x,rep(10))
z = x+y  
levelplot(z~x*y, colorkey=list(labels=list(cex=1,font=2,col="brown"),height=1,width=1.4),main=list('b',side=1,line=0.5))
trellis.focus("legend", side="right", clipp.off=TRUE, highlight=FALSE)
grid.text(expression(m^3/m^3), 0.2, 0, hjust=0.5, vjust=1)
trellis.unfocus()

但是此代码(将相同的图另存为变量)不起作用:

p1 <- levelplot(z~x*y, colorkey=list(labels=list(cex=1,font=2,col="brown"),height=1,width=1.4),main=list('b',side=1,line=0.5))
trellis.focus("legend", side="right", clipp.off=TRUE, highlight=FALSE)
grid.text(expression(m^3/m^3), 0.2, 0, hjust=0.5, vjust=1)
trellis.unfocus()

我该如何实现?

解决方案

这不能直接回答您的问题,但可能会改变工作流程,您可以使用colorkey参数在键中添加标题. /p>

它涉及调整draw.colorkey函数.

最简单的方法是交互使用fixInNamespace

fixInNamespace("draw.colorkey", "lattice")

在函数末尾将最后几行更改为

    }
    if (!is.null(key$title)) {
        key.gf <- placeGrob(key.gf, textGrob(key$title, hjust = key$hjust, 
            vjust = key$vjust, gp = key$gp), row = key$row, col = key$column)
    }
    if (draw) 
        grid.draw(key.gf)
    key.gf
}

保存并关闭,然后可以如下所示使用.


但是,您可能无法交互地执行此操作,因此也可以按照以下方式完成

library(lattice)
library(grid)

# Amend key function
# Hopefully a nicer way to do this!
mykey <- draw.colorkey

body(mykey)[28:30] <- list(
quote(
  if(!is.null(key$title)){
      key.gf <- placeGrob(key.gf,
                      textGrob(key$title,hjust=key$hjust, vjust=key$vjust, gp=key$gp),
                          row=key$row, col=key$column)
  }),
body(mykey)[[28]], 
body(mykey)[[29]])

# Assign to namespace: http://stackoverflow.com/questions/6254744/override-a-function-that-is-imported-in-a-namespace
unlockBinding("draw.colorkey", as.environment("package:lattice"))
assign("draw.colorkey", mykey, "package:lattice")
unlockBinding("draw.colorkey", getNamespace("lattice"))
assign("draw.colorkey", mykey, getNamespace("lattice"))

然后您可以传递一个关键标题,指定位置

# Draw plot
x = 1:10
y = rep(x,rep(10,10))
x = rep(x,rep(10))
z = x+y 
p <- levelplot(z~x*y, 
              colorkey=list(labels=list(cex=1, font=2, col="brown"),
                            height=1, width=1.4,
                            title=expression(m^3/m^3), row=3, column=1, vjust=2),
               main=list('b',side=1,line=0.5))


p

哪个生产

I would like to add the title for a legend in a levelplot graph saved to a variable.

For example, this code works:

library(lattice)
library(grid)
x = 1:10
y = rep(x,rep(10,10))
x = rep(x,rep(10))
z = x+y  
levelplot(z~x*y, colorkey=list(labels=list(cex=1,font=2,col="brown"),height=1,width=1.4),main=list('b',side=1,line=0.5))
trellis.focus("legend", side="right", clipp.off=TRUE, highlight=FALSE)
grid.text(expression(m^3/m^3), 0.2, 0, hjust=0.5, vjust=1)
trellis.unfocus()

But this code, where the same plot is being saved as a variable, does NOT work:

p1 <- levelplot(z~x*y, colorkey=list(labels=list(cex=1,font=2,col="brown"),height=1,width=1.4),main=list('b',side=1,line=0.5))
trellis.focus("legend", side="right", clipp.off=TRUE, highlight=FALSE)
grid.text(expression(m^3/m^3), 0.2, 0, hjust=0.5, vjust=1)
trellis.unfocus()

How can I achieve this?

解决方案

This doesn't answer your question directly, but it perhaps offers a change in workflow, where you can add a title to the key using the colorkey arguments.

It involves tweaking the draw.colorkey function.

The easiest way is to use fixInNamespace interactively

fixInNamespace("draw.colorkey", "lattice")

at the end of the function change the last few lines to

    }
    if (!is.null(key$title)) {
        key.gf <- placeGrob(key.gf, textGrob(key$title, hjust = key$hjust, 
            vjust = key$vjust, gp = key$gp), row = key$row, col = key$column)
    }
    if (draw) 
        grid.draw(key.gf)
    key.gf
}

Save and close, and you can then use as shown below.


However, you may not be able to do this interactively, so it can also be done as

library(lattice)
library(grid)

# Amend key function
# Hopefully a nicer way to do this!
mykey <- draw.colorkey

body(mykey)[28:30] <- list(
quote(
  if(!is.null(key$title)){
      key.gf <- placeGrob(key.gf,
                      textGrob(key$title,hjust=key$hjust, vjust=key$vjust, gp=key$gp),
                          row=key$row, col=key$column)
  }),
body(mykey)[[28]], 
body(mykey)[[29]])

# Assign to namespace: http://stackoverflow.com/questions/6254744/override-a-function-that-is-imported-in-a-namespace
unlockBinding("draw.colorkey", as.environment("package:lattice"))
assign("draw.colorkey", mykey, "package:lattice")
unlockBinding("draw.colorkey", getNamespace("lattice"))
assign("draw.colorkey", mykey, getNamespace("lattice"))

You can then pass a key title, specifying the position

# Draw plot
x = 1:10
y = rep(x,rep(10,10))
x = rep(x,rep(10))
z = x+y 
p <- levelplot(z~x*y, 
              colorkey=list(labels=list(cex=1, font=2, col="brown"),
                            height=1, width=1.4,
                            title=expression(m^3/m^3), row=3, column=1, vjust=2),
               main=list('b',side=1,line=0.5))


p

Which produces

这篇关于R-如何将图例标题添加到保存到变量的关卡中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆