2017-08-22 23 views
0

我使用geom_segment爲我的繪圖生成註釋背景。具體來說,我使用下面的代碼來繪製圖例。唯一的問題是,我希望看到圖例框中的顏色,但是這是不可能的,因爲我的geom_segments非常小(geom_segment不能填充,也就是說需要scale_color_manual並且不能使用scale_fill_manual,這可以解決問題)。使用geom_segment的各種圖例框的統一顏色

xa <- seq(1,20000) 
ya <- rep(0, length(xa)) 
anna <- as.factor(sample(c(1,2), size = length(xa), replace = T)) 
ggplot() + geom_segment(aes(x = xa, y = ya, xend = xa, yend = ya + 1, 
          col = anna), 
          size = 0.1) + 
scale_color_manual(name = "tit", values = c("#b47b00", "#000000"), 
            labels = c('bels1', 'bels2')) 

enter image description here 我想有在標籤盒只有統一的顏色,是在價值觀變量相同。

+0

如果你改變gem_segment線圖例的大小? 。把它加到你的代碼中:'guides(color = guide_legend(override.aes = list(size = 5)))' –

回答

0

您可以通過在guides層,像這樣使用override.aes做到這一點:

guides(color = guide_legend(override.aes = list(size=5))) 

全碼:

ggplot() + 
    geom_segment(aes(x = xa, y = ya, xend = xa, yend = ya + 1, col = anna), size = 0.1) + 
    scale_color_manual(name = "tit", values = c("#b47b00", "#000000"), 
            labels = c('bels1', 'bels2'))+ 
    guides(color = guide_legend(override.aes = list(size=5))) 

enter image description here

+0

謝謝,就是這樣! :) – Garini

相關問題