【发布时间】:2018-03-23 05:49:53
【问题描述】:
使用ggplot2,我正在尝试根据它们的类别使用2 种不同的颜色为一组特定的条着色。在下面的图中,我有超过某个值的列用红色填充,但由于我的方法,当我希望它们用蓝色填充时,它会从着色中排除它们的“伙伴列”(它们顶部的列) .
如果我更改 scale_fill_manual() 中的值,则它不会做任何事情,因为“填充”表达式将优先为“真”和“假”类别着色。
如何更改我的代码,使填充的红色条旁边的条变为蓝色?
我目前的剧情:
我的代码:
pop %>%
group_by(age_range, sex) %>%
summarize(population = sum(population)) %>%
mutate(prop = population / sum(population)) %>%
ggplot() +
geom_col(aes(x = age_range, y = prop, color = sex,
fill = (prop >= .504 & sex == 'female' & age_range != '75 - 79'),
width = .85),
position = 'dodge') +
scale_fill_manual(values = c('Grey60', 'Grey60', 'Blue', 'Red')) +
scale_color_manual(values = c('Red', 'Blue')) +
geom_text(aes(x = age_range, y = prop, fill = sex, label = percent(prop)),
position = position_dodge(width = .9),
vjust = .358, hjust = 1.1,size = 4, color = 'White') +
scale_y_continuous(limits = c(0, 1), expand = c(0,0)) +
geom_hline(yintercept = .504, color = 'Grey', alpha = .7) +
coord_flip()
【问题讨论】: