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