簡體   English   中英

在 ggplot 內移動時圖例消失

[英]Legend disappears when moved inside of ggplot

我正在制作一個圖,顯示隨着時間的推移幾種動物的家庭范圍大小。 圖例自動填充到圖的右側,我可以使用+ theme(legend.position= "position")成功地將它移動到圖的上方、下方或左側,但是當我嘗試將圖例移動到使用+ theme(legend.position= c(1, 250))的情節消失了。

我的數據由列“id”(字符向量)、“wtd_area”(數字)和“study_year”(數字)組成。

data %>%
ggplot(aes(x= study_year, y= wtd_area, color= id, shape= id)) + 
  theme_js() + 
  geom_point(size= 3) + geom_line(aes(group=id), size= 1) + 
  ylim(0,160) + scale_color_manual(values= palette) + 
  labs(x= NULL, y= NULL, color= "Animal ID", shape= "Animal ID") +
  theme(legend.position= c(1,150))

我有:

1.) 確認自定義主題theme_js()不會通過切換到通用主題來干擾。

2.) 分別為geom_point()geom_line() ) 添加了aes(color= id, shape= id)aes(color= id)

3.) 將show.legend= TRUE添加到geom_point()geom_line()

4.) 在scale_color_manual()中添加了aesthetics= "color"

palette是包含顏色十六進制代碼的字符向量。

該位置應在 0 到 1 的范圍內,而不是對應於您的軸。 x,y 為 0,0(左下)到 1,1(右上)。 在這里,我以ToothGrowth樣本數據為例。

ToothGrowth$dose <- as.factor(ToothGrowth$dose)

library(ggplot2)

ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) + 
  geom_boxplot() +
  theme(legend.position = c(0.8, 0.2))

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM