3

Here's some test data:

y <- c(1:10, 6:15)
b <- c(rep(c("A", "B"), each=10))
x <- 1:10
df <- data.frame(b, x, y)

And a test plot:

library(ggplot2)
library(directlabels)
p1 <- ggplot(df, aes(x=x, y=y, colour=b)) + geom_line()
direct.label(p1, list("first.points", hjust=-1, vjust=-0.5))

enter image description here

I'd like the background of the labels to be white (in a rectangle around the text). Is there a way to achieve that? I tried fill="white", colour="white", background="white", nothing happened..

erc
  • 10,113
  • 11
  • 57
  • 88
  • Have you tried `theme_set(theme_bw())`? Maybe you get a warning, but it should work fine. – Peter Lustig Sep 23 '14 at 06:51
  • 3
    [**This Q&A**](http://stackoverflow.com/questions/24815672/how-can-i-configure-box-color-in-directlabels-draw-rects) may be helpful. – Henrik Sep 23 '14 at 06:52
  • @Henrik, thanks, `my.dl <- list(box.color="white", "draw.rects"); direct.label(p1, list("first.points", hjust=-1, vjust=-0.5, "calc.boxes", "my.dl"))` works! – erc Sep 23 '14 at 07:02
  • @beetroot, can you please write up an answer with your code and the resulting plot? Cheers. – Henrik Sep 23 '14 at 08:51
  • @Henrik, sure, done! – erc Sep 23 '14 at 10:43
  • @beetroot, Great! I think you can [**accept your own answer**](http://stackoverflow.com/help/self-answer) after 48 hours. – Henrik Sep 23 '14 at 10:46

2 Answers2

5

Alright, thanks to Henrik's comment pointing to this question I came up with this:

p1 <- ggplot(df, aes(x=x, y=y, colour=b)) + geom_line()

my.dl <- list(box.color="white", "draw.rects")
direct.label(p1, list("first.points", hjust=-1, vjust=-0.3, "calc.boxes", "my.dl"))

enter image description here

Community
  • 1
  • 1
erc
  • 10,113
  • 11
  • 57
  • 88
-1

Your only adaption your code requires is theme_set(theme_bw())

y <- c(1:10, 6:15)
b <- c(rep(c("A", "B"), each=10))
x <- 1:10
df <- data.frame(b, x, y)

theme_set(theme_bw())  # added
library(ggplot2)
library(directlabels)
p1 <- ggplot(df, aes(x=x, y=y, colour=b)) + geom_line()
direct.label(p1, list("first.points", hjust=-1, vjust=-0.5))

enter image description here

Peter Lustig
  • 941
  • 11
  • 23