library(highcharter)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
#Create data which can be used for Sankey

set.seed(111)

t1 <- sample(x = c("Hosp A", "Hosp B", "Hosp C") , size = 100, replace=TRUE)
t2 <- sample(x = c("Male", "Female")   , size = 100, replace=TRUE)
t3 <- sample(x = c("Survived", "Died") , size = 100, replace=TRUE)

d <- data.frame(cbind(t1,t2,t3))

names(d) <- c('Hospital', 'Gender', 'Outcome')
head(d)
##   Hospital Gender  Outcome
## 1   Hosp B   Male Survived
## 2   Hosp C Female Survived
## 3   Hosp C Female     Died
## 4   Hosp C   Male     Died
## 5   Hosp A   Male     Died
## 6   Hosp C Female Survived
# First Sankey diagram
hchart(data_to_sankey(d), "sankey", name = "Hospital and Gender based Outcomes")
# Second Sankey diagram

dataForSankey <- d%>%dplyr::select(Hospital, Outcome)
hchart(data_to_sankey(dataForSankey), "sankey", name = "Hospital based Outcomes")
# Third Sankey Diagram

dataForSankey <- d%>%dplyr::select(Gender, Outcome)
hchart(data_to_sankey(dataForSankey), "sankey", name = "Gender based Outcomes")
pl <- hchart(data_to_sankey(d), "sankey", name = "Patient Outcomes")
pl
pl%>%
  hc_title(text= "Sankey Diagram") %>%
  hc_subtitle(text= "Hospital and Gender based outcomes")  %>%
  hc_caption(text = "<b>This is my caption at the bottom.<b>")%>%
  hc_add_theme(hc_theme_economist())
## save to html
library(htmlwidgets)
htmlwidgets::saveWidget(widget = pl, file = "C:\\Users\\liyix\\OneDrive\\Desktop\\pl.html")
## save html to png

#webshot::webshot(url = "D:\\tmp\\map.html",
#                 file = "D:\\tmp\\map.png" )
#png('D:\\tmp\\hc.png', width = 800,height = 400)
#print(pl)
#dev.off()

#ref https://rpubs.com/techanswers88/sankey