<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/wvplots-now-at-version-1-0-0-on-cran/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.win-vector.com/blog/2018/05/wvplots-now-at-version-1-0-0-on-cran/"> R – Win-Vector Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="http://www.win-vector.com/site/staff/nina-zumel/" target="_blank">Nina Zumel</a> and <a rel="nofollow" href="http://www.win-vector.com/site/staff/john-mount/" target="_blank">I</a> have been working on packaging our <a rel="nofollow" href="http://www.win-vector.com/blog/2011/12/my-favorite-graphs/" target="_blank">favorite graphing techniques</a> in a more reusable way that emphasizes the analysis task at hand over the steps needed to produce a good visualization. We are excited to announce the <a rel="nofollow" href="https://cran.r-project.org/package=WVPlots" target="_blank">WVPlots is now at version 1.0.0 on CRAN</a>!</p>
<p><span></span></p>
<p>The idea is: we sacrifice some of the flexibility and composability inherent to <a rel="nofollow" href="http://ggplot2.org/" target="_blank">ggplot2</a> in <a rel="nofollow" href="https://cran.r-project.org/" target="_blank">R</a> for a menu of prescribed presentation solutions. This is a package to produce plots while you are in the middle of another task.</p>
<p>For example the plot below showing both an observed discrete empirical distribution (as stems) and a matching theoretical distribution (as bars) is a built in &quot;one liner.&quot;</p>
<div>
<pre><code><span>set.seed</span>(<span>52523</span>)
d &lt;-<span> </span><span>data.frame</span>(<span>wt=</span><span>100</span>*<span>rnorm</span>(<span>100</span>))
WVPlots::<span>PlotDistCountNormal</span>(d,<span>'wt'</span>,<span>'example'</span>)</code></pre>
</div>
<div>
<img border="0" src="https://i2.wp.com/www.win-vector.com/blog/wp-content/uploads/2018/05/unnamed-chunk-2-1.png?resize=288%2C288" alt="Unnamed chunk 2 1" width="288" title="unnamed-chunk-2-1.png" data-recalc-dims="1" height="288">
</div>
<p>The graph above is actually the product of a number of presentation decisions:</p>
<ul>
<li>Using a discrete histogram approach to summarize data (instead of a kernel density approach) to create a presentation more familiar to business partners.</li>
<li>Using a Cleveland style dot with stem plot instead of wide bars to emphasize the stem heights represent total counts (and not the usual accidental misapprehension that bar <em>areas</em> represent totals).</li>
<li>Automatically fitting and rendering the matching (properly count-scaled) normal distribution as thin translucent bars for easy comparison (again to try and de-emphasize area).</li>
</ul>
<p>All of these decisions are triggered by choosing which plot to use from the WVPlots library. In this case we chose <code>WVPlots::PlotDistCountNormal</code>. For an audience of analysts we might choose an area/density based representation (by instead specifying <code>WVPlots::PlotDistDensityNormal</code>) which is shown below:</p>
<div>
<pre><code>WVPlots::<span>PlotDistDensityNormal</span>(d,<span>'wt'</span>,<span>'example'</span>)</code></pre>
</div>
<div>
<img border="0" src="https://i2.wp.com/www.win-vector.com/blog/wp-content/uploads/2018/05/unnamed-chunk-3-1.png?resize=288%2C288" alt="Unnamed chunk 3 1" width="288" title="unnamed-chunk-3-1.png" data-recalc-dims="1" height="288"></div>
<p>Switching the chosen plot simultaneously changes many of the details of the presentation. WVPlots is designed to make this change simple by insisting an a very simple unified calling convention. The plot calls all insist on roughly the following arguments:</p>
<ul>
<li>frame: data frame containing the data to be presented.</li>
<li>xvar: name of the x variable column in the data frame.</li>
<li>yvar: name of the y variable column in the data frame (not part of the shown density plots!).</li>
<li>title: text title for the plot.</li>
</ul>
<p>This rigid calling interface is easy to remember and makes switching between plot types very easy. We have also make <code>title</code> a required argument, as we feel all plots should be labeled.</p>
<p>What we are trying to do is separate the specification of exactly what plot we want from the details of how to produce it. We find this separation of concerns and encapsulation of implementation allows us to routinely use rich annotated graphics. Below are a few more examples:</p>
<div>
<pre><code><span>set.seed</span>(<span>34903490</span>)
x =<span> </span><span>rnorm</span>(<span>50</span>)
y =<span> </span><span>0.5</span>*x^<span>2</span> +<span> </span><span>2</span>*x +<span> </span><span>rnorm</span>(<span>length</span>(x))
frm =<span> </span><span>data.frame</span>(<span>x=</span>x,<span>y=</span>y,<span>yC=</span>y>=<span>as.numeric</span>(<span>quantile</span>(y,<span>probs=</span><span>0.8</span>)))
frm$absY &lt;-<span> </span><span>abs</span>(frm$y)
frm$posY =<span> </span>frm$y ><span> </span><span>0</span>
WVPlots::<span>ScatterHist</span>(frm, <span>&quot;x&quot;</span>, <span>&quot;y&quot;</span>, <span>smoothmethod=</span><span>&quot;lm&quot;</span>, 
                     <span>title=</span><span>&quot;Example Linear Fit&quot;</span>)</code></pre>
