How can I configure box.color in directlabels "draw.rects"?
Asked Answered
K

2

4

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?

Kensell answered 18/7, 2014 at 0:33 Comment(3)
In MacOS using the SL version of R 3.1.1 I do not get the angling of the boxes, nor is the fill an opaque-white. I tried a variety of efforts at redefining the various directlabels functions without success.Insurgent
Did you get the latest version of directlabels from r-forge? install.packages("directlabels", repos="http://r-forge.r-project.org")Kensell
When I compiled from source it has the behavior you demonstrate. Still not able to see what component of that object to modify.Insurgent
I
7

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

Insurgent answered 18/7, 2014 at 4:49 Comment(3)
I tried doing a similar thing, but without the assignInNamespace function - never heard of it til now! I couldn't work out how to call my custom function, it drove me mad. Thanks for the effort, great answer, shame it takes so much effort.Kensell
Sometimes you will fail, as in this case, with simply masking a function with <- because it doesn't have the proper environment to pick up the parameters when it gets executed. The new function will be evaluated in the GlobalEnv(), So assignInNamespace assures that it will be evaluated in the proper environment.Insurgent
Can't see another way to do it, so you get the tick. I think there's a way using apply.method, but documentation isn't clear enough to me to show me how to do it.Kensell
K
4

I also have a response from the package author, Toby Hocking. Either of these will work:

my.dl <- list(dl.trans(box.color="red"),"draw.rects")
direct.label(p, list("far.from.others.borders","calc.boxes", "enlarge.box", "my.dl"))

OR (shortcut)

my.dl <- list(box.color="red", "draw.rects")
direct.label(p, list("far.from.others.borders","calc.boxes", "enlarge.box", "my.dl"))
Kensell answered 28/7, 2014 at 23:57 Comment(1)
These work. Use box.color=NA to make the border invisible.Harmon

© 2022 - 2024 — McMap. All rights reserved.