简体   繁体   English

在R中制作Sankey图以进行打印

[英]Making a Sankey diagram in R for printing

I am trying to make a Sankey diagram using R for a printed report. 我正在尝试使用R作为打印报告制作一个Sankey图。 I have currently used 2 methods to do this but neither is achieving quite what I wanted. 我目前使用了两种方法来做到这一点,但都没有达到我想要的效果。

The first uses the Riverplot function and produces this: 第一个使用Riverplot函数并生成: 在此输入图像描述

My problems with this plot is that I can't work out how to control the whitespace around the plot (it appears to be fixed and does not adjust to different sized datasets) or the positioning of the annotated text. 我对这个图的问题是我无法弄清楚如何控制图周围的空白(它似乎是固定的,不适应不同大小的数据集)或注释文本的位置。 I want to repeat this plot for a number of similar but differently sized datasets but the text placing is very variable in its positioning. 我想为许多类似但不同大小的数据集重复这个图,但文本放置的位置变化很大。 This is the code I am currently using to add the text annotations: 这是我目前用于添加文本注释的代码:

riverplot(a, srt = 0)
y_lim<-par("yaxp") #gets y axis limits to spicify text placements
#  text(1.5,y_lim[1], Name,font=2) #labels plot with Name (LA)
text(1,y_lim[1],"2004")
text(2,y_lim[1],"2010")

The second uses the rCharts and produces this: 第二个使用rCharts并产生: 在此输入图像描述

My problems with this are (a) I would like to fix the order of the categories on each side (as in the first image) and (b) I would like to annotate the diagram as in the first image. 我的问题是(a)我想修复每一侧的类别顺序(如在第一张图像中)和(b)我想在第一张图像中注释图表。 My questions are: 我的问题是:

  1. How I can fine tune the formatting of the Riverplot figure (first image), or, 我如何微调Riverplot图形(第一张图片)的格式,或者,
  2. How do I add annotations to the rCharts Sankey plot (I'm guessing this could be through the script section but I am new to rCharts, d3 and javascript), and, 如何在rCharts Sankey图中添加注释(我猜这可能是通过脚本部分,但我是rCharts,d3和javascript的新手),以及
  3. Is it possible to fix the order of the categories int the rCharts sankey plot. 是否可以在rCharts sankey图中修复类别的顺序。

After a lot of tinkering and pinching code from a bunch of places, I have managed to produce what I wanted (solving point 2 of my original question). 经过大量的修补和捏捏代码之后,我已经设法生产了我想要的东西(解决了我原来问题的第2点)。 在此输入图像描述
The code could be refined in a number of places and could probably be done more elegantly but I have included it below in case it helps anyone else. 代码可以在很多地方进行优化,并且可能更优雅地完成,但我已将其包含在下面,以防它帮助其他任何人。

sankeyPlot$setTemplate(
  afterScript = "
<script>

var cscale = d3.scale.ordinal()
.domain(['N','E', 'G' ,'I','T','N ','E ', 'G ' ,'I ','T '])
.range(['#d3d3d3', '#32ca32', '#1f78b4', '#e31a1c','#ecd736','#d3d3d3',    '#32ca32', '#1f78b4', '#e31a1c','#ecd736'])



d3.selectAll('#{{ chartId }} svg path.link')
.style('stroke', function(d){
return cscale(d.source.name);
//returns grey links
})

d3.selectAll('#{{ chartId }} svg .node rect')
.style('fill', function(d){
return cscale(d.name)
})
.style('stroke', 'none')

var text_box = d3.selectAll('#{{ chartId }}').append('svg')
    .attr('width', 500)
    .attr('height', 100)

var TextData = [
    { 'cx': 0, 'cy': 20 ,'label': 'Type A', 'pos': 'left'},
    { 'cx': 250, 'cy': 20,'label': 'Label','pos': 'middle'},
    { 'cx': 500, 'cy': 20, 'label': 'Type B','pos': 'end'}];

var text = text_box.selectAll('text')
                    .data(TextData)
                    .enter()
                    .append('text');

//Add the text attributes
var textLabels = text
             .attr('x', function(d) { return d.cx; })
             .attr('y', function(d) { return d.cy; })
             .text( function (d) { return d.label ; })
              .attr('text-anchor', function(d) { return d.pos ;})
             .attr('font-family', 'sans-serif')
             .attr('font-size', '14px')
             .attr('fill', 'black');

</script>
")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM