Add values in each stackerd bar whit extensive data

Hi,
I try to make a stackerd bar in ggplot2. I have 476970 observations. I maked a stackerd bar but I need put the total number in middle of each square

For each month and year I have many values.

library(ggplot2)
ggplot(dt,aes(x=MES,y=CANTIDAD, fill= ANO,label=CANTIDAD)) +
  geom_bar(stat = 'identity') 

image

Im add geom_text by the result is very wrong

library(ggplot2)
ggplot(dt,aes(x=MES,y=CANTIDAD, fill= ANO,label=CANTIDAD)) +
  geom_bar(stat = 'identity') +
geom_text(position = "stack", aes(x = MES, y = CANTIDAD, 
                                  label = CANTIDAD, hjust = 0.5))

# I try whit `geom_cor()` but the result was the same. 

image

I prove diffente opotion in forums but the code not working well.

This a dput() of my data

structure(list(CANTIDAD = c(1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 
2, 1, 3, 1, 2, 2, 1, 1, 1, 12, 1, 1, 2, 1, 1, 3, 1, 1, 1, 1, 
6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 4, 3, 1, 1, 1, 1, 
1, 1, 3, 1, 3, 1, 3, 1, 1, 3, 1, 10, 2, 1, 2, 1, 1, 2, 1, 8, 
1, 1, 1), ANO = c("2010", "2010", "2010", "2010", "2010", "2010", 
"2010", "2010", "2010", "2010", "2010", "2010", "2010", "2010", 
"2010", "2010", "2010", "2010", "2010", "2010", "2012", "2012", 
"2012", "2012", "2012", "2012", "2012", "2012", "2012", "2012", 
"2012", "2014", "2014", "2014", "2014", "2014", "2014", "2014", 
"2014", "2014", "2014", "2014", "2015", "2015", "2015", "2015", 
"2015", "2015", "2015", "2015", "2015", "2015", "2015", "2016", 
"2016", "2016", "2016", "2016", "2016", "2016", "2016", "2016", 
"2016", "2016", "2019", "2019", "2019", "2019", "2019", "2019", 
"2019", "2019", "2019", "2019", "2019"), MES = c("01", "01", 
"01", "01", "01", "01", "01", "01", "01", "01", "01", "01", "01", 
"01", "01", "01", "01", "01", "01", "01", "10", "10", "10", "10", 
"10", "10", "10", "10", "10", "10", "10", "02", "02", "02", "02", 
"02", "02", "02", "02", "02", "02", "02", "12", "12", "12", "12", 
"12", "12", "12", "12", "12", "12", "12", "10", "10", "10", "10", 
"10", "10", "10", "10", "10", "10", "10", "01", "01", "01", "01", 
"01", "01", "01", "01", "01", "01", "01")), row.names = c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
16L, 17L, 18L, 19L, 20L, 50000L, 50001L, 50002L, 50003L, 50004L, 
50005L, 50006L, 50007L, 50008L, 50009L, 50010L, 80000L, 80001L, 
80002L, 80003L, 80004L, 80005L, 80006L, 80007L, 80008L, 80009L, 
80010L, 150000L, 150001L, 150002L, 150003L, 150004L, 150005L, 
150006L, 150007L, 150008L, 150009L, 150010L, 190000L, 190001L, 
190002L, 190003L, 190004L, 190005L, 190006L, 190007L, 190008L, 
190009L, 190010L, 300000L, 300001L, 300002L, 300003L, 300004L, 
300005L, 300006L, 300007L, 300008L, 300009L, 300010L), class = "data.frame")

Tnks,

I think your issue is that your bars are actually loads of little bars stacked up. If you summarise your data ahead of the plot you get the result you're looking for:

library(tidyverse)

tibble(data) %>% 
  group_by(MES, ANO) %>% 
  summarise(CANTIDAD = sum(CANTIDAD)) %>% 
  ggplot(aes(
    x = MES,
    y = CANTIDAD,
    fill = ANO,
    label = CANTIDAD
  )) +
  geom_bar(stat = 'identity') +
  geom_text(
    position = position_stack(vjust = .5), 
    aes(
      x = MES,
      y = CANTIDAD,
      label = CANTIDAD,
    ))

1 Like

Thanks JackDavison, this is that I need.

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.