在 R 中,人口金字塔的 ggplot:如何在翻转坐标后将轴附近的标签与 geom_bar geom_label 对齐
In R, ggplot for a population pyramid: how to align labels near to the axis with geom_bar geom_label after flipping the coordinates
我正在使用 ggplot 制作一种人口金字塔(plotrix 不允许我做花哨的标签等),然后我从带有标签的 geom_bar 开始,然后翻转坐标。可悲的是,标签几乎看不到。我想将这些标签移到中间的 "y- axis" 附近,现在显示的是年龄组。
数据在这里:
d <- data.frame(age.grp2 = c("1-10", "11-20", "21-30", "31-40", "41-50", "1-10", "11-20", "21-30", "31-40", "41-50"),
sex = c("Female","Female","Female","Female","Female","Male","Male","Male","Male","Male" ),
n.enroll = c(288,500,400,300,200,300,460,300,200,300),
proportion = c(17.1,29.6,23.7,17.8,11.8,51,47.9,42.9,40,60),
proportion2 = c(-17.1,-29.6,-23.7,-17.8,-11.8,51,47.9,42.9,40,60))
我的代码是这样的:
ggplot(d, aes(x = age.grp2, y = proportion2, fill = sex)) +
geom_bar(position = position_dodge(width=1), stat='identity') +
geom_label(aes(label = paste(n.enroll," (",proportion,"%)", sep=""), group = factor(sex)),
fill="white", colour = "black",
position= position_dodge(width=1),
size = 3) +
scale_fill_manual(values=c("#BFD5E3", "grey")) +
facet_share(~sex, dir = "h", scales = "free", reverse_num = TRUE) +
coord_flip() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
#panel.border = element_blank(),
panel.background = element_blank(),
legend.position = "none",
#axis.line.x = element_line(color = "black"),
axis.ticks.y = element_blank(),
axis.text.x = element_text(colour = "black", size = 8, face = "bold", angle=0, hjust=0.5),
axis.text.y = element_text(colour = "black", size = 8, face = "bold"),
axis.title.x = element_text(size = 14, face="bold", margin = margin(t = 30, r = 20, b = 10, l = 20)),
plot.margin = unit(c(1,1,1,1),"cm")) +
labs(y = "Enrollment percentage within sex",x="")
我也附上情节,我们可以在女性中看到11-20岁年龄段的标签被剪掉了。我想让所有标签都靠近年龄组标签,在每个栏内:女性标签向右移动,男性标签向左移动。此外,我希望将每个 x 轴扩展到 100% 或至少在相同范围内,女性高达 30%,男性高达 60%。感谢大家的评论
这是使用基础 ggplot
包的最小解决方案,没有大部分格式。关键部分是在 geom_label(aes())
部分添加条件 y = ...
:
d %>%
mutate(
label = str_c(n.enroll, " (", proportion, "%)"),
label_loc = if_else(sex == "Female", -9.5, 3),
proportion_for_chart = if_else(sex == "Female", -proportion, proportion)
) %>%
ggplot(aes(x = age.grp2, y = proportion_for_chart, fill = sex)) +
geom_col(show.legend = FALSE) +
geom_label(aes(y = label_loc, label = label), size = 3, fill = "white", hjust = 0) +
coord_flip() +
facet_wrap(~ sex, scales = "free") +
theme(
axis.title = element_blank()
)
只要有可能,我都会尝试重塑数据并使用 geom_col
,而不是尝试使用 geom_bar
来碰碰运气。您应该能够在 geom_label
调用中使用 y
的不同硬编码值,以根据格式和图像 size/scale 修复标签的正确位置。
我正在使用 ggplot 制作一种人口金字塔(plotrix 不允许我做花哨的标签等),然后我从带有标签的 geom_bar 开始,然后翻转坐标。可悲的是,标签几乎看不到。我想将这些标签移到中间的 "y- axis" 附近,现在显示的是年龄组。
数据在这里:d <- data.frame(age.grp2 = c("1-10", "11-20", "21-30", "31-40", "41-50", "1-10", "11-20", "21-30", "31-40", "41-50"),
sex = c("Female","Female","Female","Female","Female","Male","Male","Male","Male","Male" ),
n.enroll = c(288,500,400,300,200,300,460,300,200,300),
proportion = c(17.1,29.6,23.7,17.8,11.8,51,47.9,42.9,40,60),
proportion2 = c(-17.1,-29.6,-23.7,-17.8,-11.8,51,47.9,42.9,40,60))
我的代码是这样的:ggplot(d, aes(x = age.grp2, y = proportion2, fill = sex)) +
geom_bar(position = position_dodge(width=1), stat='identity') +
geom_label(aes(label = paste(n.enroll," (",proportion,"%)", sep=""), group = factor(sex)),
fill="white", colour = "black",
position= position_dodge(width=1),
size = 3) +
scale_fill_manual(values=c("#BFD5E3", "grey")) +
facet_share(~sex, dir = "h", scales = "free", reverse_num = TRUE) +
coord_flip() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
#panel.border = element_blank(),
panel.background = element_blank(),
legend.position = "none",
#axis.line.x = element_line(color = "black"),
axis.ticks.y = element_blank(),
axis.text.x = element_text(colour = "black", size = 8, face = "bold", angle=0, hjust=0.5),
axis.text.y = element_text(colour = "black", size = 8, face = "bold"),
axis.title.x = element_text(size = 14, face="bold", margin = margin(t = 30, r = 20, b = 10, l = 20)),
plot.margin = unit(c(1,1,1,1),"cm")) +
labs(y = "Enrollment percentage within sex",x="")
我也附上情节,我们可以在女性中看到11-20岁年龄段的标签被剪掉了。我想让所有标签都靠近年龄组标签,在每个栏内:女性标签向右移动,男性标签向左移动。此外,我希望将每个 x 轴扩展到 100% 或至少在相同范围内,女性高达 30%,男性高达 60%。感谢大家的评论
这是使用基础 ggplot
包的最小解决方案,没有大部分格式。关键部分是在 geom_label(aes())
部分添加条件 y = ...
:
d %>%
mutate(
label = str_c(n.enroll, " (", proportion, "%)"),
label_loc = if_else(sex == "Female", -9.5, 3),
proportion_for_chart = if_else(sex == "Female", -proportion, proportion)
) %>%
ggplot(aes(x = age.grp2, y = proportion_for_chart, fill = sex)) +
geom_col(show.legend = FALSE) +
geom_label(aes(y = label_loc, label = label), size = 3, fill = "white", hjust = 0) +
coord_flip() +
facet_wrap(~ sex, scales = "free") +
theme(
axis.title = element_blank()
)
只要有可能,我都会尝试重塑数据并使用 geom_col
,而不是尝试使用 geom_bar
来碰碰运气。您应该能够在 geom_label
调用中使用 y
的不同硬编码值,以根据格式和图像 size/scale 修复标签的正确位置。