Legend title position in ggplot2

十年热恋 提交于 2019-12-06 20:12: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"))



来源:https://stackoverflow.com/questions/9739947/legend-title-position-in-ggplot2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!