Column chart not reaching full value (ggplot)

Beginner here. I am having some difficulty with my column chart (ggplot2): one of the columns is cut in half/does not reach the value it should (column for year 2015 should reach value of 43.96). For context, I am trying to create a column chart where the labels give the TOTAL amount by year and region (hence use of stat = 'summary', fun = sum). Below are my data and the code I have used to generate the plot. Any help would be appreciated!

Data:
COMPANY CURRENCY CNTRY YEAR AMT REG
movado_group_inc USD US 2015 30000000 North America
urban_outfitters USD US 2015 13960000 North America
lerner_new_york USD US 2014 8200000 North America
sri_rejeki_isman_tbk USD ID 2019 50000000 Asia
VF_corp USD US 2018 24100000 North America

Code:
'''r
ggplot(sus_loans_2, aes(x=factor(YEAR),y=AMT/1000000, fill=REG))+
geom_col(position=position_dodge()) +labs(x="Year",y="Amount, $MM",
title="Sustainability-Linked Loans: Textile Companies",
fill="Region") +
theme_minimal() +
geom_text_repel(
aes(label = stat(y), group = factor(YEAR)),
stat = 'summary', fun = sum
)
'''

This works. You weren't summarising your data by the groups (year and region).

library(tidyverse)
library(ggrepel)

# your data
sus_loans_2 <- tibble::tribble(
                               ~COMPANY, ~CURRENCY, ~CNTRY, ~YEAR,      ~AMT,            ~REG,
                     "movado_group_inc",     "USD",   "US", 2015L, 30000000L, "North America",
                     "urban_outfitters",     "USD",   "US", 2015L, 13960000L, "North America",
                      "lerner_new_york",     "USD",   "US", 2014L,  8200000L, "North America",
                 "sri_rejeki_isman_tbk",     "USD",   "ID", 2019L, 50000000L,          "Asia",
                              "VF_corp",     "USD",   "US", 2018L, 24100000L, "North America"
                 )

# grouped and summarised data
sus_loans_3 <- sus_loans_2 %>% 
  group_by(REG, YEAR) %>% 
  summarise(AMT = sum(AMT))
  
# edited plot
ggplot(sus_loans_3, aes(x=factor(YEAR),y=AMT/1000000, fill=REG))+
  geom_col(position=position_dodge()) +labs(x="Year",y="Amount, $MM",
                                            title="Sustainability-Linked Loans: Textile Companies",
                                            fill="Region") +
  theme_minimal() +
  geom_text_repel(
    aes(label = stat(y), group = factor(YEAR)),
    stat = 'summary', fun = sum)

That fixed it, thank you.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.