簡體   English   中英

在 ggplot2 中編輯圖例值

[英]edit legend values in ggplot2

我有如下圖所示。 它是使用以下命令創建的:

popanim <-  ggplot(data =  pop2, aes(
  x = age1,
  y = ifelse(gender == 1, -pop_rel, pop_rel),
  fill = gender, 
  group = district
)) +
  geom_col() +
  # facet_wrap( ~ country) +
  coord_flip() +
  transition_states(district, transition_length = 2, state_length = 1) +
  labs(title = 'district: {closest_state}')+
  xlab("age") + ylab("population") #HERE


anim_pop <- animate(popanim, nframes = 50, renderer = gifski_renderer("gganimate.gif"))

anim_save(anim_pop = anim_pop, filename ="//pyramidd.gif")

現在我要做的下一件事是將圖例值從 1 修改為male ,將 2 修改為female

在此處輸入圖片說明

我試過scale_fill_discret但它不起作用

你可以做scale_fill_discrete(labels = c("Male", "Female")) 在這里,我展示了一個可重現的示例,因為您尚未提供可重現的數據(始終推薦):

library(ggplot2)
ggplot(mtcars, aes(hp, mpg, fill = factor(cyl))) +
        geom_col() +
        coord_flip() +
        scale_fill_discrete(labels = c("Male", "Female", "Other"))

在此處輸入圖片說明

暫無
暫無

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

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