【问题标题】:R / ggplot2 / add total value(sum) above barplot with each value in fillR / ggplot2 / 在条形图上方添加总值(总和),每个值都填充
【发布时间】:2020-02-01 06:20:02
【问题描述】:

我想在栏上方添加“总和”值的文本。

fac_report_field %>% 
 ggplot(aes(x = m, y = total, fill = field))+
 geom_bar(stat="identity")+
 scale_x_continuous(breaks = seq(1:12))+
 geom_text(aes(label = total), stat = "identity" , size = 1.5,
        position=position_stack(0.5))+
 facet_grid(.~y)

enter image description here

head(fac_report_field)
 # A tibble: 6 x 9
 # Groups:   y, m, ym [3]
  y         m ym     field    total  paid others opinion plc_ratio
 <fct> <dbl> <chr>  <fct>    <int> <int>  <int>   <int>     <dbl>
1 2016      1 201601 Non_life   499   143    355       1      28.7
2 2016      1 201601 Life       297   100    189       8      34.6
3 2016      2 201602 Non_life   433   128    305       0      29.6
4 2016      2 201602 Life       253    82    166       5      33.1
5 2016      3 201603 Non_life   609   176    433       0      28.9
6 2016      3 201603 Life       336    94    235       7      28.6

我制作了新的数据集 'fac_report_sum' 如下

> head(fac_report_sum)
# A tibble: 6 x 3
# Groups:   y [1]
      y     m   Sum
  <dbl> <dbl> <int>
1  2016     1   796
2  2016     2   686
3  2016     3   945
4  2016     4   698
5  2016     5  1003
6  2016     6  1201

并添加 geom_text 如下

fac_report_field %>%
ggplot(aes(x = m, y = total, fill = field))+
geom_bar(stat="identity")+
scale_x_continuous(breaks = seq(1:12))+
geom_text(aes(label = total), stat = "identity" , size = 1.5,
      position=position_stack(0.5))+
geom_text(data = fac_report_sum, aes(label = sum, x = m, y = sum),
        vjust = -.25, size = 1.5)+
facet_grid(.~y)

但是有如下错误

Error in FUN(X[[i]], ...) : object 'field' not found

请指导我如何解决这个问题。 非常感谢。

【问题讨论】:

标签: r ggplot2 sum bar-chart add


【解决方案1】:

我认为您在第二个geom_text 中缺少inherit.aes = FALSE。简而言之,在您的第二个 geom_text 中,您正在调用一个新的数据框,但是 ggplot 仍在寻找您在第二个数据框中的第一个 aes 中设置的填充类别 field。由于它不存在,因此会打印出错误。

如果您使用inherit.aes = FALSE,它将向ggplot 指示不考虑以前的映射参数(注意:我将dfsum 子集以匹配第一个数据帧中m 的值):

library(ggplot2)
ggplot(df_report, aes(x = m, y = total, fill= field))+
  geom_col()+
  geom_text(aes(label = total),
            position=position_stack(0.5))+
  geom_text(inherit.aes = FALSE, data = subset(dfsum, m %in% 1:3), 
            aes(x = m, y = Sum, label = Sum), vjust = -0.25)+
  ylim(0,1000)

示例数据

df_report(相当于你的fac_report_field):

structure(list(Row = 1:6, y = c(2016L, 2016L, 2016L, 2016L, 2016L, 
2016L), m = c(1L, 1L, 2L, 2L, 3L, 3L), ym = c(201601L, 201601L, 
201602L, 201602L, 201603L, 201603L), field = c("Non_life", "Life", 
"Non_life", "Life", "Non_life", "Life"), total = c(499L, 297L, 
433L, 253L, 609L, 336L), paid = c(143L, 100L, 128L, 82L, 176L, 
94L), others = c(355L, 189L, 305L, 166L, 433L, 235L), opinion = c(1L, 
8L, 0L, 5L, 0L, 7L), plc_ratio = c(28.7, 34.6, 29.6, 33.1, 28.9, 
28.6)), row.names = c(NA, -6L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x55eb576ff5c0>)

dfsum(相当于你的fac_report_sum):

structure(list(Row = 1:6, y = c(2016L, 2016L, 2016L, 2016L, 2016L, 
2016L), m = 1:6, Sum = c(796L, 686L, 945L, 698L, 1003L, 1201L
)), row.names = c(NA, -6L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x55eb576ff5c0>)

【讨论】:

    【解决方案2】:

    如果您提供fac_report_field 或至少每个col()str() 会更好

    试试

    geom_text(aes(label = field), x = m, y = total)
    

    ?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-19
      • 1970-01-01
      • 2016-12-14
      • 2023-02-21
      • 2019-03-10
      相关资源
      最近更新 更多