【发布时间】:2015-12-01 14:21:43
【问题描述】:
【问题讨论】:
标签: r ggplot2 legend facet facet-wrap
【问题讨论】:
标签: r ggplot2 legend facet facet-wrap
我为此使用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 表示将图例放置在轴的末尾。
如果您喜欢有更多示例,可以查看cookbook。
根据@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))
【讨论】:
legend.justification,即theme(legend.position = c(1, 0), legend.justification = c(1, 0))。
legend.justification 参数。
theme(legend.position = c(1, 0), legend.justification = c(1, 0)) 会一直放在右下角。