【问题标题】:googleVis sankey-diagrams not displaying correctlygoogleVis sankey-diagrams 显示不正确
【发布时间】:2015-11-10 13:45:56
【问题描述】:

我能够创建一个桑基图。然而,当渲染时只有一半的情节出现在网络浏览器中:

关于如何在不过多使用 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)

【问题讨论】:

  • 我们无法复制您的错误(如果是错误),因为您的代码意味着读取我们没有的外部文件。发布重现错误的代码。
  • @SabDeM:见第 4 行和第 5 行代码。文件位于:ben.epe.gov.br/downloads/…
  • @SabDeM:很抱歉下载没有完全自动化,download.file() 给了我一些其他错误。 tks.

标签: r googlevis sankey-diagram


【解决方案1】:

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

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'}}}")))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-27
    • 2016-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多