简体   繁体   中英

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

在此处输入图片说明

I have tried scale_fill_discret but it does not work

You can do 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"))

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM