在ggplot2中旋转图例

7

I wish to rotate a ggplot2 legend 90°

From

qplot(mpg, wt, data=mtcars, colour=cyl)

输入图像描述

生产

输入图像描述

3个回答

7

类似于:

p <- qplot(mpg, wt, data=mtcars, colour=cyl)
p + scale_colour_continuous(guide = guide_legend(direction = "horizontal", title.position = "top",
                             label.position="bottom", label.hjust = 0.5, label.vjust = 0.5,
                             label.theme = element_text(angle = 90))) + 
      theme(legend.position = c(0.5, 0.9))

Ref: ggplot docs

enter image description here


1
你知道为什么使用 guide = guide_legend(... 似乎会将键从连续变为离散吗? - user20650
3
那是因为传说是离散的 - 你需要使用“guide_colorbar”。 - Matt74

4

你可以尝试这个方法

library(ggplot2)
qplot(mpg, wt, data=mtcars, colour=cyl) + theme(legend.position = "top")


0

enter image description here

要完全正确,我认为您需要在guide_legend内使用label.themetitle.theme。 我认为使用title.position =“left”更好。
(复制以前解决方案的部分)
library(ggplot2)
qplot(mpg, wt, data=mtcars, colour=cyl) +
    scale_colour_continuous(guide = guide_legend(direction = "horizontal", title.position = "left", title.theme = element_text(angle = 90),
                             label.position="bottom", label.hjust = 0.5, label.vjust = 0.5,
                             label.theme = element_text(angle = 90))) + 
      theme(legend.position = c(0.5, 0.9))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接