2

当我在常规 R 会话中创建 sankey 图时,输出看起来还不错。工具提示在连接之间显示一个箭头:

require(rCharts)
require(rjson)

links <- matrix(unlist(
  rjson::fromJSON(
    file = "http://bost.ocks.org/mike/sankey/energy.json"
  )$links
),ncol = 3, byrow = TRUE)
nodes <- unlist(
  rjson::fromJSON(
    file = "http://bost.ocks.org/mike/sankey/energy.json"
  )$nodes
)

links <- data.frame(links)
colnames(links) <- c("source", "target", "value")
links$source <- sapply(links$source, FUN = function(x) {return(as.character(nodes[x+1]))}) #x+1 since js starts at 0
links$target <- sapply(links$target, FUN = function(x) {return(nodes[x+1])}) #x+1 since js starts at 0
sankeyPlot <- rCharts$new()
sankeyPlot$setLib('http://timelyportfolio.github.io/rCharts_d3_sankey')
sankeyPlot$set(
  data = links,
  nodeWidth = 15,
  nodePadding = 10,
  layout = 32,
  width = 960,
  height = 500,
  units = "TWh",
  title = "Sankey Diagram"
)
sankeyPlot

在此处输入图像描述

当我在 中创建它时shiny,工具提示中的箭头被不寻常的字符替换。同样在情节下方打印了一个不寻常的字符。我需要下载 d3_sankey 库以使闪亮的应用程序版本工作,所以如果你想重现它,你必须更改setLib语句中的路径。如何解决这个问题?

require(shiny)

runApp(list(
  ui = pageWithSidebar(
    headerPanel('Test'),
    sidebarPanel(  'Test'  ),
    mainPanel(
      chartOutput("Plot", 'C:/R-3.0.1/library/rCharts/libraries/sankey')  
    )
  ),
  server = function(input, output, session){

    output$Plot <-  renderChart2({
      sankeyPlot2 <- rCharts$new()
      sankeyPlot2$setLib('C:/R-3.0.1/library/rCharts/libraries/sankey')
      sankeyPlot2$set(
    data = links,
    nodeWidth = 15,
    nodePadding = 10,
    layout = 32,
    width = 960,
    height = 500,
    units = "TWh",
    title = "Sankey Diagram"
      )
      return(sankeyPlot2)
     })
  }
))

在此处输入图像描述

> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=Dutch_Belgium.1252  LC_CTYPE=Dutch_Belgium.1252   
[3] LC_MONETARY=Dutch_Belgium.1252 LC_NUMERIC=C                  
[5] LC_TIME=Dutch_Belgium.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] shiny_0.8.0.99 rjson_0.2.13   rCharts_0.4.2 

loaded via a namespace (and not attached):
 [1] bitops_1.0-6    caTools_1.16    digest_0.6.4    grid_3.0.2     
 [5] httpuv_1.2.1    lattice_0.20-23 plyr_1.8        Rcpp_0.10.6    
 [9] RCurl_1.95-4.1  RJSONIO_1.0-3   tools_3.0.2     whisker_0.3-2  
[13] xtable_1.7-1    yaml_2.1.10    
4

1 回答 1

1

该问题与多个文件中的字符编码有关。这就是我在 Windows 7 机器上解决问题的方法。

  1. 鼠标悬停提示问题

箭头字符用于构建源和目标之间的“链接”。它出现在这些文件中:

example_build_network_sankey.html
layouts\chart.html
layouts\chart_static_title.html
layouts\chart.html

将箭头替换为 ASCii 字符 -> 使代码如下所示:

.text(function (d) { return d.source.name + " -> " + d.target.name + "\n" + format(d.value); });
  1. 图表下方的字符

 见于:

\libraries\highlighters\prettify\css\sunburst.css
\layouts\chart.html
\libraries\widgets\d3_sankey\layouts\chart.html

我使用 UltraEdit 中的文件搜索和替换工具将这个特殊字符替换为空格。这个很棘手,因为我在 UE 编辑器中看不到这个角色。如果我突出显示空白区域,它会显示为反引号。该字符也在 jquery-1.8.2.min.js 中找到。

于 2016-09-02T14:49:54.123 回答