Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ggplot – reorder_within – Order month

I am plotting a bar graph in RSTUDIO. However, the axes x show in different month order. Using the command reoder_within, doesn’t work for the purpose.

Follow below the ME.

ggplot(de, aes(fill=Cidade, y = Leitura  , x = Mes ))+geom_bar(position='dodge', stat='identity')

Generate the follow plot:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Plot

My purpose is modify the axes x to: Jan, Fev, March …..

The set of data is:

  Cidade    Mes       Leitura
  <chr>     <chr>       <dbl>
1 Petrolina Janeiro     74.2 
2 Petrolina Fevereiro   73.2 
3 Petrolina Março       68.7 
4 Petrolina Abril       42.9 
5 Petrolina Maio         9.84
6 Petrolina Junho        8.02

Thank you for help

Yours Faithfully

>Solution :

We can use fct_inorder here:

ggplot will order x axis alphapetically. To get the order in your table use fct_inorder:

library(ggplot2)
library(forcats)
library(dplyr)

de %>% 
  mutate(Mes = fct_inorder(Mes)) %>% 
  ggplot(aes(fill = Cidade, y = Leitura, x = Mes)) +
  geom_bar(position = 'dodge', stat = 'identity')

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading