Positioning legend horizontally within a plot?

1,726 views
Skip to first unread message

Thomas

unread,
Aug 22, 2013, 12:55:18 PM8/22/13
to ggp...@googlegroups.com
I would like to put a legend horizontally in the plot area.  I am failing. If I set legend.position to a set of coordinates, it won't produce a horizontal legend.  If, I set legend position to "bottom", it will produce a horizontal legend, but now (of course) it is outside of the plot area).  Any help with this mystery would be appreciated. (note, the real data has negative values so scale_colour_gradient2 will display the red)  

p <- ggplot(diamonds, aes(x, y))+
  geom_point(aes(colour=carat))+
  scale_colour_gradient2(low="red",mid="white",high="blue", name="this is\nthe legend")+  
  theme(legend.text = element_text(size=12),
        legend.key.size = unit(1.5, "lines"),
        legend.key = element_blank(),
        legend.position = c(0.1,0.17), 
        #legend.position = "bottom",
        legend.box = "horizontal"  ) 
x11()
p


Ista Zahn

unread,
Aug 22, 2013, 1:43:30 PM8/22/13
to Thomas, ggplot2
Hi Thomas,

The element should be legend.direction, not legend.box.

Best,
Ista


--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2
 
---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Thomas

unread,
Sep 1, 2013, 7:49:35 PM9/1/13
to ggp...@googlegroups.com, Thomas
That worked, thanks.  I now have another problem with this.  The legend title will only appear to the left of the horizontal legend.  I have seen a number of examples with horizontal legends and titles on top, but they never have explicit code for doing this? 

In the following code I want the legend title on top of the color scale.

p <- ggplot(diamonds, aes(x, y))+
  geom_point(aes(colour=carat))+
  scale_colour_gradient2(low="red",mid="white",high="blue", name="this is the legend")+  
  theme(
        legend.text = element_text(size=12),
        legend.key = element_blank(),
        legend.position = c(0.1,0.17), 
        legend.direction = "horizontal"  ) 
x11()
p

baptiste auguie

unread,
Sep 1, 2013, 7:54:15 PM9/1/13
to Thomas, ggplot2
p + guides(colour=guide_colourbar(title.position="top"))

HTH,

b.

Thomas

unread,
Sep 1, 2013, 8:43:03 PM9/1/13
to ggp...@googlegroups.com, Thomas
Thanks, 
That seems to resolve that issue. Last question is:  How do I center the entire legend?
The below code center the title, but if the title is greater than the length of the legend, it is a left justified center.  I can fake a solution by just making a longer bar (see commented out code), 
but I am curious to know the real solution. I didn't see anything like "colourbar.hjust"...

p <- ggplot(diamonds, aes(x, y))+
  geom_point(aes(colour=carat))+
  scale_colour_gradient2(low="red",mid="white",high="blue", name="this is the longer version for \nlegend")+  
  guides(colour=guide_colourbar(title.position="top", title.hjust=0.5))+ #centered title but the legend is left justified overall
  #guides(colour=guide_colourbar(title.position="top", title.hjust=0.5, barwidth=10))+ #fake solution to making entire legend appear centered.
  theme(
        legend.text = element_text(size=12),
        legend.key = element_blank(),
        legend.position = c(0.4,0.17), 
Reply all
Reply to author
Forward
0 new messages