Home > Mobile >  Add values in each stacked bar with extensive data
Add values in each stacked bar with extensive data

Time:12-22

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

enter image description here

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.

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

Thanks,

CodePudding user response:

I'm not sure about Total number means but you may try

library(dplyr)
library(ggplot2)

dt2 <- dt %>%
  group_by(ANO, MES) %>%
  summarize(CANTIDAD = sum(CANTIDAD))

ggplot(dt,aes(x=MES,y=CANTIDAD, fill= ANO,label=CANTIDAD))  
  geom_bar(stat = 'identity')  
  geom_text(data = dt2, position = position_stack(vjust = .5), aes(x = MES, y = CANTIDAD, label = CANTIDAD, hjust = 0.5))

enter image description here

  • Related