r-当图例位置为底部时,如何将图例标题放置在标签顶部



示例:

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

我们如何才能将标题"type"放在A和B的顶部,而不是放在它们的左侧?

感谢

您可以使用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

虽然这已经很老了,但我注意到这个答案图例标题在ggplot2中的位置还不错,即使ggplot2现在有很多版本高于0.9。我注意到,对图书馆(天平)的呼吁已经没有必要了。希望这能帮助

最新更新