[英]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.