繁体   English   中英

用ggplot2包装时,在图内显示图例

[英]Display the legend inside the graph when wrapping with ggplot2

我能怎么做...

图例位于一小部分的备用角

...而不是默认值...

在此处输入图片说明

我为此使用了diamonds数据集。 您可以使用theme(legend.position=来做到这一点:

ggplot(diamonds, aes(carat, price, fill = ..density..)) +
  xlim(0, 2) + stat_binhex(na.rm = TRUE) + theme(aspect.ratio = 1) +
  facet_wrap(~ color) + theme(legend.position=c(.8,.15))

输出:

在此处输入图片说明

本质上, theme(legend.position=c(.8,.15))取两个值,范围从0到1,一个用于x轴,一个用于y轴。 0表示将图例放在轴的起点,而1表示在轴的终点。

如果您喜欢其他示例,可以阅读食谱

同样根据@Roland的注释,在legend.justification使用以下内容可能会使其位置更好:

ggplot(diamonds, aes(carat, price, fill = ..density..)) +
  xlim(0, 2) + stat_binhex(na.rm = TRUE) + theme(aspect.ratio = 1) +
  facet_wrap(~ color) + theme(legend.position = c(1, 0), legend.justification = c(1, 0))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM