Legend title position in ggplot2

2019-04-21 00:50发布

问题:

Anone know how to change the position of the legend title in ggplot?

I have used the following code to move the legend to the bottom and make it horizontal

p <- p + opts(legend.position = 'bottom', legend.direction = 'horizontal')

But now I want the title to be to the left of the legend instead of above. I've looked in the follwing places but cant find it or figure it out:

https://github.com/hadley/ggplot2/wiki/Legend-Attributes http://had.co.nz/ggplot2/book/toolbox.r

Any assistance would be greatly appreciated

回答1:

Using the transition guide to version 0.9 as a reference, you might try the following (assuming you want to change the title position for the colour legend):

library(scales)
+ guides(colour = guide_legend(title.position = "left"))

For a continuous scale you'd use guide_colorbar instead of guide_legend.

Just to provide a concrete example to prove I'm not just making this up,

library(ggplot2)
library(scales)
p <- ggplot(mtcars, aes(wt, mpg))
p + geom_point(aes(colour = qsec)) + 
    guides(colour = guide_legend(title.position = "right"))