【问题标题】:How can I place the legend title on top of the labels when the legend position is, for example, bottom?当图例位置为底部时,如何将图例标题放在标签顶部?
【发布时间】:2016-12-03 02:17:12
【问题描述】:

例子:

xy <- data.frame(x=1:10, y=10:1, type = rep(LETTERS[1:2], each=5))

plot <- ggplot(data = xy)+
  geom_point(aes(x = x, y = y, color=type)) +
  theme(legend.position = 'bottom')

plot

我们如何才能在 A 和 B 的顶部而不是它们的左侧获得标题“类型”?

谢谢

【问题讨论】:

    标签: r ggplot2 legend


    【解决方案1】:

    您可以使用 guides() 并指定标题位置来解决您的问题。

    library(ggplot2)
    xy <- data.frame(x=1:10, y=10:1, type = rep(LETTERS[1:2], each=5))
    
    plot <- ggplot(data = xy)+ geom_point(aes(x = x, y = y, color=type)) +
            theme(legend.position = 'bottom') + 
            guides(colour = guide_legend(title.position = "top"))
    
    plot
    

    虽然这很旧,但我注意到这个答案 Legend title position in ggplot2 还不错,即使 ggplot2 现在是 0.9 以上的许多版本。 我注意到不再需要调用库(秤)。 希望这会有所帮助

    【讨论】:

    • 谢谢!我知道如何使用 opts 来做到这一点,这已经被弃用了一段时间。我无法弄清楚定位是在颜色内确定的
    猜你喜欢
    • 2017-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    • 2014-08-04
    • 1970-01-01
    相关资源
    最近更新 更多