0

I want to add text of 'sum' value above the bar.

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

I made new data set 'fac_report_sum' as below

> 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

and add geom_text as below

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)

however there is an error as below

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

Please guide me how solve this problem. Many thanks in advance.

AndyIntae
  • 1
  • 2
  • 1
    Does this answer your question? [Showing data values on stacked bar chart in ggplot2](https://stackoverflow.com/questions/6644997/showing-data-values-on-stacked-bar-chart-in-ggplot2) – UseR10085 Feb 01 '20 at 06:38

2 Answers2

1

I think you are missing inherit.aes = FALSE in the second geom_text. Briefly, in your second geom_text, you are calling a new dataframe, however ggplot is still looking for the filling category field that you set in the first aes in this second dataframe. As it does not exist, it prints out an error.

If you used inherit.aes = FALSE, it will indicate to ggplot to not take in consideration of previous mapping arguments (NB: I subset dfsum to match the values of m in the first dataframe):

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)

enter image description here

Example Data

df_report (equivalent to your 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 (equivalent to your 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>)
dc37
  • 15,840
  • 4
  • 15
  • 32
0

It would be better if you provided fac_report_field or at least the str() of each col()

try

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

?

geom_na
  • 258
  • 2
  • 10