2013-02-01 36 views
9

連續可變下面我有一個腳本來說明我的問題:調整傳說的寬度爲GGPLOT2

temp.df <- data.frame(x=1:100,y=1:100,z=1:100,stringsAsFactors=FALSE) 
chart <- ggplot(data=temp.df,aes(x=x,y=y)) 
chart <- chart + geom_line(aes(colour=z)) 
chart <- chart + scale_colour_continuous(low="blue",high="red") 
chart <- chart + theme(legend.position="bottom") 
# so far so good, but I think the legend positioned at bottom with such a small size is a waste of space, so I want to "widen" it using the line below... 
chart <- chart + guides(colour=guide_legend(keywidth=5,label.position="bottom")) 
# oops, it changed to legend for categorical variable 

我怎樣才能擴大位於底部的「連續可變」的傳說?

+4

由於@Didzis給出了你正在尋找的答案,我想我會給出可能的鏈接你可以設置的屬性。 [**這裏是**](https://github.com/hadley/ggplot2/wiki/Legend-Attributes) – Arun

回答

15

而不是函數guides()你應該使用功能theme()並設置legend.key.width=

temp.df <- data.frame(x=1:100,y=1:100,z=1:100,stringsAsFactors=FALSE) 
chart <- ggplot(data=temp.df,aes(x=x,y=y)) 
chart <- chart + geom_line(aes(colour=z)) 
chart <- chart + scale_colour_continuous(low="blue",high="red") 
chart <- chart + theme(legend.position="bottom") 
chart <- chart + theme(legend.key.width=unit(3,"cm")) 
+1

(+1)在7秒內擊敗我! – Arun

5

您可以在代碼中使用guide_colourbar代替guide_legend

temp.df <- data.frame(x=1:100,y=1:100,z=1:100,stringsAsFactors=FALSE) 
chart <- ggplot(data=temp.df,aes(x=x,y=y)) 
chart <- chart + geom_line(aes(colour=z)) 
chart <- chart + scale_colour_continuous(low="blue",high="red") 
chart <- chart + theme(legend.position="bottom") 
chart + guides(colour=guide_colourbar(barwidth=30,label.position="bottom")) 

enter image description here