0

I produce this simple table with R code below:

# A tibble: 9 × 3
  report_year bank_financial amount
  <fct>       <chr>           <dbl>
1 2013        NPL %          0.0266
2 2014        NPL %          0.0224
3 2015        NPL %          0.0176
4 2016        NPL %          0.0164
5 2017        NPL %          0.0120
6 2018        NPL %          0.0113
7 2019        NPL %          0.0101
8 2020        NPL %          0.0137
9 2021        NPL %          0.0118

convetional_npl <- df %>% filter((bank_financial == 'Total Impaired Loan' | bank_financial == 'Gross Loan'), category == 'Universal Commercial Banks') %>%
  group_by(report_year, bank_financial) %>% summarise(amount = sum(amount), .groups = 'drop') %>%
  pivot_wider(names_from = bank_financial, values_from = amount) %>%
  mutate(report_year = as_factor(report_year),
         `NPL %` = `Total Impaired Loan` / `Gross Loan`) %>%
  pivot_longer(!report_year, names_to = 'bank_financial', values_to = 'amount') %>%
  filter(bank_financial == 'NPL %')

Try to create a line chart with

convetional_npl %>% ggplot(aes(report_year, bank_financial)) + geom_line()

R studio prompted me with warning message below

`geom_line()`: Each group consists of only one observation.
ℹ Do you need to adjust the group aesthetic?

The code produce this chart enter image description here

Hope someone can advise me on this warning message and produce the complete chart.

stefan
  • 90,330
  • 6
  • 25
  • 51
Ong K.S
  • 229
  • 1
  • 4
  • 15
  • Your `report_year` is factor, may be convert to numeric i.e. `convetional_npl %>% mutate(report_year = as.numeric(as.character(report_year))) %>% ggplot(aes(report_year, amount)) + geom_line()` – akrun Feb 06 '23 at 03:56
  • 1
    Have you tried with `group = 1` ? https://stackoverflow.com/questions/27082601/ggplot2-line-chart-gives-geom-path-each-group-consist-of-only-one-observation – Ronak Shah Feb 06 '23 at 03:58
  • Also, the second column bank_financial is character. You may want the `amount` column as `y`? – akrun Feb 06 '23 at 03:58
  • @akrun convert report_year to numeric type works and thanks for pointing out my silly mistake for the y = bank_financial. Mind to explain why report_year in factor type gives that warning numeric type doesn't?? – Ong K.S Feb 06 '23 at 04:45
  • @RonakShah TQ so much. It works. I see the explanation in Cookbook for R about group = 1. Wonder about the meaning 1 ==> is it 1st column, 1 column?? – Ong K.S Feb 06 '23 at 04:51
  • The "1" has no special meaning. Using `group=1` means to treat all observations as belonging to one group which we name `1`. You could have also used `group=2` or `group="one"` or ... – stefan Feb 06 '23 at 07:48

0 Answers0