Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201867

How to show frequency or value or number on the Sankey diagram in R

$
0
0

I want to show every bar's number on the sankey like the image I uploaded,which represents the population of the relevant treatment pathways. I tried a lot of methods to add labels but it doesn't work in networkD3.

enter image description here

the image source : https://onlinelibrary.wiley.com/doi/full/10.1002/pds.4874

enter code here
 s<-c( "A1","A1","B1","B1","B1","C1","C1","A2","B2","A2","B2","C2","A2" ,"A2","C2")
 t<-c("A2","B2","A2","B2","C2","A2","C2","C3","A3","A3","C3","C3","B3","C3" ,"B3")
 v<-c(4,2,3,2,1,7,2,3,2,2,2,1,1,3,2)
a<- data.frame(s,t,v)
a$s=as.character(a$s) 
a$t=as.character(a$t)

Sankeylinks<-a
Sankeynodes<-data.frame(name=unique(c(Sankeylinks$s,Sankeylinks$t)))

Sankeynodes$index<-0:(nrow(Sankeynodes)-1)   

Sankeylinks<-merge(Sankeylinks,Sankeynodes,by.x="s",by.y="name")

Sankeylinks<-merge(Sankeylinks,Sankeynodes,by.x="t",by.y="name")

Sankeydata<-Sankeylinks[,c(4,5,3)];
names(Sankeydata)<-c("Source","Target","Value")
Sankeyname<-Sankeynodes[,1,drop=FALSE]

Sankeyname$type2 <- sub(' .*', '',c("A","B","C","A","B","C","C","A","B")) 


my_color <- 'd3.scaleOrdinal() .domain(["A","B","C"]) 
                 .range(["#69b3a2","steelblue","red"])'

plot <- sankeyNetwork(Links=Sankeydata,Nodes=Sankeyname,Source="Source",
          Target="Target",Value="Value",NodeID="type2",    
          fontSize=12,nodeWidt=20, NodeGroup = "type2"  ,      
          sinksRight = FALSE,colourScale = my_color)

 plot  

Viewing all articles
Browse latest Browse all 201867

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>