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?

Sandy Muspratt
  • 31,719
  • 12
  • 116
  • 122
a different ben
  • 3,900
  • 6
  • 35
  • 45
  • 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. – IRTFM Jul 18 '14 at 03:50
  • Did you get the latest version of directlabels from r-forge? `install.packages("directlabels", repos="http://r-forge.r-project.org")` – a different ben Jul 18 '14 at 03:53
  • When I compiled from source it has the behavior you demonstrate. Still not able to see what component of that object to modify. – IRTFM Jul 18 '14 at 04:36

2 Answers2

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

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • 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. – a different ben Jul 18 '14 at 05:00
  • 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. – IRTFM Jul 18 '14 at 15:27
  • 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. – a different ben Jul 21 '14 at 00:45
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"))
a different ben
  • 3,900
  • 6
  • 35
  • 45