2014-09-23 113 views
3

下面是一些測試數據:directlabels&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",什麼都沒有發生..

+0

你有沒有試過'theme_set(theme_bw())'?也許你會得到警告,但它應該可以正常工作。 – 2014-09-23 06:51:37

+3

[** This Q&A **](http://stackoverflow.com/questions/24815672/how-can-i-configure-box-color-in-directlabels-draw-rects)可能會有所幫助。 – Henrik 2014-09-23 06:52:45

+0

@Henrik,謝謝,'my.dl < - list(box.color =「white」,「draw.rects」); direct.label(p1,list(「first.points」,hjust = -1,vjust = -0.5,「calc.boxes」,「my.dl」))'工作! – beetroot 2014-09-23 07:02:54

回答

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

你的代碼只需要適應是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

謝謝,但那不是我正在尋找的,我希望信件的背景也是白色的。 – beetroot 2014-09-23 06:57:15

+0

啊,我明白了,對不起,那@亨利克的提示可能會有所幫助! – 2014-09-23 06:58:26

+0

不用擔心,現在得益於@Henrik – beetroot 2014-09-23 07:02:03