0

Estoy intentando ordenar los estratos de un plot alluvial pero no puedo. Intento darles un nivel pero sigue saliendo la misma escala

  dput(ggplot_alluvial_PHQ9)


  structure(list(Var_tiempo = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 
  2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 5L), levels = c("0 Months Baseline", 
  "3 Months using ETI", "6 Months using ETI", "9 Months using ETI", 
  "12 Months using ETI"), class = "factor"), values = structure(c(1L, 
  2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 
  1L, 2L, 3L, 5L), levels = c("Normal", "Mild", "Moderate", "Moderately severe", 
  "Severe"), class = "factor"), per = c(63.2075471698113, 23.5849056603774, 
  11.3207547169811, 0.943396226415094, 0.943396226415094, 70.1754385964912, 
  22.8070175438597, 3.50877192982456, 3.50877192982456, 69.4736842105263, 
  23.1578947368421, 4.21052631578947, 1.05263157894737, 2.10526315789474, 
  78.6885245901639, 13.1147540983607, 8.19672131147541, 66.6666666666667, 
  24.7311827956989, 7.52688172043011, 1.0752688172043)), row.names = c(NA, 
  -21L), class = c("tbl_df", "tbl", "data.frame"))



  ggplot_alluvial_PHQ9 %>% 
    mutate(values=factor(values,
                   levels = c("Normal",
                              "Mild",
                              "Moderate",
                              "Moderately severe",
                              "Severe")))->ggplot_alluvial_PHQ9


  ggplot(ggplot_alluvial_PHQ9,
   aes(x = Var_tiempo, 
       stratum=paste0(values,sprintf("\n%0.1f",round(per, digits = 1)), "%"),
       # stratum=round(per, digits = 3),
       alluvium = values,
       y = per,
       fill = values, label = values)) +
    scale_x_discrete(expand = c(.1, 0)) +
    geom_flow(width = 1/4, aes.flow = "backward") +
    geom_stratum(alpha = .5, width = 1/4, position = "identity") +
    geom_text(stat = "stratum", aes(label = after_stat(stratum)),size = 3,discern=FALSE)+
    labs(x="",y="")+
    guides(fill = guide_legend(title = NULL)) +
    theme(legend.position = "none")->plot3;plot3

introducir la descripción de la imagen aquí

¿Cómo puedo ordenarlas? Gracias de antemano

1 respuesta 1

0

La respuesta era ordenar la variable que se introducía en stratum. Como hacía un paste esta se quitaba su orden previamente impuesto.

Por lo que hacemos antes el paste y ordenamos la columna nueva, y ejecutamos en stratum esta columna nueva:

     ggplot_alluvial %>% 
     filter(Name == "GAD7") %>% 
     select(-c("sum", "Name")) %>% 
     mutate(values = case_when(
     values == 0 ~ "Normal",
     values == 1 ~ "Mild",
     values == 2 ~ "Moderate",
     values == 3 ~ "Severe",
     TRUE ~ as.character(NA)
     ),
     values2 = paste0(values," ",sprintf("%0.1f",round(per, digits = 1)), "%"),
     values2=factor(values2,
             levels = c("Normal 63.2%",
                        "Normal 73.7%",  
                        "Normal 62.1%",
                        "Normal 70.0%",
                        "Normal 60.4%",
                        "Mild 25.5%",
                        "Mild 12.3%",
                        "Mild 23.2%",
                        "Mild 21.7%",
                        "Mild 25.3%",
                        "Moderate 6.6%" ,
                        "Moderate 12.3%",
                        "Moderate 8.4%",
                        "Moderate 6.7%",
                        "Moderate 11.0%",
                        "Severe 4.7%",
                        "Severe 1.8%",
                        "Severe 6.3%" ,  
                        "Severe 1.7%",
                        "Severe 3.3%"
                        ))) -> ggplot_alluvial_GAD7


     mis_colores2 <- c("#f7958e", "#b6b83c", "#76cef0", "#e98cf3")

     ggplot(ggplot_alluvial_GAD7,
   aes(x = Var_tiempo, 
       stratum=values2,
       alluvium = values,
       y = per,
       fill = values, label = values)) +
       scale_x_discrete(expand = c(.1, 0)) +
       geom_flow(width = 1/4) +
       geom_stratum(alpha = .5, width = 1/4) +
       geom_text(stat = "stratum", aes(label = after_stat(stratum)),size = 4,discern=FALSE)+
       labs(x="",y="")+
       scale_fill_manual(values = mis_colores2) +
       guides(fill = guide_legend(title = NULL)) +
       theme(legend.position = "none",
    axis.text.x = element_text(angle = 0, hjust = 0.5,size=12),
    axis.text = element_text(face = "bold", hjust = 0.5)) ->plot2;plot2

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.