拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 在具有大量资料的每个堆栈条中添加值

在具有大量资料的每个堆栈条中添加值

白鹭 - 2022-02-10 2114 0 0

我尝试在ggplot2. 我有 476970 个观察值。我做了一个 stackerd bar,但我需要把总数放在每个方块的中间

对于每个月和每年,我都有很多值。

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

在具有大量资料的每个堆栈条中添加值

我通过结果添加 geom_text 是非常错误的

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

我试了一下,geom_cor()但结果是一样的。

我在论坛中证明了 diffente 选项,但代码运行不佳。

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

谢谢

uj5u.com热心网友回复:

我不确定总数的意思,但你可以试试

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

在具有大量资料的每个堆栈条中添加值

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *