【问题标题】:Label ggplot geom_bar with total stacked bar values用总堆积条形值标记 ggplot geom_bar
【发布时间】:2020-09-30 10:17:19
【问题描述】:

我正在执行这段代码:

url <- 'http://pdet.mte.gov.br/images/Seguro-Desemprego/Segunda%20Quinzena%20de%20Maio/3-%20S%C3%A9rie%20Hist%C3%B3rica%20do%20Seguro-Desemprego%20-%202000%20a%202020%20-%20mensal.xlsx'
data <- rio::import(url, which = "Tabela 1")[-(1:4),] # import data from brazilian gov

# preparing the data
my_df <- t(data[(1),-1])
dates <- seq(as.Date('2000-01-01'), as.Date('2020-05-01'), by='1 month')
meses <- format(dates,"%b")
anos <- as.numeric(format(dates,"%Y"))
my_df <- data.frame(anos, meses, as.numeric(my_df))
colnames(my_df) <- c('year', 'month', 'amount')
my_df <- subset(my_df, month %in% c('jan', 'fev', 'mar', 'abr', 'mai'))
my_df$month <- factor(my_df$month, levels=c('jan', 'fev', 'mar', 'abr', 'mai')) 

> str(my_df)
'data.frame':   105 obs. of  3 variables:
 $ year  : num  2000 2000 2000 2000 2000 ...
 $ month : Factor w/ 5 levels "jan","fev","mar",..: 1 2 3 4 5 1 2 3 4 5 ...
 $ amount: num  343398 375906 394778 347326 386524 ...


# plot
ggplot(my_df, aes(y = year, x = amount/100000)) +
  geom_bar(aes(fill = month), stat = "identity", position = position_stack(reverse = TRUE)) +
  labs(title='Seguro-Desemprego: nº de requerimentos por mês',
       subtitle='Valores acumulados no ano (milhões), corte em maio',
       color = '', x = '', y = '') +
  theme(panel.background = element_blank(),
        panel.grid.minor = element_blank(),
        panel.grid.major = element_blank(),
        plot.background = element_rect(fill='moccasin'),
        plot.title = element_text(color = "red3", size = 19, face = "bold"),
        plot.subtitle = element_text(color = "red4", size = 12, face = "bold"),
        plot.caption = element_text(color = "red4", size = 11, face = "bold"),
        axis.line = element_blank(),
        axis.text.y = element_text(color = "red4", size = 11, face = "bold"),
        axis.text.x = element_blank(),
        axis.ticks=element_blank(),
        legend.position = "none") +
  scale_fill_brewer(palette="Set1", direction = -1) +
  scale_y_continuous(trans = "identity",
                     breaks = seq(from = 2000, to = 2020, by = 1),
                     expand = c(0,0)) +
  scale_x_continuous(expand = c(0,0)) +
  geom_text(aes(label = month), size = 4.6,  hjust = 1, vjust = .3, position = "stack", color = "white", fontface=2)

但我想将amount 变量的总数按year 放在每个条的末尾。也就是说,我想要整个堆叠条的总和(在右侧的情况下)。

我尝试在多个规范中使用stat_summarygeom_text,但没有得到想要的结果。有什么方法可以做到这一点而无需修改我的数据框?

编辑

好消息,我刚刚使用 stat_summary 得到了预期的结果,如下所示:

stat_summary(aes(label = stat(x)/10), fun = 'sum', geom = 'text', col = 'red4', hjust = -.25, vjust = .45, fontface = 2, position = "身份", size = 3.6)

【问题讨论】:

  • 你试过this specific approach吗?
  • 我试过了,但是这个过程没有用。但是,好消息,我得到了 stat_summary 的预期结果,如下所示: stat_summary(aes(label = stat(x)/10), fun = 'sum', geom = 'text', col = 'red4', hjust = -.25, vjust = .45, fontface = 2, position = "identity", size = 3.6)

标签: r ggplot2 geom-bar stacked-chart geom-text


【解决方案1】:

找到解决方案:

+ stat_summary(aes(label = stat(x)/10),
fun = 'sum',
geom = 'text',
col = 'red4',
hjust = -.25,
vjust = .45,
fontface = 2,
position = "identity",
size = 3.6)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-22
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多