直接标签的背景颜色& GGPLOT2?

时间:2014-09-23 06:20:43

标签: r ggplot2

以下是一些测试数据:

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

测试图:

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

我希望标签的背景为白色(在文本周围的矩形中)。有没有办法实现这一目标?我尝试了fill="white"colour="white"background="white",没有发生任何事情......

2 个答案:

答案 0 :(得分:4)

好的,感谢Henrik的评论,指出this question我想出了这个:

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

答案 1 :(得分:-1)

您的代码所需的唯一修改是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