r - Add vertical line to line chart in rChart NVD3 (nPlot) -
I would like to add several vertical lines that show special events in the line chart created with the enplot any suggestions?
Library (Resep 2) Library (GG Plot 2) Library (RCHHT) ECM & LT; - Repeat 2 :: Melt (Economics [, C ('Date', 'UMPP', 'Pisa', 'ID =' Date '', ID = 'Date') P 7 & lt; Last goal: Add 'date' = '1967-11-30' - nPlot (value = date, group = 'variable', data = ECM, type = 'linewatfocus chart' , Vertical lines with labels on '2001-11-30', '1968-01-31', etc.
OK, so this question should be answered. I think you will see that this is generally beyond the skilled level of the average R user. After all, it would be good to write a custom chart to handle this behavior well and the normal R will be easy for the user and I would like to generalize to handle the dashed and vertical lines in R. Here's more.
Library (Resep 2) Library (GG Plot 2) Library (RCARTS) ECM & LT; - Rheps 2 :: Melt (Economics [, C ('Date', 'Umpp', 'Pisa']], ID = 'Date') P 7 & lt; - n Plot (value ~ date, group = 'variable', data = ECM, type = 'lineWithFocusChart') # Let's make it easy to add to date date P7 $ xAxis (tickFormat = "#! Function ) {Return d3.time.format ('% b% Y') (New Date (d * 86400000);}! # ") #https: # github.com / ramnathv / rCharts / blob / master / inst / libraries /nvd3/layouts/chart.html #grafRenders # (Script = SprintF ("script" = script type = 'text / javascript' & gt; $ (document) .ready (function () to add callback to p7 $ setTemplate {{ChartId}} ();}); function draw {{chartId}} () {Var opts = {{{opts}}} var data = {{{Data}}}; if ((Opts.type === 'piechart' || opts.type === 'sparklineplus' || opts .type === 'bulletchart')) {var data = D3.nest () .key (function (d) {// return opts.group === U Ndefined? 'Main': d [opts.group] // Instead of the main opts that optts opts.x opts .group === Undefined? Opts.y: D [opts.group];}). Access (Data);} If (opts.disabled! = Undefined) {data.map (function (d, i) {d. Disabled = opts.disabled [i]}}} nv.addGraph (function () {chart = nv.models [opts. type) (). If (opts.width) .height (opts.height) if (opts.type! = 'Bulletchart') {chart.x (function (d) {return d [opts.x]}). Y (function (d) {return d [opts.y]}}} {{{chart}}} {{{x xx}}} {{x2Axis}}} {{yAxis}}} d3.select (' # '+ Opts.id) .append (' svg ') .Data (data) .transition () .duration (500) .Col (chart); chart.dispatch.brush.on (' brushstart ', function () { DrawVerticalLines (opts)} chart.dispatch.brush.on ('brushhead', function () {window.setTimeout (function) {Draftical lines (opts)}, 250)}); Nv.utils.windowResize (chart updates); Return chart;},% s); }; % S & lt; / Script & gt; ", # Where you can type your vertical row / label function" function () {drawal lines (opts)} ", # add translate here if shiny" function with drawal lines (opt) {if (! ( Select 'D3.select' ('#' + Opts.id + '.nvd3 .nv-focus .nv-linesWrap'). ('Vertical-line') [0] [0])) {d3.select ('# '+ Opts .ID +' .nvd3 .nv-focus .nv-linesWrap ') .andand (' g ') .attr (' class', 'vertical-lines')} vertLines = d3.select (' # '+ Select opts.id ('vertical-line'). All ('vertical-line'). Data ([['date': new Date ('1967-11-30'), 'label': 'to expose some 1967]}, {' date ': new date (' 2001-11-30 '),' label ' : 'Something to highlight 2001'}}) var vertg = VertLines.enter () .append ('g') .attr ('class',' vertical-line ') vertG.append (' svg: line ') VertG.append (' text ') vertLines.exit (). Remove () VertLines.selectAll (' line ') .attr (' x1 ', function (d) {return chart.xAxis.scale () (d. Date / 60/60/24/1000)}) .attr ('x2', function (d) {return chart, xxis. Scale () (D.date/60/60/24/1000)}) .attr ('y1', chart.yAxis.Scale (.) Category () [0]) .attr ('y2', chart.exis Scale (). Category () [1]). Style ('stroke', 'red') vertLines.selectAll ('text') .text (function (d) {return d.label}) .attr ('dy', '1em') // x placement; Make changes for minor adjustments but mainly change the / d.date/60/60/24/1000 // y placement; To change 2 where you want vertical placement // rotate-90, feel free to change whatever you want .attr ('transform', function (d) {return 'translate (' + chart.xAxis.scale () ( D). Date / 60/60/24/1000) + ',' + Chart.exe. Scale () (2) + ') Rotate (-90)'}) // You can also style but you are here // would like to have an example of changing the font size. Style ('font size', '80% ')} ")) p7 Let me know your thoughts.
Comments
Post a Comment