How can I customize 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 zk 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 change the box border color to β€œwhite”, but I don’t see how to do it. The NEWS for the package says:

2.5 --- April 6, 2012

draw.rects with custom color, black by default.

And seeing that "angled.boxes" is a list containing:

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

I suppose it's possible, but how?

+3
source share
2 answers

This seems to be a hack, but it worked. I just overridden the draw.rects function, since I couldn't figure out how to pass arguments due to the awkward way that directlabels calls its functions. (Very similar to ggplot functions. I'm never used to having functions to be symbolic 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

+7
source

I also have a response from the author of the package, Toby Hawking. Any of them 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")) 
+3
source

Source: https://habr.com/ru/post/986599/


All Articles