如何在直接标签中配置box.color“draw.rects”? [英] How can I configure box.color in directlabels "draw.rects"?

查看:196
本文介绍了如何在直接标签中配置box.color“draw.rects”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的工作示例:

  library(ggplot2)
library(directlabels)#ver 2014.6.13经由r-forge
DF< - expand.grid(z = seq(1,3001,by = 10),k = seq(from = 0.5,to = 5,by = 0.25))

#定义每个zk组合的函数值
DF $ dT < - 与(DF,-0.07 * z *(1 / 2.75 - 1 / k))

p < - ggplot(DF,aes(x = z,y = k,z = dT))+ theme_bw()+
stat_contour(aes(color = .. level ..),breaks = c(seq (从= -40到= 0,by = 5),c(seq(从= 5,到= 150,by = 10))))
angled.boxes< --list(far.from。 others.borders,calc.boxes,enlarge.box,draw.rects)
direct.label(p,angled.boxes)

看起来像这样:


我想将标签的框边框颜色变成白色,但我看不到如何去做。在新闻的包装,这是写:


2.5 --- 2012年4月6日



使用可配置的颜色draw.rects,默认为黑色。

以angled.boxes :

  angled.boxes<  -  list(far.from.others.borders,calc.boxes,我想这是可能的,但是怎么做?  
> p>

解决方案

这似乎是一个黑客,但它的工作。我刚刚重新定义了 draw.rects 函数,因为我看不到如何将参数传递给它,因为直接标签调用它的函数的笨拙方式。 (非常像ggplot-functions,我从来没有习惯把函数当作字符值):

$ $ $ $ $ $ $ $ $> assignInNamespace('draw.rects ',
函数(d,...)
{
if(is.null(d $ box.color))
d $ box.color< - red
if(is.null(d $ fill))
d $ fill< - white
for(i in 1:nrow(d)){
with(d [i,],{
grid.rect(gp = gpar(col = box.color,fill = fill),
vp = viewport(x,y,w,h,cm,c (hjust,vjust),
angle = rot))
})
}
d
},ns ='directlabels')
dlp < - direct.label(p,angled.boxes)
dlp


Here is my working example:

library(ggplot2)
library(directlabels)  # ver 2014.6.13 via r-forge
DF <- expand.grid(z = seq(1, 3001, by=10), k = seq(from=0.5, to=5, by=0.25))

# Defines the function value for each z-k combination
DF$dT <- with(DF, -0.07 * z * (1/2.75 - 1/k))

p <- ggplot(DF, aes(x = z, y = k, z = dT)) +  theme_bw() + 
 stat_contour(aes(colour=..level..), breaks=c(seq(from=-40, to=0, by=5), c(seq(from=5, to=150, by=10))))
angled.boxes <- list("far.from.others.borders","calc.boxes","enlarge.box","draw.rects")
direct.label(p, "angled.boxes")

Which looks like this:

I want to turn the labels' box border colour to "white" but I cannot see how to do that. In the NEWS for the package, is written this:

2.5 --- 6 April 2012

draw.rects with configurable color, default black.

And seeing as "angled.boxes" is a list comprising:

angled.boxes <- list("far.from.others.borders","calc.boxes","enlarge.box","draw.rects")

I suppose it's possible, but how?

解决方案

This seems to be a hack but it worked. I just redefined the draw.rects function, since I could not see how to pass arguments to it due to the clunky way that directlabels calls its functions. (Very like ggplot-functions. I never got used to having functions be character values.):

assignInNamespace( 'draw.rects',  
function (d, ...) 
{
    if (is.null(d$box.color)) 
        d$box.color <- "red"
    if (is.null(d$fill)) 
        d$fill <- "white"
    for (i in 1:nrow(d)) {
        with(d[i, ], {
            grid.rect(gp = gpar(col = box.color, fill = fill), 
                vp = viewport(x, y, w, h, "cm", c(hjust, vjust), 
                  angle = rot))
        })
    }
    d
}, ns='directlabels')
dlp <- direct.label(p, "angled.boxes")
dlp

这篇关于如何在直接标签中配置box.color“draw.rects”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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