Background color for directlabels & ggplot2?

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 the test graph:

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 would like the background of the labels to be white (in a rectangle around the text). Is there a way to achieve this? I tried fill="white"

, colour="white"

, background="white"

, nothing happened ..

+3


source to share


2 answers


Ok, thanks to Henrik's comment pointing out 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

+4


source


Your only adaptation 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

-1


source







All Articles