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

Viewed 231

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

4 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")

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)

Try fixing the x co-ordinate in the call to geom_text and managing alignment with hjust...

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)
)


library(ggplot2)
library(dplyr)
library(forcats)

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(x = 1.3, label = paste0("$", Amount)),
            position = position_stack(vjust = 0.5),
            hjust = 0,
            size = 5) +
  geom_text(aes(x = 0.6, label = Label),
            position = position_stack(vjust = 0.5),
            hjust = 0,
            size = 5) +
  theme(legend.position = "none") +
  theme(plot.title = element_text(size = 50, hjust = .5, vjust = 0)) +
  ggtitle("Food Costs by Reindeer")

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

Using Peter's answer above (reminding me of the "x" position argument I forgot existed), this was the final fix that got what I was looking for. hjust = 0 is left-justification and hjust = 1 is right justification.

library(tidyverse)
library(grid)

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(x = 1.3, label = paste0("$", Amount)),
                         position = position_stack(vjust = 0.5),
                         hjust = 0,
                         size = 5) +
               geom_text(aes(x = 0.7, label = Label),
                         position = position_stack(vjust = 0.5),
                         hjust = 1,
                         size = 5) +
               theme(legend.position = "none",
                     plot.margin = unit(c(0,0,2,0), "cm"))
grid.text("Food Costs by Reindeer", x = unit(0.5, "npc"), y = unit(0, "npc"),
          vjust = -0.5, gp = gpar(cex=4))

Solution Result

Related