googleVis sankey-diagrams无法正确显示

时间:2015-08-17 19:52:39

标签: r googlevis sankey-diagram

我能够创建一个Sankey图。但是,当渲染时,只有一半的绘图出现在Web浏览器中:

enter image description here

关于如何在不离开R语言的情况下正确渲染绘图的任何想法?

代码如下复制图表:

library(reshape2)
library(googleVis)

#Download the file at:
#https://ben.epe.gov.br/downloads/Matriz%20Energ%c3%a9tica%20Nacional%20ab2014.xlsx

#importing and formating the data
a <- read.xlsx('Matriz Energética Nacional ab2014.xlsx', sheetName = 'consolidada tep',
                    rowIndex = 24:49 , colIndex=2:10, header=FALSE) # startRow=4
b <- as.matrix(read.xlsx('Matriz Energética Nacional ab2014.xlsx', sheetName = 'consolidada tep',
                     rowIndex = 2:3, colIndex=2:10, header=FALSE, stringsAsFactors=FALSE)  )
b <- paste0(b[1,],b[2,])
colnames(a) <- b
c <- read.xlsx('Matriz Energética Nacional ab2014.xlsx', sheetName = 'consolidada tep',
           rowIndex = 24:49, colIndex=1:1, header=FALSE,     stringsAsFactors=FALSE) # startRow=4
a <- cbind(c,a)

#converting to long format
a2 <- melt(a,id='X1')[,c(2,1,3)]
colnames(a2) <- c('source','target','value')

#googleVis
g <-   gvisSankey(a2, from="source", 
              to="target", weight="value"      ,
              options=list(
                height=500,
                sankey="{link:{color:{fill:'lightblue'}}}"
              ))

cat(g$html$chart, file="tmp.html")
plot(g)

1 个答案:

答案 0 :(得分:2)

排除值为零的行会产生以下结果:

library(reshape2)
library(googleVis)
library(xlsx)

download.file("https://ben.epe.gov.br/downloads/Matriz%20Energ%c3%a9tica%20Nacional%20ab2014.xlsx", tf <- tempfile(fileext = ".xlsx"), mode = "wb")

a <- xlsx::read.xlsx(tf, sheetName = 'consolidada tep', rowIndex = 24:49 , colIndex=2:10, header=FALSE) # startRow=4
b <- as.matrix(xlsx::read.xlsx(tf, sheetName = 'consolidada tep', rowIndex = 2:3, colIndex=2:10, header=FALSE, stringsAsFactors=FALSE)  )
b <- paste0(b[1,],b[2,])
colnames(a) <- b
c <- xlsx::read.xlsx(tf, sheetName = 'consolidada tep', rowIndex = 24:49, colIndex=1:1, header=FALSE,     stringsAsFactors=FALSE) # startRow=4
a <- cbind(c,a)
a2 <- melt(a,id='X1')[,c(2,1,3)]
colnames(a2) <- c('source','target','value')

plot(gvisSankey(subset(a2, value > 0), from="source", to="target", weight="value0", options=list(height=500, sankey="{link:{color:{fill:'lightblue'}}}")))

enter image description here