是否可以将图例定位到R中ggplot的右上角? [英] Is it possible to position the legend to the top right of a ggplot in R?

查看:1146
本文介绍了是否可以将图例定位到R中ggplot的右上角?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为ggplot创建一个主题,然后我可以将它用于我的所有图形,并让它们看起来既漂亮又漂亮。我想将图例从当前位置垂直居中的右侧移动到与右侧图形顶部对齐,如下面的红色箭头所示。

I am trying to create a theme for ggplot that I can then use for all my graphs and getting them looking both nice and nice and uniform. I want to move the legend from its current position vertically centred on the right to being aligned with the top of the graph on the right, as indicated by the red arrow below.

我无法弄清楚。我可以通过使用 legend.position 来将它放置在里面的位置,但是如果我做了 legend.justification = c (0.0,1.0)它会将图例推到它绘制的区域之外,并完全切断。我知道我可以通过为每个图形进行grobs和gtables的混合来为每个图形单独执行操作,但是我不希望每次绘制图形都要这样做。

I cannot figure it out. I can get it to position inside the plot by using legend.position but if I then do legend.justification = c(0.0, 1.0) it pushes the legend outside of the area it plots and it gets cut off completely. I know I could do it individually for each graph by messing around with grobs and gtables for each individual graph but I don't want to have to do that every time I plot a graph.

有没有办法使用主题

Is there anyway to do this with a theme?

推荐答案

<看起来像是最终可以用ggplot2 2.2.0

Seems like it's finally possible with ggplot2 2.2.0

library(ggplot2)
ggplot(mpg, aes(displ, hwy, colour=fl)) + 
  geom_point() + 
  theme(legend.justification = "top")

这篇关于是否可以将图例定位到R中ggplot的右上角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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