0
使用ggplot2
,我試圖根據其類別使用2種不同的顏色對一組特定的顏色進行着色。在下面的圖中,我列出了一些填充爲紅色的特定值,但由於我的方法,它會將「合作伙伴列」(列上的列)從着色中排除,但我希望它們充滿藍色。如何在ggplot 2中按類別高亮顯示特定的條?
如果我更改scale_fill_manual()
中的值,那麼它將不會執行任何操作,因爲「填充」表達式會優先着色「TRUE」和「FALSE」類別。
我該如何更改我的代碼,以便伴隨着填充的紅色條的條會被塗成藍色?
我現在的情節:
我的代碼:
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()
嘿,感謝您的幫助!你的回答正是我需要的! –