<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/the-power-of-ggplot2-in-arcgis-the-plotting-toolbox/"></div></div><div><a data-url="http://www.r-bloggers.com/the-power-of-ggplot2-in-arcgis-the-plotting-toolbox/" data-counturl="http://www.r-bloggers.com/the-power-of-ggplot2-in-arcgis-the-plotting-toolbox/" data-count="horizontal" data-text="The Power of ggplot2 in ArcGIS – The Plotting Toolbox" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-video-tutorial.blogspot.com/2016/07/the-power-of-ggplot2-in-arcgis-plotting.html"> R tutorial for Spatial Statistics</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In this post I present my third experiment with <a target="_blank" href="https://r-arcgis.github.io/">R-Bridge</a>. The plotting toolbox is a plug-in for ArcGIS 10.3.x that allows the creation of beautiful and informative plot, with ggplot2, directly from the ESRI ArcGIS console.<br>As always I not only provide the toolbox but also a dataset to try it out. Let’s start from here…</p>
<p><span>Data</span><br>For testing the plotting tool, I downloaded some air pollution data from EPA (US Environmental Protection Agency), which provides open access to its database. I created a custom function to download data from EPA that you can find in <a target="_blank" href="http://r-video-tutorial.blogspot.ch/2015/05/introductory-time-series-analysis-of-us.html">this post</a>.<br>Since I wanted to provide a relatively small dataset, I extracted values from only four states: California, New York, Iowa and Ohio. For each of these, I included time series for Temperature, CO, NO2, SO2 and Barometric Pressure. Finally, the coordinates of the points are the centroid for each of these four states. The image below depicts the location and the first lines of the attribute table. This dataset is provided in shapefile and CSV, both can be used with the plotting toolbox.</p>
<div><a target="_blank" href="http://i2.wp.com/2.bp.blogspot.com/-DoJYpzZ_4g8/V4M8O6XaiII/AAAAAAAAR-o/5CKK4oDgMj80IWtEigh-eMlUJqM5ekRtQCLcB/s1600/Fig1.jpg?ssl=1"><img src="http://i0.wp.com/2.bp.blogspot.com/-DoJYpzZ_4g8/V4M8O6XaiII/AAAAAAAAR-o/5CKK4oDgMj80IWtEigh-eMlUJqM5ekRtQCLcB/s640/Fig1.jpg?resize=456%2C211&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p><span>Toolbox</span><br>Now that we have seen the sample dataset, we can take a look at the toolbox. I included 5 packages to help summarize and visualize spatial data directly from the ArcGIS console.</p>
<div><a target="_blank" href="http://i2.wp.com/1.bp.blogspot.com/-ogvdk-7FkNE/V4M91pcKYVI/AAAAAAAAR-0/4-m-BOdmI0YxTdtq9NF07usA4laRWEqYACLcB/s1600/Fig2.jpg?ssl=1"><img src="http://i1.wp.com/1.bp.blogspot.com/-ogvdk-7FkNE/V4M91pcKYVI/AAAAAAAAR-0/4-m-BOdmI0YxTdtq9NF07usA4laRWEqYACLcB/s320/Fig2.jpg?resize=320%2C189&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p>I first included a package for summarizing our variables, this creates a table with some descriptive statistics. Then I included all the major plotting types I presented in my book “<a target="_blank" href="http://r-video-tutorial.blogspot.ch/2016/04/learning-r-for-data-visualization-video.html">Learning R for Data Visualization [VIDEO]</a>” edited by <a target="_blank" href="https://www.packtpub.com/big-data-and-business-intelligence/learning-r-data-visualization-video">Packt Publishing</a>, with some modifications to the scripts for adapting them to the <a target="_blank" href="https://r-arcgis.github.io/">R-Bridge</a> Technology from <a target="_blank" href="https://blogs.esri.com/esri/esri-insider/2015/07/20/building-a-bridge-to-the-r-community/">ESRI</a>. For more information, practical and theoretical, about each of these plots please refer to the book.</p>
<p><b>The tool can be downloaded from my GitHub page at:</b><br><a target="_blank" href="https://github.com/fveronesi/PlottingToolbox">https://github.com/fveronesi/PlottingToolbox</a></p>
<p><span>Summary </span><br>This is the first package I would like to describe simply because a data analysis should always start with a look at our variables with some descriptive statistics. As for all the tools presented here its use is simple and straightforward with the GUI presented below:</p>
<div><a target="_blank" href="http://i1.wp.com/3.bp.blogspot.com/-le3lFPsmmAQ/V4NACm82RJI/AAAAAAAAR_A/eynxteK6LoY5lU7lcphp5qLQwRWkBAG3gCLcB/s1600/Fig3.jpg?ssl=1"><img src="http://i1.wp.com/3.bp.blogspot.com/-le3lFPsmmAQ/V4NACm82RJI/AAAAAAAAR_A/eynxteK6LoY5lU7lcphp5qLQwRWkBAG3gCLcB/s640/Fig3.jpg?resize=456%2C355&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p>Here the user has to point to the dataset s/he wants to analyze in “Input Table”. This can be a shapefile, either loaded already in ArcGIS or not, but it can also be a table, for example a CSV. That is the reason why I included a CSV version of the EPA dataset as well.<br>At this point the area in “Variable” will fill up with the column names of the input file, from here the user can select the variable s/he is interested in summarizing. The final step is the selection of the “Output Folder”. <b>Important</b>: users need to first create the folder and then select it. This is because this parameter is set as input, so the folder needs to exist. I decided to do it this way because otherwise for each new plot a new folder would have need to be created. This way all the summaries and plots can go into the same directory.<br>Let’s take a look at the results:</p>
<div><a target="_blank" href="http://i1.wp.com/3.bp.blogspot.com/-96EZ8T8uc54/V4NERGqD9fI/AAAAAAAAR_M/8hbZey9Qzz0SJfjjPNc-x2-xKbHWuyOXQCLcB/s1600/Fig4.jpg?ssl=1"><img src="http://i0.wp.com/3.bp.blogspot.com/-96EZ8T8uc54/V4NERGqD9fI/AAAAAAAAR_M/8hbZey9Qzz0SJfjjPNc-x2-xKbHWuyOXQCLcB/s640/Fig4.jpg?resize=456%2C251&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p>The Summary package presents two tables, with all the variables we wanted to summarize, arranged one above the other. This is the output users will see on screen once the toolbox has competed its run. Then in the output folder, the R script will save this exact table in PDF format, plus a CSV with the raw data.</p>
<p><span>Histogram</span><br>As the name suggest this tool provides access to the histogram plot in ggplot2. ArcGIS provides a couple of ways to represent data in histograms. The first way is by clicking with the right mouse button on one of the column in the attribute table; a drop-down menu will appear and from there users can click on “Statistics” to access some descriptive statistics and a histogram. Another way is through the “Geostatistical Analyst”, which is an add-on for which users need an additional license. This has an “Explore Data” package from which it is possible to create histograms. The problem with both these methods is that the results are not, in my opinion at least, suitable for any publication. You can maybe share them with your colleagues, but I would not suggest using them for any article. This implies that ArcGIS users need to open another software, maybe Excel, to create the plots they require, and we all know how painful it is in Excel to produce any decent plot, and histogram are probably the most painful to produce.<br>This changes now!!</p>
<p>By combining the power of R and ggplot2 with ArcGIS we can provide users with an easy to use way to produce beautiful visualizations directly within the ArcGIS environment and have them saved in jpeg at 300 dpi. This is what this set of packages does.<br>Let’s now take a look at the interface for histograms:</p>
<div><a target="_blank" href="http://i1.wp.com/1.bp.blogspot.com/-78lHgtsRZFg/V4NWTqIxwOI/AAAAAAAAR_c/1sK-bbsy14ozGXWEKH-R-ozTLoX7JXKnQCLcB/s1600/Fig5.jpg?ssl=1"><img src="http://i1.wp.com/1.bp.blogspot.com/-78lHgtsRZFg/V4NWTqIxwOI/AAAAAAAAR_c/1sK-bbsy14ozGXWEKH-R-ozTLoX7JXKnQCLcB/s640/Fig5.jpg?resize=456%2C356&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p>As for Summary we first need to insert the input dataset, and then select the variable/s we want to plot. If two or more variables are selected, several plots will be created and saved in jpeg.<br>I also added some optional values to further customize the plots. The first is the <a target="_blank" href="http://www.cookbook-r.com/Graphs/Facets_(ggplot2)/">faceting</a> variable, which is a categorical variable. If this is selected the plot will have a histogram for each category; for example, in the sample dataset I have the categorical variable “state”, with the name of the four states I included. If I select this for faceting the result will be the figure below:</p>
<div><a target="_blank" href="http://i0.wp.com/1.bp.blogspot.com/-7KXMHG7qarI/V4NYRYMTPnI/AAAAAAAAR_o/d59KGNS4cmkv2QGBpQt0uCFNICtK_xmqgCLcB/s1600/Fig6.jpg?ssl=1"><img src="http://i1.wp.com/1.bp.blogspot.com/-7KXMHG7qarI/V4NYRYMTPnI/AAAAAAAAR_o/d59KGNS4cmkv2QGBpQt0uCFNICtK_xmqgCLcB/s640/Fig6.jpg?resize=456%2C252&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p>Another option available here is the binwidth. The package ggplot2 usually sets this number as the range of the variable divided by 30. However, this can be customized by the user and this is what you can do with this option. Finally users need to specify an output folder where R will save a jpeg of the plots shown on screen.</p>
<p><span>Box-Plot</span><br>This is another great way to compare variables’ distributions and as far as I know it cannot be done directly from ArcGIS. Let’s take a look at this package:</p>
<div><a target="_blank" href="http://i2.wp.com/4.bp.blogspot.com/-UYMeMCvCqjk/V4NgU4FHo9I/AAAAAAAAR_4/GDbzl-iYwo07UNmJk9N8N6_joQGxSy8qQCLcB/s1600/Fig7.jpg?ssl=1"><img src="http://i2.wp.com/4.bp.blogspot.com/-UYMeMCvCqjk/V4NgU4FHo9I/AAAAAAAAR_4/GDbzl-iYwo07UNmJk9N8N6_joQGxSy8qQCLcB/s640/Fig7.jpg?resize=456%2C355&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p>This is again very easy to use. Users just need to set the input file, then the variable of interest and the categorical variable for the grouping. Here I am again using the states, so that I compare the distribution of NO2 across the four US states in my dataset.<br>The results is ordered by median values and it is shown below:</p>
<div><a target="_blank" href="http://i0.wp.com/3.bp.blogspot.com/-ZZ1PHzVTlN8/V4Ngx-T2egI/AAAAAAAAR_8/ermG8YlyBPoRDAw-F4GheIS9rl19wyAAgCLcB/s1600/Fig8.jpg?ssl=1"><img src="http://i2.wp.com/3.bp.blogspot.com/-ZZ1PHzVTlN8/V4Ngx-T2egI/AAAAAAAAR_8/ermG8YlyBPoRDAw-F4GheIS9rl19wyAAgCLcB/s640/Fig8.jpg?resize=456%2C251&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p>As you can see I decided to plot the categories vertically, as to accomodate long names. This of course can be changed by tweaking the R script. As for each package, this plot is saved in the output folder.</p>
<p><span>Bar Chart</span><br>This tool is generally used to compare different values for several categories, but generally we have one value for each category. However, it may happen that the dataset contains multiple measurements for some categories, and I implemented a way to deal with that here. Below is presented the GUI to this package:</p>
<div><a target="_blank" href="http://i2.wp.com/3.bp.blogspot.com/-4vHDb4vJXPY/V4Nh5RT7z9I/AAAAAAAASAM/L3mEu9wl4rM1qynSdYAzvPg0S5t5pMLbACLcB/s1600/Fig9.jpg?ssl=1"><img src="http://i1.wp.com/3.bp.blogspot.com/-4vHDb4vJXPY/V4Nh5RT7z9I/AAAAAAAASAM/L3mEu9wl4rM1qynSdYAzvPg0S5t5pMLbACLcB/s640/Fig9.jpg?resize=456%2C359&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p>The inputs are basically the same as for box-plots, the only difference is in the option “Average Values”. If this is set, R will average the values of the variable for each unique category in the dataset. The results are again ordered, and are saved in jpeg in the output folder:</p>
<div><a target="_blank" href="http://i2.wp.com/4.bp.blogspot.com/-cdMW1GLX-Y8/V4NiSJDFo-I/AAAAAAAASAQ/8lN47ele6bMLEMLUHaDcjEp9TNZSl99bwCLcB/s1600/Fig10.jpg?ssl=1"><img src="http://i0.wp.com/4.bp.blogspot.com/-cdMW1GLX-Y8/V4NiSJDFo-I/AAAAAAAASAQ/8lN47ele6bMLEMLUHaDcjEp9TNZSl99bwCLcB/s640/Fig10.jpg?resize=456%2C251&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p><span>Scatterplot</span><br>This is another great way to visually analyze our data. The package ggplot2 allows the creation of highly customized plots and I tried to implement as much as I could in terms of customization in this toolbox. Let’s take a look:</p>
<div><a target="_blank" href="http://i1.wp.com/3.bp.blogspot.com/-HQpLV73OGz0/V4Nixsu7n2I/AAAAAAAASAY/4SUop0cjXe0cnZ5fZNl-rkzoTAidNPLtgCLcB/s1600/Fig11.jpg?ssl=1"><img src="http://i0.wp.com/3.bp.blogspot.com/-HQpLV73OGz0/V4Nixsu7n2I/AAAAAAAASAY/4SUop0cjXe0cnZ5fZNl-rkzoTAidNPLtgCLcB/s640/Fig11.jpg?resize=456%2C355&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p>After selecting the input dataset the user can select what to plot. S/he can choose to only plot two variables, one on the X axis and one on the Y axis, or further increase the amount of information presented in the plot by including a variable that changes the color of the points and one for their size. Moreover, there is also the possibility to include a regression line. Color, size and regression line are optional, but I wanted to include them to present the full range of customizations that this package allows.</p>
<div><a target="_blank" href="http://i2.wp.com/2.bp.blogspot.com/-54JvcT222Ks/V4NjwPAihII/AAAAAAAASAk/rJghpUstuGINaqZvxo6uyktAWQr47--yACLcB/s1600/Fgi12.jpg?ssl=1"><img src="http://i1.wp.com/2.bp.blogspot.com/-54JvcT222Ks/V4NjwPAihII/AAAAAAAASAk/rJghpUstuGINaqZvxo6uyktAWQr47--yACLcB/s640/Fgi12.jpg?resize=456%2C251&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p>Once again this plot is saved in the output folder.</p>
<p><span>Time Series</span><br>The final type of plots I included here is the time series, which is also the one with the highest number of inputs from the user side. In fact, many spatial datasets include a temporal component but often time this is not standardized. By that I mean that in some cases the time variable has only a date, and in some cases it includes a time; in other cases the format changes from dataset to dataset. For this reason it is difficult to create an R script that works with most datasets, therefore for time-series plots users need to do some pre-processing. For example, <i>if date and time are in separate columns, these need to be merged into one for this R script to work.</i><br>At this point the TimeSeries package can be started:</p>
<div><a target="_blank" href="http://i2.wp.com/3.bp.blogspot.com/-YCbPtwkfudY/V4NlB_Du7bI/AAAAAAAASA0/bN_8DE_ZbFwqLtWRzgcXm_C2v3kx-_JsACLcB/s1600/Fig12.jpg?ssl=1"><img src="http://i0.wp.com/3.bp.blogspot.com/-YCbPtwkfudY/V4NlB_Du7bI/AAAAAAAASA0/bN_8DE_ZbFwqLtWRzgcXm_C2v3kx-_JsACLcB/s640/Fig12.jpg?resize=456%2C358&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p>The first two columns are self explanatory. Then users need to select the column with the temporal information, and then input manually the format of this column.<br>In this case the format I have in the sample dataset is the following: 2014-01-01<br>Therefore I have the year with century, a minus sign, the month, another minus sign and the day. I need to use the symbols for each of these to allow R to recognize the temporal format of the file.<br>Common symbol are:<br>%Y – Year with century<br>%y – Year without century<br>%m – Month<br>%d – Day<br>%H – Hour as decimal number  (00-23)<br>%M – Minute as decimal number (00-59)<br>%S – Second as decimal number</p>
<p>More symbols can be found at this page: <a target="_blank" href="http://www.inside-r.org/r-doc/base/strptime">http://www.inside-r.org/r-doc/base/strptime</a></p>
<p>The remaining two inputs are optional, but if one is selected the other needs to be provided. For “Subsetting Column” I intend a column with categorical information. For example, in my dataset I can generate a time-series for each US state, therefore my subsetting column is state. In the option “Subset” users need to write manually the category they want to use to subset their data. Here I just want to have the time-series for California, so I write California. You need to be careful here to write exactly the name you see in the attribute table because <b>R is case sensitive</b>, thus if you write california with a lower case c R will be unable to produce the plot.<br>The results, again saved in jpeg automatically, is presented below:</p>
<div><a target="_blank" href="http://i0.wp.com/2.bp.blogspot.com/-tUEOnGcnIgI/V4Nnl2bLv1I/AAAAAAAASBA/tiTi7kpTPv41QZpKzPzYPE6RAxnzdDmYgCLcB/s1600/Fig13.jpg?ssl=1"><img src="http://i2.wp.com/2.bp.blogspot.com/-tUEOnGcnIgI/V4Nnl2bLv1I/AAAAAAAASBA/tiTi7kpTPv41QZpKzPzYPE6RAxnzdDmYgCLcB/s640/Fig13.jpg?resize=456%2C247&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/the-power-of-ggplot2-in-arcgis-the-plotting-toolbox/"></div></div><div><a data-url="http://www.r-bloggers.com/the-power-of-ggplot2-in-arcgis-the-plotting-toolbox/" data-counturl="http://www.r-bloggers.com/the-power-of-ggplot2-in-arcgis-the-plotting-toolbox/" data-count="vertical" data-text="The Power of ggplot2 in ArcGIS – The Plotting Toolbox" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://r-video-tutorial.blogspot.com/2016/07/the-power-of-ggplot2-in-arcgis-plotting.html"> R tutorial for Spatial Statistics</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/wD2AFe0E0kE"></a></div>