How to sort my bar chart per day of the week on R

Asked

Viewed 39 times

-1

Hello, Good afternoon.

I need a help to sort by day of the week on R my bar chart, as below:

inserir a descrição da imagem aqui

The code I used is as follows::

date_manip %>% na.omit() %>% ggplot(aes(x=Dia_week), group=1) + geom_bar(aes(x=Weekday)) + ggtitle("Travel by day of the week") + theme_clean()

Thank you,

Wil

  • Welcome to Stackoverflow! Unfortunately, this question cannot be reproduced by anyone trying to answer it. Please take a look at this link (mainly in the use of function dput) and see how to ask a reproducible question in R. So, people who wish to help you will be able to do this in the best possible way.

2 answers

0


First you can sort the data by day of the week:

date_manip$Dia_semana <- ordered(date_manip$Dia_semana, levels=c("domingo", "segunda-feira", "terça-feira", "quarta-feira", "quinta-feira", "sexta-feira", "sábado"))

And then the chart:

date_manip %>% na.omit() %>% ggplot(aes(x=Dia_semana), group=1) + geom_bar(aes(x=Dia_semana)) + ggtitle("Viagens por dia da semana") + theme_clean()

  • Maravilha @Josiane Souza! It worked super well. Thank you so much for sharing your knowledge! :-)

-1

Hello, just change the data used in the X-axis to factor.

Below is a possible resolution:

date_manip %>% na.omit() %>% mutate(Dia_semana, levels = c("segunda-feira","terça-feira","quarta-feira","quinta-feria","sexta-feira","sábado","domingo")) %>% ggplot(aes(x=Dia_semana), group=1) + geom_bar(aes(x=Dia_semana)) + ggtitle("Viagens por dia da semana") + theme_clean()

  • Hello @hamers! Your solution is also good. It helped me a lot! Thank you

Browser other questions tagged

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