1

I did a sankey diagram in R using the ggsankey package and everything is fine, only detail is I want to rearrenge the order of the nodes in the second level (right now it is arranged SD, PR, DP, and CR from top to bottom and i want them to appear CR, PR, SD, DP from top to bottom). Is there a way to customize the position?

enter image description here

This is the code i used to generate the plot

devtools::install_github("davidsjoberg/ggsankey")
library(ggsankey)

sankey_df <- base_limpia%>% transmute(
  Treatment = Type.of.Treatment,
  Response = Respuesta,
  Transplantation = Post_therapy_trasplantation
)

sankey_df <- sankey_df %>% make_long(Treatment,
                                     Response,
                                     Transplantation
)

reagg <- sankey_df%>%
  dplyr::group_by(node)%>%  # Here we are grouping the data by node and then we are taking the frequency of it 
  tally()

sankey_df2 <- merge(sankey_df,
             reagg, 
             by.x = 'node', 
             by.y = 'node', 
             all.x = TRUE)
png(file = "Figure 6.png", width = 3200, height = 2000, res = 300)
plot <- ggplot(sankey_df2, aes(x = x,                        
                     next_x = next_x,                                     
                     node = node,
                     next_node = next_node,        
                     fill = factor(node),
                     label = paste0(node, " = ", n)))

plot <- plot + geom_sankey(flow.alpha = 0.5,          
                      node.color = "black",        
                      show.legend = F) + 
  geom_sankey_label(size = 3,
                    color = "black",
                    fill = "white") +
  theme_bw() +
  theme(legend.position = "none") +
  theme(axis.title = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks = element_blank(),
        panel.grid = element_blank())
Phil
  • 7,287
  • 3
  • 36
  • 66
vaquera
  • 11
  • 1

0 Answers0