簡體   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