例子:
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 的顶部,而不是它们的左侧?
谢谢
最佳答案
您可以使用 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 以上的许多版本。 我注意到不再需要调用 library(scales)。 希望这有帮助
关于r - 例如,当图例位置为底部时,如何将图例标题放在标签的顶部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38640593/