</div>
<div>
<img border="0" src="https://i1.wp.com/www.win-vector.com/blog/wp-content/uploads/2018/05/unnamed-chunk-4-1.png?resize=288%2C288" alt="Unnamed chunk 4 1" width="288" title="unnamed-chunk-4-1.png" data-recalc-dims="1" height="288">
</div>
<div>
<pre><code><span>set.seed</span>(<span>34903490</span>)
y =<span> </span><span>abs</span>(<span>rnorm</span>(<span>20</span>)) +<span> </span><span>0.1</span>
x =<span> </span><span>abs</span>(y +<span> </span><span>0.5</span>*<span>rnorm</span>(<span>20</span>))

frm =<span> </span><span>data.frame</span>(<span>model=</span>x, <span>value=</span>y)

frm$costs=<span>1</span>
frm$costs[<span>1</span>]=<span>5</span>
frm$rate =<span> </span><span>with</span>(frm, value/costs)

frm$isValuable =<span> </span>(frm$value >=<span> </span><span>as.numeric</span>(<span>quantile</span>(frm$value, <span>probs=</span><span>0.8</span>)))
gainx =<span> </span><span>0.10</span>  <span># get the top 10% most valuable points as sorted by the model</span>

<span># make a function to calculate the label for the annotated point</span>
labelfun =<span> </span>function(gx, gy) {
  pctx =<span> </span>gx*<span>100</span>
  pcty =<span> </span>gy*<span>100</span>
  
  <span>paste</span>(<span>&quot;The top &quot;</span>, pctx, <span>&quot;% most valuable points by the model</span><span>\n</span><span>&quot;</span>,
        <span>&quot;are &quot;</span>, pcty, <span>&quot;% of total actual value&quot;</span>, <span>sep=</span><span>''</span>)
}

WVPlots::<span>GainCurvePlotWithNotation</span>(frm, <span>&quot;model&quot;</span>, <span>&quot;value&quot;</span>, 
                                   <span>title=</span><span>&quot;Example Gain Curve with annotation&quot;</span>, 
                          <span>gainx=</span>gainx,<span>labelfun=</span>labelfun) </code></pre>
</div>
<div>
<img border="0" src="https://i2.wp.com/www.win-vector.com/blog/wp-content/uploads/2018/05/unnamed-chunk-5-1.png?resize=288%2C288" alt="Unnamed chunk 5 1" width="288" title="unnamed-chunk-5-1.png" data-recalc-dims="1" height="288">
</div>
<div>
<pre><code><span>set.seed</span>(<span>52523</span>)
d =<span> </span><span>data.frame</span>(<span>meas=</span><span>rnorm</span>(<span>100</span>))
threshold =<span> </span><span>1.5</span>
WVPlots::<span>ShadedDensity</span>(d, <span>&quot;meas&quot;</span>, threshold, <span>tail=</span><span>&quot;right&quot;</span>, 
                       <span>title=</span><span>&quot;Example shaded density plot, right tail&quot;</span>)</code></pre>
</div>
<div>
<img border="0" src="https://i1.wp.com/www.win-vector.com/blog/wp-content/uploads/2018/05/unnamed-chunk-6-1.png?resize=288%2C288" alt="Unnamed chunk 6 1" width="288" title="unnamed-chunk-6-1.png" data-recalc-dims="1" height="288">
</div>
<div>
<pre><code><span>set.seed</span>(<span>34903490</span>)
frm =<span> </span><span>data.frame</span>(<span>x=</span><span>rnorm</span>(<span>50</span>),<span>y=</span><span>rnorm</span>(<span>50</span>))
frm$z &lt;-<span> </span>frm$x+frm$y
WVPlots::<span>ScatterHistN</span>(frm, <span>&quot;x&quot;</span>, <span>&quot;y&quot;</span>, <span>&quot;z&quot;</span>, <span>title=</span><span>&quot;Example Joint Distribution&quot;</span>)</code></pre>
</div>
<div>
<img border="0" src="https://i0.wp.com/www.win-vector.com/blog/wp-content/uploads/2018/05/unnamed-chunk-7-1.png?resize=288%2C288" alt="Unnamed chunk 7 1" width="288" title="unnamed-chunk-7-1.png" data-recalc-dims="1" height="288">
</div>
<div>
<pre><code><span>set.seed</span>(<span>34903490</span>)
x =<span> </span><span>rnorm</span>(<span>50</span>)
y =<span> </span><span>0.5</span>*x^<span>2</span> +<span> </span><span>2</span>*x +<span> </span><span>rnorm</span>(<span>length</span>(x))
frm =<span> </span><span>data.frame</span>(<span>x=</span>x,<span>yC=</span>y>=<span>as.numeric</span>(<span>quantile</span>(y,<span>probs=</span><span>0.8</span>)))
WVPlots::<span>ROCPlot</span>(frm, <span>&quot;x&quot;</span>, <span>&quot;yC&quot;</span>, <span>TRUE</span>, <span>title=</span><span>&quot;Example ROC plot&quot;</span>)</code></pre>
</div>
<div>
<img border="0" src="https://i1.wp.com/www.win-vector.com/blog/wp-content/uploads/2018/05/unnamed-chunk-8-1.png?resize=288%2C288" alt="Unnamed chunk 8 1" width="288" title="unnamed-chunk-8-1.png" data-recalc-dims="1" height="288">
</div>
<p>We know this collection doesn’t rise to the standard of a complete &quot;grammar of graphics.&quot; But it can become (through accumulation) a re-usable repository of a number of specific graphing tasks done well. It is also a chance to eventually document presentation design decisions (though we haven’t gotten far on that yet). The complete set of graphs is shown in the <code>WVPlots_example</code> vignette.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.win-vector.com/blog/2018/05/wvplots-now-at-version-1-0-0-on-cran/"> R – Win-Vector Blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/wM9Lg-sHv84" height="1"></a></div>