Проблемы с коэффициентом переупорядочения в ggplot на основе другого столбца

#r #ggplot2 #group-by

#r #ggplot2 #группировка по

Вопрос:

У меня есть эти данные

    nome_movimento                               date_info     n total
   <fct>                                        <chr>     <int> <int>
 1 "scivolamento rotazionale"                   day         787  1875
 2 "scivolamento rotazionale"                   year        454  1875
 3 "scivolamento traslativo"                    day         434  1175
 4 "scivolamento rotazionale"                   no date     354  1875
 5 "scivolamento traslativo"                    year        339  1175
 6 "scivolamento traslativo"                    no date     290  1175
 7 "scivolamento rotazionale"                   month       280  1875
 8 "scivolamento traslativo"                    month       112  1175
 9 "colamento "rapido""                       day          56   105
10 "crollo"                                     day          51    97
11 "colamento "rapido""                       no date      30   105
12 "crollo"                                     no date      26    97
13 "Aree soggette a frane superficiali diffuse" no date      24    44
14 "Aree soggette a frane superficiali diffuse" year         18    44
15 "colamento "lento""                        year         16    44
16 "colamento "lento""                        no date      13    44
17 "colamento "lento""                        day          12    44
18 "colamento "rapido""                       year         12   105
19 "crollo"                                     month        11    97
20 "crollo"                                     year          9    97
 

и glimpse показывает, что это уже сгруппированный tibble:

 Rows: 42
Columns: 4
Groups: nome_movimento [13]
$ nome_movimento <fct> scivolamento rotazionale, scivolamento rotazionale, scivolamento traslativo, s...
$ date_info      <chr> "day", "year", "day", "no date", "year", "no date", "month", "month", "day", "...
$ n              <int> 787, 454, 434, 354, 339, 290, 280, 112, 56, 51, 30, 26, 24, 18, 16, 13, 12, 12...
$ total          <int> 1875, 1875, 1175, 1875, 1175, 1175, 1875, 1175, 105, 97, 105, 97, 44, 44, 44, ...
 

То, что я хочу сделать сейчас, — это какой-то сюжет, подобный этому:

 df %>%
  mutate(
    nome_movimento = as.factor(nome_movimento),
    nome_movimento = fct_reorder(nome_movimento, total)
  ) %>%
  ggplot(aes(n, nome_movimento))  
  geom_col(aes(fill = date_info))  
  labs(y = "Nome Movimento")
 

Что дает мне этот график:

введите описание изображения здесь

Но чего я хотел добиться fct_reorder , так это того, что «nome_movimento» с наибольшим n_total значением является самой верхней строкой и т. Д…

Я действительно думаю, что я что-то здесь напутал, но я не совсем уверен, что это такое…

Ответ №1:

Вы могли бы попробовать просто ungroup tibble:

 df %>%
  mutate(
    nome_movimento = as.factor(nome_movimento),
    nome_movimento = fct_reorder(nome_movimento, total)
  ) %>%
  ggplot(aes(n, nome_movimento))  
  geom_col(aes(fill = date_info))  
  labs(y = "Nome Movimento")
 

введите описание изображения здесь


Данные

 structure(list(nome_movimento = c("scivolamento rotazionale", 
"scivolamento rotazionale", "scivolamento traslativo", "scivolamento rotazionale", 
"scivolamento traslativo", "scivolamento traslativo", "scivolamento rotazionale", 
"scivolamento traslativo", "colamento "rapido"", "crollo", 
"colamento "rapido"", "crollo", "Aree soggette a frane superficiali diffuse", 
"Aree soggette a frane superficiali diffuse", "colamento "lento"", 
"colamento "lento"", "colamento "lento"", "colamento "rapido"", 
"crollo", "crollo"), date_info = c("day", "year", "day", "no date", 
"year", "no date", "month", "month", "day", "day", "no date", 
"no date", "no date", "year", "year", "no date", "day", "year", 
"month", "year"), n = c(787L, 454L, 434L, 354L, 339L, 290L, 280L, 
112L, 56L, 51L, 30L, 26L, 24L, 18L, 16L, 13L, 12L, 12L, 11L, 
9L), total = c(1875L, 1875L, 1175L, 1875L, 1175L, 1175L, 1875L, 
1175L, 105L, 97L, 105L, 97L, 44L, 44L, 44L, 44L, 44L, 105L, 97L, 
97L)), class = "data.frame", row.names = c("1", "2", "3", "4", 
"5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", "19", "20"))