2013-02-01 12 views
9

Ho uno script di seguito per illustrare la mia domanda:regolare la larghezza di leggenda per la variabile continua in 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 

Come posso ampliare la leggenda "variabile continua" posizionato in basso?

+4

Dal @Didzis dato la risposta che stavi cercando, ho pensato di dare il link al possibile le caratteristiche che potresti impostare. [** Eccolo **] (https://github.com/hadley/ggplot2/wiki/Legend-Attributes) – Arun

risposta

15

Invece di funzione guides() si dovrebbe utilizzare la funzione theme() e impostare la 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) mi ha battuto di 7 secondi! – Arun

5

È possibile utilizzare guide_colourbar invece di guide_legend nel codice:

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

Problemi correlati