2017-08-27 47 views
0

我想在圖例沒有標題時使圖例邊框更緊。實際上,圖例鍵上方有一個空白區域。我也想把邊框變成虛線。沒有標題的緊密圖例邊框

ggplot2

情節是基於以下代碼:

library(ggplot2) 
ggplot(temp, aes(x=diff_abs, y=emp_est_15, color=diff_sign)) + geom_point(shape=1, size=2) + 
    scale_colour_manual(values=c("green4", "red")) + 
    scale_x_log10(breaks=10^(0:3)) + scale_y_log10(breaks=c(c(2,4,8) %o% 10^(0:3))) + 
    theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
     panel.background = element_blank(), axis.line = element_line(colour = "black"), 
     axis.text = element_text(color="black", size=13), axis.title = element_text(color="black", size=13), 
     legend.key = element_blank(), legend.position=c(.2,.8), legend.box.background = element_rect(), 
     legend.background = element_blank()) + 
    labs(x="\nGain ou perte d'emploi 2001-2015 (milliers, échelle log 10)", 
     y="Emploi 2015 (milliers, échelle log 10)\n", color="") 
+2

?'主題(legend.title = element_blank())來改變' –

回答

3

正如理查德德福註釋中,設置legend.title = element_blank()將移除圖例標題&所佔用的空間,因此「緊縮」的傳說框。

圖例框的邊框類型可以與legend.box.background = element_rect(line = <some number other than 1>)

# example using mtcars dataset 
p <- ggplot(mtcars, aes(wt, mpg, col = factor(cyl))) 
p + geom_point() + 
    theme_bw() + 
    theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
     panel.background = element_blank(), 
     legend.key = element_blank(), legend.position=c(.8,.8), 
     legend.title = element_blank(), 
     legend.box.background = element_rect(line = 3), 
     legend.background = element_blank()) 

enter image description here