如何在 ggplot 2 中按类别突出显示特定条形图?
How can I highlight specific bars by category in ggplot 2?
使用 ggplot2
,我尝试根据类别使用 2 种不同的颜色为一组特定的条着色。在下面的图中,我将超过某个值的列填充为红色,但由于我的方法,它从着色中排除了它们的 'partner columns' (它们顶部的列),而我希望它们被填充蓝色的。
如果我更改 scale_fill_manual()
中的值,那么它不会执行任何操作,因为 'fill' 表达式将优先为 '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()
这是一种解决方法:
# define TRUE / FALSE condition, then assign the same condition
# to the male group within the same age range
pop <- pop %>%
mutate(condition = prop >= 0.504 & sex == "female" & age_range != '75 - 79') %>%
group_by(age_range) %>%
mutate(condition = any(condition))
# define colour / fill scale for gender
sex.scale <- c("female" = "red", "male" = "blue")
ggplot(pop,
aes(x = age_range, y = prop,
color = sex, group = sex,
label = scales::percent(prop))) +
# bars with colored outlines & grey fill
geom_col(position = "dodge", fill = "grey60") +
# bars with coloured fill; only visible if condition is TRUE
geom_col(aes(fill = sex, alpha = condition),
position = "dodge") +
scale_color_manual(values = sex.scale) +
scale_fill_manual(values = sex.scale, guide = F) +
scale_alpha_manual(values = c("TRUE" = 1, "FALSE" = 0)) +
geom_text(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()
样本子集数据:
pop <- data.frame(
age_range = rep(c("10-14", "15-19", "20-24", "25-29"), each = 2),
sex = rep(c("male", "female"), by = 4),
prop = c(0.51, 0.49, 0.518, 0.482, 0.495, 0.505, 0.446, 0.554)
)
使用 ggplot2
,我尝试根据类别使用 2 种不同的颜色为一组特定的条着色。在下面的图中,我将超过某个值的列填充为红色,但由于我的方法,它从着色中排除了它们的 'partner columns' (它们顶部的列),而我希望它们被填充蓝色的。
如果我更改 scale_fill_manual()
中的值,那么它不会执行任何操作,因为 'fill' 表达式将优先为 '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()
这是一种解决方法:
# define TRUE / FALSE condition, then assign the same condition
# to the male group within the same age range
pop <- pop %>%
mutate(condition = prop >= 0.504 & sex == "female" & age_range != '75 - 79') %>%
group_by(age_range) %>%
mutate(condition = any(condition))
# define colour / fill scale for gender
sex.scale <- c("female" = "red", "male" = "blue")
ggplot(pop,
aes(x = age_range, y = prop,
color = sex, group = sex,
label = scales::percent(prop))) +
# bars with colored outlines & grey fill
geom_col(position = "dodge", fill = "grey60") +
# bars with coloured fill; only visible if condition is TRUE
geom_col(aes(fill = sex, alpha = condition),
position = "dodge") +
scale_color_manual(values = sex.scale) +
scale_fill_manual(values = sex.scale, guide = F) +
scale_alpha_manual(values = c("TRUE" = 1, "FALSE" = 0)) +
geom_text(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()
样本子集数据:
pop <- data.frame(
age_range = rep(c("10-14", "15-19", "20-24", "25-29"), each = 2),
sex = rep(c("male", "female"), by = 4),
prop = c(0.51, 0.49, 0.518, 0.482, 0.495, 0.505, 0.446, 0.554)
)