I have a horizontal bar plot and would like to align my bar labels to the left and right of each bar like shown below:

ie. I want the labels left aligned if the bar plot has a positive value and right aligned if the bar plot has a negative value.
Use the code below as a workable example:
value <- c(3,2,1,-1,-2,-3)
label <- factor(c("Arnold","Bob","Chris","David","Emma","Frank"))
df <- data.frame(value, label)
ggplot(df, aes(x = reorder(label, value), y = value)) +
geom_bar(stat = "identity") +
geom_text(aes(y = 0, label = label)) +
coord_flip() +
labs(x = NULL, y = NULL) +
theme_classic() +
theme(axis.ticks.y = element_blank(),
axis.line.y = element_blank(),
axis.text.y = element_blank())
Thank you
>Solution :
You can add an hjust aesthetic to geom_text, like this…
ggplot(df, aes(x = reorder(label, value), y = value)) +
geom_bar(stat = "identity") +
geom_text(aes(y = 0, label = label, hjust = 0.5 + 0.5 * sign(value))) +
coord_flip() +
labs(x = NULL, y = NULL) +
theme_classic() +
theme(axis.ticks.y = element_blank(),
axis.line.y = element_blank(),
axis.text.y = element_blank())
