Failing to reorder factors

Hi,

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,
                                   80,80,80,80,80,80,80,80,
                                   77,77,77,77,77,77,77,77,
                                   77,77,77,77,77,77,77,77,77),
                          sectores=c("E","ENE","ESE","N","NE",
                                     "NNE","NNW","NW","S","SE",
                                     "SSE","SSW","SW","W","WNW",
                                     "WSW","E","ENE","ESE","N",
                                     "NE","NNE","NNW","NW","S",
                                     "SE","SSE","SSW","SW","W","WNW","WSW","WSW"),
                          V=c(2.715,2.862,2.119,3.094,2.893,4.211,
                              3.230,2.722,7.157,2.654,4.741,5.781,
                              4.372,2.801,2.448,3.781,2.742,2.816,
                              2.068,2.919,2.720,3.657,3.177,2.702,
                              6.975,2.603,4.617,5.642,4.294,2.772,2.420,3.729,3.729))

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",
             "SSW","SW","WSW","W","WNW","NW","NNW")

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) %>% 
  radarchart::chartJSRadar(showToolTipLabel=TRUE)

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`)) %>% 
  radarchart::chartJSRadar(showToolTipLabel=TRUE)

The labels should be like in this example.
ejemplo

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) %>% 
  radarchart::chartJSRadar(showToolTipLabel=TRUE)

@bisulcsm
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.