在ggplot2中旋转图例

时间:2015-04-12 19:53:56

标签: r plot ggplot2

我希望将ggplot2图例旋转90°

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

enter image description here

生产

enter image description here

3 个答案:

答案 0 :(得分:3)

类似的东西:

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))

参考:ggplot docs

enter image description here

答案 1 :(得分:3)

你可以试试这个

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

答案 2 :(得分:0)

enter image description here

要完全正确,我认为您需要在label.theme中使用title.theme以及guide_legend。我认为使用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))