ggplot2图例到底部和水平 [英] ggplot2 legend to bottom and horizontal

查看:169
本文介绍了ggplot2图例到底部和水平的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将ggplot2图例移动到图的底部并水平旋转?

How can I move a ggplot2 legend to the bottom of the plot and turn it horizontally?

示例代码:

library(reshape2) # for melt
df <- melt(outer(1:4, 1:4), varnames = c("X1", "X2"))
p1 <- ggplot(df, aes(X1, X2)) + geom_tile(aes(fill = value))
p1 + scale_fill_continuous(guide = guide_legend())

所需(近似)结果:

推荐答案

如果要移动图例的位置,请使用以下代码:

If you want to move the position of the legend please use the following code:

library(reshape2) # for melt
df <- melt(outer(1:4, 1:4), varnames = c("X1", "X2"))
p1 <- ggplot(df, aes(X1, X2)) + geom_tile(aes(fill = value))
p1 + scale_fill_continuous(guide = guide_legend()) +
    theme(legend.position="bottom")

这应该给您想要的结果.

This should give you the desired result.

这篇关于ggplot2图例到底部和水平的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