r - ggplot : position legend in top left

标签 r ggplot2

我有一个用 ggplot2 制作的图,我想将图例定位在 左上角
legend.position = "top" 给我一个位于情节上方的图例,但居中:

Legend is on top, but centered
legend.position = c(0,1) 获取左上角的图例,但它 float 在其他绘图元素上:

Look at that legend float!

知道如何将那个图例放在左上角而不让它 float 吗?我尝试声明图例高度,但没有骰子。我是否必须调整标题和绘图区域的大小和位置?

谢谢!

最佳答案

可以使用预定义选项通过legend.justification 来完成。

library(ggplot2)

ggplot(mtcars, aes(x=factor(cyl), y=mpg, fill=factor(cyl))) + 
  geom_boxplot() +
  ggtitle("No title needed") +
  theme(legend.position='top', 
        legend.justification='left',
        legend.direction='horizontal')

enter image description here

关于r - ggplot : position legend in top left,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28816467/

相关文章:

r - 在 R 中使用 marrangeGrob 和 ggsave 的多页 PDF 的页面标题

r - 如何在条形图(ggplot)中的条形组之间添加空间

r - 在ggplot中绘制经验和拟合半变异函数

r - Shinydashboard: 'restoreInput' 不是从 'namespace:shiny' 导出的对象

r - 使用因子使用 ggplot2 创建密度图

R dplyr : Drop multiple columns

read.table 函数用于读取 R 中不完整的数据

r - scale_fill_manual 在 geom_bar 中不起作用

r - 向量中仅某些元素的 N 种排列

r - 删除 R 图中的顶点标签