<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/r-for-beginners-some-simple-code-to-produce-informative-graphs-part-one/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://dmwiig.net/2017/01/11/r-for-beginners-some-simple-code-to-produce-informative-graphs-part-one/"> r – R Statistics and Programming</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>A Tutorial by D. M. Wiig</p>
<p>The R programming language has a multitude of packages that can be used to display various types of graph. For a new user looking to display data in a meaningful way graphing functions can look very intimidating. When using a statistics package such as SPSS, Stata, Minitab or even some of the R Gui’s such R Commander sophisticated graphs can be produced but with a limited range of options. When using the R command line to produce graphics output the user has virtually 100 percent control over every aspect of the graphics output.</p>
<p>For new R users there are some basic commands that can be used that are easy to understand and offer a large degree of control over customisation of the graphical output. In part one of this tutorial I will discuss some R scripts that can be used to show typical output from a basic correlation and regression analysis.</p>
<p>For the first example I will use one of the datasets from the R MASS dataset package. The dataset is ‘UScrime´ which contains data on certain factors and their relationship to violent crime. In the first example I will produce a simple scatter plot using the variables ‘GDP’ as the independent variable and ´crimerate´ the dependent variable which is represented by the letter ‘y’ in the dataset.</p>
<p>Before starting on this project install and load the R package ‘MASS.’ Other needed packages are loaded when R is started. The scatter plot is produced using the following code:</p>
<p><strong>####################################################</strong><br>
<strong>### make sure that the MASS package is installed</strong><br>
<strong>###################################################</strong><br>
<strong>library(MASS)   </strong>## load MASS<br>
<strong>attach(UScrime)  </strong> ## use the UScrime dataset<br>
## plot the two dimensional scatterplot and add appropriate #labels<br>
<strong>#</strong><br>
<strong>plot(GDP, y,</strong><br>
<strong> main=”Basic Scatterplot of Crime Rate vs. GDP”, </strong><br>
<strong> xlab=”GDP”,</strong><br>
<strong> ylab=”Crime Rate”)</strong><br>
<strong>#</strong><br>
<strong>####################################################</strong></p>
<p>The above code produces a two-dimensional plot of GDP vs. Crimerate. A regression line can be added to the graph produced by including the following code:</p>
<p><strong>####################################################</strong><br>
## add a regression line to the scatter plot by using simple bivariate #linear model<br>
## lm generates the coefficients for the regression<strong> model.extract</strong><br>
## col sets color; lwd sets line width; lty sets line type<br>
#<br>
<strong>abline(lm(y ~ GDP), col=”red”, lwd=2, lty=1) </strong><br>
<strong>#</strong><br>
<strong>####################################################</strong></p>
<p>As is often the case in behavioral research we want to evaluate models that involve more than two variables. For multivariate models scatter plots can be generated using a 3 dimensional version of the R <em>plot()</em> function. For the above model we can add a third variable ‘Ineq’ from the dataset which is a measure the distribution of wealth in the population. Since we are now working with a multivariate linear model of the form ‘y = b1(x1) + b2(x2) + a’ we can use the R function <em>scatterplot3d() </em>to generate a 3 dimensional representation of the variables.</p>
<p>Once again we use the MASS package and the dataset  ‘UScrime’ for the graph data. The code is seen below:</p>
<p>####################################################<br>
## create a 3d graph using the variables y, GDP, and Ineq<br>
####################################################<br>
#<br>
<strong>library(scatterplot3d)   </strong>##load scatterplot3d function<br>
<strong>require(MASS)</strong><br>
<strong>attach(UScrime)   </strong>## use data from UScrime dataset<br>
<strong>scatterplot3d(y,GDP, Ineq, </strong><br>
<strong> main=”Basic 3D Scatterplot”) </strong>## graph 3 variables, y<br>
<strong>#</strong><br>
<strong>###################################################</strong></p>
<p>The following graph is produced:</p>
<p><a target="_blank" rel="nofollow" href="https://i1.wp.com/dmwiig.files.wordpress.com/2017/01/screenshot-graphics-device-number-2-active-e28093-27rkward27.png?ssl=1"><img sizes="(max-width: 300px) 100vw, 300px" data-image-description="" data-orig-file="https://dmwiig.files.wordpress.com/2017/01/screenshot-graphics-device-number-2-active-e28093-27rkward27.png" data-orig-size="771,668" data-image-title="3d Scatter Plot" class="wp-image-276" data-comments-opened="1" data-recalc-dims="1" data-attachment-id="276" data-permalink="https://dmwiig.net/2017/01/11/r-for-beginners-some-simple-code-to-produce-informative-graphs-part-one/screenshot-graphics-device-number-2-active-%27rkward%27/" alt="screenshot-graphics-device-number-2-active-%27rkward%27" data-medium-file="https://i1.wp.com/dmwiig.files.wordpress.com/2017/01/screenshot-graphics-device-number-2-active-e28093-27rkward27.png?resize=300%2C260&amp;ssl=1" data-large-file="https://dmwiig.files.wordpress.com/2017/01/screenshot-graphics-device-number-2-active-e28093-27rkward27.png?w=474" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://i1.wp.com/dmwiig.files.wordpress.com/2017/01/screenshot-graphics-device-number-2-active-e28093-27rkward27.png?resize=300%2C260&ssl=1"></a></p>
<p>The above code will generate a basic 3d plot using default values. We can add straight lines from the plane of the graph to each of the data points by setting the graph type option as ‘type=”h”, as seen in the code below:</p>
<h6><strong>##############################################</strong></h6>
<p><strong>require(MASS)</strong><br>
<strong>library(scatterplot3d)</strong><br>
<strong>attach(UScrime)</strong><br>
<strong>model &lt;- scatterplot3d(GDP, Ineq, y,</strong><br>
<strong> type=”h”, </strong>## add vertical lines from plane with this option<br>
<strong> main=”3D Scatterplot with Vertical Lines”)</strong><br>
<strong>####################################################</strong></p>
<p>This results in the graph:</p>
<p><a target="_blank" rel="nofollow" href="https://i2.wp.com/dmwiig.files.wordpress.com/2017/01/screenshot-graphics-device-number-2-active-e28093-27rkward27-1.png?ssl=1"><img sizes="(max-width: 300px) 100vw, 300px" data-image-description="" data-orig-file="https://dmwiig.files.wordpress.com/2017/01/screenshot-graphics-device-number-2-active-e28093-27rkward27-1.png" data-orig-size="771,668" data-image-title="screenshot-graphics-device-number-2-active-%27rkward%27-1" data-comments-opened="1" class="wp-image-277" data-recalc-dims="1" data-attachment-id="277" data-permalink="https://dmwiig.net/2017/01/11/r-for-beginners-some-simple-code-to-produce-informative-graphs-part-one/screenshot-graphics-device-number-2-active-%27rkward%27-1/" alt="screenshot-graphics-device-number-2-active-%27rkward%27-1" data-medium-file="https://i2.wp.com/dmwiig.files.wordpress.com/2017/01/screenshot-graphics-device-number-2-active-e28093-27rkward27-1.png?resize=300%2C260&amp;ssl=1" data-large-file="https://dmwiig.files.wordpress.com/2017/01/screenshot-graphics-device-number-2-active-e28093-27rkward27-1.png?w=474" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://i2.wp.com/dmwiig.files.wordpress.com/2017/01/screenshot-graphics-device-number-2-active-e28093-27rkward27-1.png?resize=300%2C260&ssl=1"></a></p>
<p>There are numerous options that can be used to go beyond the basic 3d plot. Refer to CRAN documentation to see these. A final addition to the 3d plot as discussed here is the code needed to generate the regression plane of our linear regression model using the y (crimerate), GDP, and Ineq variables. This is accomplished using the <em>plane3d() </em>option that will draw a plane through the data points of the existing plot. The code to do this is shown below:</p>
<h6><strong>##############################################</strong><br>
<strong>require(MASS)</strong><br>
<strong>library(scatterplot3d)</strong><br>
<strong>attach(UScrime)</strong><br>
<strong>model &lt;- scatterplot3d(GDP, Ineq, y,</strong><br>
<strong> type=”h”,  </strong> ## add vertical line from plane to data points with this #optio<strong>n</strong><br>
<strong> main=”3D Scatterplot with Vertical Lines”)</strong><br>
<strong>## now calculate and add the linear regression data</strong><br>
<strong>model1 &lt;- lm(y ~ GDP + Ineq)   #</strong><br>
<strong>model$plane3d(model1)   </strong>## link the 3d scatterplot in ‘model’ to the ‘plane3d’ option with ‘model1’ regression information<br>
#<br>
####################################################</h6>
<p>The resulting graph is:</p>
<p><a target="_blank" rel="nofollow" href="https://i2.wp.com/dmwiig.files.wordpress.com/2017/01/screenshot-graphics-device-number-2-active-e28093-27rkward27-2.png?ssl=1"><img sizes="(max-width: 300px) 100vw, 300px" data-image-description="" data-orig-file="https://dmwiig.files.wordpress.com/2017/01/screenshot-graphics-device-number-2-active-e28093-27rkward27-2.png" data-orig-size="771,668" data-image-title="screenshot-graphics-device-number-2-active-%27rkward%27-2" data-comments-opened="1" class="wp-image-278" data-recalc-dims="1" data-attachment-id="278" data-permalink="https://dmwiig.net/2017/01/11/r-for-beginners-some-simple-code-to-produce-informative-graphs-part-one/screenshot-graphics-device-number-2-active-%27rkward%27-2/" alt="screenshot-graphics-device-number-2-active-%27rkward%27-2" data-medium-file="https://i2.wp.com/dmwiig.files.wordpress.com/2017/01/screenshot-graphics-device-number-2-active-e28093-27rkward27-2.png?resize=300%2C260&amp;ssl=1" data-large-file="https://dmwiig.files.wordpress.com/2017/01/screenshot-graphics-device-number-2-active-e28093-27rkward27-2.png?w=474" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://i2.wp.com/dmwiig.files.wordpress.com/2017/01/screenshot-graphics-device-number-2-active-e28093-27rkward27-2.png?resize=300%2C260&ssl=1"></a></p>
<p>To draw a regression plane through the data points only change the ‘type’ option to ‘type=”p” to show the data points without vertical lines to the plane. There are also many other options that can be used. See the CRAN documentation to review them.</p>
<p>I have hopefully shown that relatively simple R code can be used to generate some informative and useful graphs. Once you start to become aware of how to use the multitude of options for these functions you can have virtually total control of the visual presentation of data. I will discuss some additional simple graphs in the next tutorial that I post.</p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dmwiig.wordpress.com/273/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/dmwiig.wordpress.com/273/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></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="https://dmwiig.net/2017/01/11/r-for-beginners-some-simple-code-to-produce-informative-graphs-part-one/"> r – R Statistics and Programming</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/6K57aWu5wMM"></a></div>