簡體   English   中英

如何更改此 ggplot 圖中的圖例?

[英]How to change the legend in this ggplot graph?

我正在嘗試更改我的 ggplot 圖的圖例,但我沒有設法這樣做。 這是我的 df 和我的圖表代碼:

#df
df = structure(list(Date = structure(c(18319, 18320, 18321, 18322, 
18323, 18324, 18325, 18326, 18327, 18328, 18329, 18330, 18331, 
18332, 18333, 18334, 18335, 18336, 18337, 18338, 18339, 18340, 
18341, 18342, 18343), class = "Date"), Daily_Cases = c(250, 238, 
240, 566, 342, 466, 587, 769, 778, 1247, 1492, 1797, 977, 2313, 
2651, 2547, 3497, 3590, 3233, 3526, 4207, 5322, 5986, 6557, 5560
), Tests = c(2427, 3681, 2966, 2466, 2218, 2511, 3981, 2525, 
3997, 5703, 7875, 3889, 6935, 12393, 12857, 11477, 11682, 15729, 
13063, 10695, 16884, 17236, 24109, 26336, 25180), Proportion = c(10.3, 
6.5, 8.1, 23, 15.4, 18.6, 14.7, 30.5, 19.5, 21.9, 18.9, 46.2, 
14.1, 18.7, 20.6, 22.2, 29.9, 22.8, 24.7, 33, 24.9, 30.9, 24.8, 
24.9, 22.1)), class = "data.frame", row.names = c(NA, -25L))


df <- df %>% pivot_longer(names_to = 'Legend', values_to = 'value', Tests:Daily_Cases) 

ggplot(df) + theme_bw() +
  geom_col(aes(Date, value, fill = Legend), position = position_dodge()) +
  geom_line(aes(Date, Proportion*1000, colour = "Infected (%)", group = 1), size = 0.8) + scale_y_continuous("\n", sec.axis = sec_axis(~ . /1000, name = "(%) \n")) + labs(x="", y="") + 
  scale_colour_manual(" ", values=c("Infected (%)" = "black")) +
  theme(legend.position = "bottom")

這是結果:

在此處輸入圖片說明

不過,我希望能夠以分別刪除單詞聯想,改變它的話Daily_Cases並進入測試案例測試件號碼

我沒辦法這樣做。 誰能幫我?

一種方法是scale_fill_discrete

ggplot(df) + theme_bw() +
  geom_col(aes(Date, value, fill = Legend), position = position_dodge()) + 
  scale_fill_discrete(name=NULL,labels=c("Number of Cases","Number of Tests")) +
  geom_line(aes(Date, Proportion*1000, colour = "Infected (%)", group = 1), size = 0.8) + scale_y_continuous("\n", sec.axis = sec_axis(~ . /1000, name = "(%) \n")) + labs(x="", y="") + 
  scale_colour_manual(" ", values=c("Infected (%)" = "black")) +
  theme(legend.position = "bottom")

您的問題的所有答案都是根據此處對在 ggplot 中使用 Legends 的相關問題的答案對代碼進行的修改。 最簡單的方法是添加對scale_fill_manual()的調用,類似於您已經對scale_color_manual()所做的:

ggplot(df) + theme_bw() +
    geom_col(aes(Date, value, fill = Legend), position = position_dodge()) +
    geom_line(aes(Date, Proportion*1000, colour = "Infected (%)", group = 1), size = 0.8) + 
    scale_y_continuous("\n", sec.axis = sec_axis(~ . /1000, name = "(%) \n")) + labs(x="", y="") + 
    scale_colour_manual(" ", values=c("Infected (%)" = "black")) +
    theme(legend.position = "bottom") +
    scale_fill_manual("", values=c('Daily_Cases'='blue', 'Tests'='red'), labels=c('Numer of Cases', 'Number of Tests'))

給你下面(你可以將顏色更改為你想要的任何顏色,但我認為這就是你要找的):

在此處輸入圖片說明

正如另一位用戶指出的那樣,您可以使用scale_fill_discrete()僅保留默認顏色並僅指定標簽:

ggplot(df) + theme_bw() +
    geom_col(aes(Date, value, fill = Legend), position = position_dodge()) +
    geom_line(aes(Date, Proportion*1000, colour = "Infected (%)", group = 1), size = 0.8) +
    scale_y_continuous("\n", sec.axis = sec_axis(~ . /1000, name = "(%) \n")) + labs(x="", y="") + 
    scale_colour_manual(" ", values=c("Infected (%)" = "black")) +
    theme(legend.position = "bottom") +
    scale_fill_discrete("", labels=c('Numer of Cases', 'Number of Tests'))

所有正確的答案,但我只是在scale_fill_ etc使用命名向量

這不僅是為了方便,而且更安全,因為您可以更好地控制標簽。

另外,不要使用name = '' ,因為會繪制“某物”。 使用NULL代替

ggplot(df) + theme_bw() +
  geom_col(aes(Date, value, fill = Legend), position = position_dodge()) +
  geom_line(aes(Date, Proportion*1000, colour = "Infected (%)", group = 1), size = 0.8) + scale_y_continuous("\n", sec.axis = sec_axis(~ . /1000, name = "(%) \n")) + labs(x="", y="") + 
  scale_colour_manual(" ", values=c("Infected (%)" = "black")) +
  theme(legend.position = "bottom") +
scale_fill_brewer(name = NULL, labels = c(Daily_Cases = 'Number of cases', Tests = 'Number of Tests'))

暫無
暫無

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

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