2013-05-03 17 views
61

我與GGPLOT2繪製線條像這樣:控制GGPLOT2傳說看不影響情節

ggplot(iris, aes(Petal.Width,Petal.Length,color=Species)) + geom_line() + theme_bw() 

current plot

我發現傳奇標記很小,所以我希望它們更大。如果我改變大小,對劇情線改變過:

ggplot(iris, aes(Petal.Width,Petal.Length,color=Species)) + geom_line(size=4) + theme_bw() 

thick plot lines

但我只想在傳說中看到粗線條,我想讓情節上的線條變細。我試圖用legend.key.size,但它改變了標記的平方,而不是線的寬度:

library(grid) # for unit 
ggplot(iris,aes(Petal.Width,Petal.Length,color=Species))+geom_line()+theme_bw() + theme(legend.key.size=unit(1,"cm")) 

big legend keys

我還試圖用點:

ggplot(iris,aes(Petal.Width,Petal.Length,color=Species)) + geom_line() + geom_point(size=4) + theme_bw() 

當然,但它仍然影響劇情和傳說:

points

我想用圖線和圖例中的點/點。

所以我問了兩件事:

  1. 如何更改圖例線的寬度不改變的情節?
  2. 如何繪製圖中的線條,但在圖例中繪製點/點/方塊?

回答

92

要改變線寬只有你應該使用功能guides(),然後colour=使用guide_legend()override.aes=並設置size=的傳說。這將覆蓋繪圖中使用的大小,並將僅爲圖例使用新的大小值。

ggplot(iris,aes(Petal.Width,Petal.Length,color=Species))+geom_line()+theme_bw()+ 
     guides(colour = guide_legend(override.aes = list(size=3))) 

enter image description here

要獲得在傳說和點線積解決辦法是增加geom_point(size=0)確保點是不可見的,然後在guides()設置linetype=0刪除線和size=3獲得更大點。

ggplot(iris,aes(Petal.Width,Petal.Length,color=Species))+geom_line()+theme_bw()+ 
     geom_point(size=0)+ 
     guides(colour = guide_legend(override.aes = list(size=3,linetype=0))) 

enter image description here

+0

真棒,謝謝!這就是我需要的!任何機會回答我的第二個問題? – baltazar 2013-05-03 10:01:46

+0

已更新我的回答 – 2013-05-03 10:08:54

+2

可以在主題中設置嗎? – nate 2015-12-31 22:19:56