Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
494 views
in Technique[技术] by (71.8m points)

r - How can I configure box.color in directlabels "draw.rects"?

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: enter image description here

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...