Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I neatly align my stacked barchart labels, with differing alignments to each side of the bar?

I have my labels roughly aligned to each side of my stacked bar chart. The problem is that they look like a mess because they aren't right and left justified on either side of the bar. How do I fix this so that they look professional?

df3 <- data.frame(
  Label = c("Dasher", "Dancer", "Comet", "Cupid", "Prancer", "Blitzen", "Rudolph"),
  Amount = c(650.01, 601.01, 340.05, 330.20, 260.01, 250.80, 10.10)
)

# Sort order
level_order <- df3 %>% 
  arrange(desc(Amount))

ggplot(level_order, aes(fill=fct_inorder(Label), y=Amount, x="")) + 
               geom_bar(position="stack", stat="identity", width = 0.55) +
               scale_fill_brewer(palette = "Blues", direction = -1) +
               theme_void() +
               geom_text(aes(label = paste0("$", Amount)),
                         position = position_stack(vjust = 0.5),
                         hjust = -3.1,
                         size = 5) +
               geom_text(aes(label = Label),
                         position = position_stack(vjust = 0.5),
                         hjust = 5,
                         size = 5) +
               theme(legend.position = "none") +
               theme(plot.title = element_text(size = 50, hjust = .5, vjust = 0)) +
               ggtitle("Food Costs by Reindeer")

enter image description here

like image 751
David M Vermillion Avatar asked Jan 25 '26 09:01

David M Vermillion


2 Answers

hjust determines the text alignment (with 0 being left-aligned, and 1 right-aligned). The x co-ordinate of your geom_text at the moment is defaulted to 1, so changing this will change the position of the text.

ggplot(level_order, aes(fill=fct_inorder(Label), y=Amount, x="")) + 
  geom_bar(position="stack", stat="identity", width = 0.55) +
  scale_fill_brewer(palette = "Blues", direction = -1) +
  theme_void() +
  geom_text(aes(x=0.6, label = paste0("$", Amount)),
            position = position_stack(vjust = 0.5),
            hjust = 0.5,
            size = 5) +
  geom_text(aes(x=1.4, label = Label),
            position = position_stack(vjust = 0.5),
            hjust = 0.5,
            size = 5) +
  theme(legend.position = "none") +
  theme(plot.title = element_text(size = 50, hjust = .5, vjust = 0)) +
  ggtitle("Food Costs by Reindeer")
like image 150
nrennie Avatar answered Jan 27 '26 00:01

nrennie


You can also pass hjust as an aesthetic. In order to do that, you will need to prepare the labelling as a separate data frame. Then, you only need to call geom_text once. I don't say this is necessarily better, but just pointing out that this is possible. A few more comments in the code, also regarding a few common pitfalls.

library(tidyverse)
df3 <- data.frame(
  Label = c("Dasher", "Dancer", "Comet", "Cupid", "Prancer", "Blitzen", "Rudolph"),
  Amount = c(650.01, 601.01, 340.05, 330.20, 260.01, 250.80, 10.10)
) %>% 
  ## arrange step here
  arrange(desc(Amount)) 

## I like to prepare the data outside ggplot
label_df <- df3 %>% 
  mutate(Amount_lab = paste0("$", Amount)) %>%
  pivot_longer(-Amount) %>%
## this adds a column for your adjustment, and the x position compared with the central column
  mutate(hjust = rep(0:1, nrow(.)/2), 
         x = rep(c(1.21, .79), nrow(.)/2))

ggplot(mapping = aes(y = Amount)) + 
  ## geom_col is geom_bar(stat = "identity"), stack is default, so you can omit it
  ## call data in the geom layers
  ## set x to 1
  ## width = .4 so it matches your selected x from above
  geom_col(data = df3, aes(x = 1, fill=fct_inorder(Label)), width = .4) +
  scale_fill_brewer(palette = "Blues", direction = -1) +
  ## need to reverse both y and value, weirdly 
  geom_text(data = label_df, aes(x, y = rev(Amount), label = rev(value), 
## this is the main trick
  hjust = hjust),
  position = position_stack(vjust = 0.5) ) +
  ## sadly, need to turn clip off
  coord_cartesian(clip = "off") +
  theme_void() +
## call theme only once!!
  theme(legend.position = "none",
        plot.title = element_text(size = 20, hjust = .5, vjust = 0),
        ## you need to add a margin 
        plot.margin = margin(r = .6, l = .6, unit = "in")) +
  ggtitle("Food Costs by Reindeer")

Created on 2021-12-20 by the reprex package (v2.0.1)

like image 40
tjebo Avatar answered Jan 26 '26 23:01

tjebo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!