Failing to reorder factors


I´m having trouble to stop the alphabetical reorder that R does with a variable.

This is an example of my df:

dist.direcc <- data.frame(Nivel =c(80,80,80,80,80,80,80,80,

I´m trying to get a radar plot where the variable sectores has cardinal directions and those are the labels of the plot.

I create the vector dir.fct to get the levels in the order I want.

dir.fct <- c("N","NNE","NE","ENE","E","ESE","SE","SSE","S",

For the radar plot I used the package radarchart and my code goes like this. I mutate the variable sectores as a factor with the levels from the vector dir.fct.

dist.direcc %>%
  mutate(sectores=factor(sectores, levels = dir.fct)) %>%
  pivot_wider(names_from = Nivel,values_from = V) %>% 

The plot that I get is this one.

If you can see, the labels are in alphabetical order but the variable sectores is supposed to be cardinal directions so N goes for North, NNE goes for North-NorthEast and so on. The plot should start with the N at the top.

I tried with forcats but I´m not sure how it works, I get the same or I move the N to the top but the values from V where in the same position.

dist.direcc %>%
  mutate(sectores=factor(sectores, levels = dir.fct)) %>%
  pivot_wider(names_from = Nivel,values_from = V) %>%
  mutate(sectores=fct_reorder(sectores,`80`)) %>% 

The labels should be like in this example.

And this is the same problem If I want to show the values as a table, the variable sectores is in alphabetical order.

Please some advice.

I think you need to arrange by the 'sectores' column. This worked for me:

dist.direcc %>%
  mutate(sectores=factor(sectores, levels = dir.fct)) %>%
  arrange(sectores) %>%
  pivot_wider(names_from = Nivel,values_from = V) %>% 

ok, that was easy.

I was drowning in a glass of water.

Thanks a lot.

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.