Reorder bars in ggplot2 according to one of the fill variables (Fill)

Asked

Viewed 213 times

2

Hello,

I have a graph that shows the proportion of equipment available in schools of Pernambuco, and I would like to order it from the equipment with greater presence (greater has) to those with lower presence:

inserir a descrição da imagem aqui

Currently my code is like this:

df %>%
  ggplot(aes(x=equipamento ,y=perc, fill=situacao), arrange(situacao)) +
  geom_col() +
  ggtitle("Percentual de creches que atendem aos parâmetros do CAQi") + 
  theme_bw() + theme(axis.text.x = element_text(angle = 45, hjust = 1))

Given the layout of my data, I do not know if it is possible to reorder using factor:

inserir a descrição da imagem aqui

I appreciate the help!

2 answers

1

Why not use fct_reorder2? With this, you can sort the two variables preferably.

df %>%
 mutate(equipamento = fct_reorder2(equipamento,situacao, perc)) %>%
  ggplot(aes(x=equipamento ,y=perc, fill=situacao), arrange(situacao)) +
  geom_col() +
  ggtitle("Percentual de creches que atendem aos parâmetros do CAQi") + 
  theme_bw() + theme(axis.text.x = element_text(angle = 45, hjust = 1))

1

Use the fct_reorder function of the forcats package for this.

df %>%
  mutate(equipamento, = fct_reorder(equipamento, perc)) %>%
  ggplot(aes(x=equipamento ,y=perc, fill=situacao), arrange(situacao)) +
  geom_col() +
  ggtitle("Percentual de creches que atendem aos parâmetros do CAQi") + 
  theme_bw() + theme(axis.text.x = element_text(angle = 45, hjust = 1))
  • William, that’s about it. William fct_reorder reordered by the highest percentage, so the kitchen was in first but the rest was in ascending order of does not possess (because they are the highest percentages), and what I would like to be in descending order of "has" .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.