#r #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
, но это не работает
Комментарии:
1.
pop2 <- pop2 %>% mutate(gender = ifelse(gender == 1, 'male', 'female'))
?2. Я сделал это, но это меняет форму пирамиды численности населения
Ответ №1:
Ты можешь это сделать 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"))