2013-02-01 7 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

をあなたが探していた答えを与えた、私はpossibへのリンクを与えるだろうと思ったあなたが設定できる属性があります。 [**ここにいる**](https://github.com/hadley/ggplot2/wiki/Legend-Attributes) – Arun

答えて

15

代わりの機能guides()あなたがあなたのコード内でguide_colourbar代わりのguide_legendを使用することができる機能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

を設定する必要があります。@Didzis以来

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

関連する問題