简体   繁体   English

在 ggplot2 中编辑图例值

[英]edit legend values in ggplot2

I have the following plot like below.我有如下图所示。 It was created with this command:它是使用以下命令创建的:

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")

Now next thing I want to do is to modify the legend value from 1 into male and 2 into female现在我要做的下一件事是将图例值从 1 修改为male ,将 2 修改为female

在此处输入图片说明

I have tried scale_fill_discret but it does not work我试过scale_fill_discret但它不起作用

You can do scale_fill_discrete(labels = c("Male", "Female")) .你可以做scale_fill_discrete(labels = c("Male", "Female")) Here I show a reproducible example since you have not provided reproducible data (which is always recommended):在这里,我展示了一个可重现的示例,因为您尚未提供可重现的数据(始终推荐):

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