如何在ggplot/ggplot2中指定图例框的大小 [英] How to specify the legend box size in ggplot/ggplot2

查看:789
本文介绍了如何在ggplot/ggplot2中指定图例框的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R/ggplot2中,我有多个图,每个图都有一个图例框. 我希望图例框的宽度与每个图相同,但是ggplot2会根据图例名称,键值等(每个图的唯一性)来动态调整图例框的大小. 各种地块必须适合图例的指定宽度,并具有指定的图例宽度,并且必须分开制作(因此,切面以确保图之间没有相同的图例宽度)是必须的. 看着theme,我找不到指定图例框宽度的选项...有什么想法吗?

In R/ggplot2, I have multiple plots, each of which has a legend box. I want the legend box to be the same width for each plot, but ggplot2 tries to dynamically size the legend box based on the legend name, key values, etc. (which are unique to each plot). The various plots must fit into a specified publication slot, with a specified width for the legend, and the plots must be made separately (so faceting to guarantee identical legend widths across the plots isn't possible). Looking at theme I couldn't find an option to specify the legend box width ... any ideas?

推荐答案

要指定图例框的大小,可以使用+ theme(legend.key.size = unit(2, "cm")).

To specify the legend box size you could use + theme(legend.key.size = unit(2, "cm")).

library(tidyverse)
tb <- tibble(a = 1:10, b = 10:1, c = rep(1:2, 5))

ggplot(tb, aes(a, b, colour = c)) +
  geom_point() +
  theme(legend.key.size = unit(0.2, "cm"))

更多详细信息和其他修改,请此处并在键宽度下自变量此处.

More details and additional modifications are here and under the keywidth argument here.

这篇关于如何在ggplot/ggplot2中指定图例框的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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