Skip to content

Instantly share code, notes, and snippets.

@jsajuria
Created April 12, 2021 11:30
Show Gist options
  • Save jsajuria/ab437c77bc4823de99b5a08a84f11f22 to your computer and use it in GitHub Desktop.
Save jsajuria/ab437c77bc4823de99b5a08a84f11f22 to your computer and use it in GitHub Desktop.
Script para crear diagrama de transferencia de votos (Sankey Plot) a partir de Cadem N378
# Library
library(networkD3)
library(dplyr)
# Cargar la base con origen y destino
links <- read.csv("https://www.sajuria.com/files/jilescsv.csv")
# Crear los nodos de la red
nodes <- data.frame(
name=c(as.character(links$Source), as.character(links$Target)) %>%
unique()
)
#Prepara los datos para networkD3
links$IDsource <- match(links$Source, nodes$name)-1
links$IDtarget <- match(links$Target, nodes$name)-1
# Crea la red
p <- sankeyNetwork(Links = links, Nodes = nodes, Source = "IDsource", Target = "IDtarget",
Value = "Value", NodeID = "name", fontSize = 10, fontFamily = "Arial",
height = 600, width = 800)
#, colourScale=my_color
p
# guarda el widget
library(htmlwidgets)
saveWidget(p, file=paste0( getwd(), "/jiles.html"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment