I am trying to label columns with geom_text()
on a geom_col()
. But, the labels (numbers) doesn't labeled the columns properly. It seems that the hjust
values are different from each other.
My Code:
state_retail %>%
group_by(subsector) %>%
summarise(avg_change_yoy = mean(change_yoy, na.rm = TRUE),
avg_change_yoy_se = mean(change_yoy_se, na.rm = TRUE),
total = sum(avg_change_yoy + avg_change_yoy_se)) %>%
pivot_longer(cols = c("avg_change_yoy", "avg_change_yoy_se"),
names_to = "avgs",
values_to = "values") %>%
arrange(desc(total)) %>%
slice_head(n = 12) %>%
ggplot(aes(values, reorder(avgs, values), fill = subsector, color = subsector)) +
geom_col(position = "dodge") +
scale_y_discrete(labels = c(
"avg_change_yoy" = "Average Change Yoy",
"avg_change_yoy_se" = "Average Change Yoy Se"
)) +
theme(legend.position = "bottom") +
#print value for each bar as well
geom_text(aes(label = format(round(values, digits = 2), big.mark = ",")), hjust = "inward",
size = 4, color ="white", position = position_dodge(1))
As you can see in the chart below, some labels are at the top of the column, some in the middle and some at the bottom.
How can I arrange these values to be centered on geom_col()
? I want all labels to be in the middle like the red columns.
Note: Also, one of the labels doesn't even appear in the column. In the second to last column.