How to ordering plot columns in numerical order considering groups

Hello Guys,
I have this code below and i would like to ordering the positions of columns by decreasing value inside of each group (Subcategoria), so for each group i want the values from higher to lower.

Could someone help me, please? Thank you very much!

library(ggplot2)
library(plotly)
library(fmsb)

library(tidyverse)

Tibble

TB1 <-tribble(
~Codigo, ~Valor, ~Descricao, ~Categoria, ~Subcategoria, ~Grupo,
1, 4, "aaa", "A", "a", "I",
2, 13, "bbb", "A", "a", "II",
3, 24, "ccc", "A", "b", "I",
4, 36, "ddd", "A", "a", "III",
5, 55, "eee", "A", "a", "I",
6, 89, "fff", "A", "c", "I",
7, 113, "ggg", "A", "d", "I",
8, 313, "hhh", "A", "a", "IV",
9, 51, "hhh", "A", "b", "II",
10, 69, "hhh", "A", "b", "II",
11, 100, "hhh", "A", "b", "IV",
12, 17, "hhh", "A", "b", "V",
13, 9, "hhh", "A", "c", "II",
14, 215, "hhh", "A", "c", "III",
15, 73, "hhh", "A", "c", "IV",
16, 81, "hhh", "A", "c", "IV",
17, 27, "hhh", "A", "d", "II",
18, 66, "hhh", "A", "d", "III",
19, 108, "hhh", "A", "d", "IV",
20, 310, "hhh", "A", "d", "V",
)

DADOS <- TB1 %>% group_by(Categoria, Subcategoria, Grupo) %>% summarise(Soma = sum(Valor)) %>%
mutate(Subcategoria = factor(Subcategoria))

DADOS %>%
ggplot(aes(x = Subcategoria, y = Soma, fill = Grupo, label = Soma)) +
geom_bar(stat = "identity", position = position_dodge2(width = 0.9, preserve = 'single'))+
geom_text(position=position_dodge2(width = 0.9, preserve = "single"), vjust=-0.5, size=3.5)

Add this: group = -Soma

1 Like

Genius! It works! Thanks a lot.

To be honest I was surprised it was so easy :smiley:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.