<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-plot-function-the-options/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://blog.datazar.com/r-plot-function-the-options-711d7f7caef5?source=rss----e2c7e6e1c75--r_language"> R Language in Datazar Blog on Medium</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>R’s plot function is probably the most used visualization function in R. It’s simple, easy and gets the job done. It’s also highly customizable. Adding <em>unnecessary</em> styling and information on a visualization/plot is not really recommended because it can take away from what’s being portrayed, but there are times when you have just have to. Whether it’s for pure aesthetics, to convey multiple things in one plot, or any other reason, here are the options you can use in R’s base plot() function.</p>
<h4>The Data Points</h4>
<p>We’re going to be using the cars dataset that is built in R. To follow along with real code, here’s an interactive <a target="_blank" rel="nofollow" href="https://www.datazar.com/file/f7f286e86-4b5a-49c8-81be-d99639d5b96e">R Notebook</a>. Feel free to copy it and play around with the code as you read along.</p>
<p>So if we were to simply plot the dataset using just the data as the only parameter, it’d look like this:</p>
<pre>plot(dataset)</pre>
<figure><img alt="" src="https://i2.wp.com/cdn-images-1.medium.com/max/700/1*E3nzoGh9nPORL0NC17CuSQ.png?w=456&ssl=1" data-recalc-dims="1"><br>
<figcaption>Plot using no options.</figcaption>
</figure>
<h4>Dot Style</h4>
<p>The default data points are circles with an empty fill. To change the style of the dots (the data points), we use the option ‘pch’:</p>
<pre>plot(dataset,pch=19)</pre>
<figure><img alt="" src="https://i2.wp.com/cdn-images-1.medium.com/max/700/1*tf3MjhLMRil_h3YHEEy57Q.png?w=456&ssl=1" data-recalc-dims="1"><br>
<figcaption>Plot using the ‘pch’ option.</figcaption>
</figure>
<p>The ‘pch’ has accepts several codes, here is a grid with all the different data point styles (the default is 1):</p>
<figure><img alt="" src="https://i1.wp.com/cdn-images-1.medium.com/max/668/1*0QEVFHrgqmkVsCVCQceh6w.jpeg?w=456&ssl=1" data-recalc-dims="1"><br>
<figcaption><a target="_blank" rel="nofollow" href="https://greggilbertlab.sites.ucsc.edu/wp-content/uploads/sites/276/2015/10/symbols.jpg">https://greggilbertlab.sites.ucsc.edu/wp-content/uploads/sites/276/2015/10/symbols.jpg</a></figcaption>
</figure>
<h4>Data Point Size</h4>
<p>To change the size of the data point, we use the ‘cex’ option:</p>
<pre>plot(dataset,pch=19,cex=2)</pre>
<figure><img alt="" src="https://i2.wp.com/cdn-images-1.medium.com/max/700/1*lLljEcQ8Ns7j93aUTFLszw.png?w=456&ssl=1" data-recalc-dims="1"><br>
<figcaption>Plot with the data point style and size options used.</figcaption>
</figure>
<h4>Data Point Color</h4>
<p>The default color for the data points is black, to change that we use the ‘col’ option:</p>
<pre>plot(dataset,pch=19,cex=2,col=&quot;pink&quot;)</pre>
<figure><img alt="" src="https://i0.wp.com/cdn-images-1.medium.com/max/700/1*wBOTx177K_lNewBrFrWBwA.png?w=456&ssl=1" data-recalc-dims="1"><br>
<figcaption>Plot with the data point, size and color options used.</figcaption>
</figure>
<p>The ‘col’ option takes in both words and integers to identify the color. So you can use words like ‘green’, ‘wheat’, ‘red’ etc… and color codes. To get all the colors, just run colors() and it will return all the colors available. So if you know the location of your favorite color in the return value of the colors() function, you can just run plot(dataset,col=colors()[45]) and you’ll have a plot with your favorite color (or just save the color in a variable somewhere in the document if you want to keep re-using it). There are 657 colors in the colors() function.</p>
<h4>Axis Labels</h4>
<p>If you work in a team, or even if you work alone (but especially if you work with a group), always label your axes. If the dataset you’re using has headers or column titles, the plot function will automatically use those as the labels for the plot axes. To add your own, use the xlab() and the ylab() options:</p>
<pre>plot(dataset, xlab(&quot;Speed (km/h)&quot;), ylab(&quot;Distance (km)&quot;))</pre>
<figure><img alt="" src="https://i1.wp.com/cdn-images-1.medium.com/max/700/1*Gst--rOThuKKM8zyZ7iV0A.png?w=456&ssl=1" data-recalc-dims="1"><br>
<figcaption>Plot option with custom axes labels.</figcaption>
</figure>
<h4>Plot Legend</h4>
<p>Plot legends can be called using the legend() function. The first two parameters are the x-position and y-position of the legend on the plot. You call this function right after you plot:</p>
<pre>plot(dataset,col=&quot;blue&quot;,pch=4)<br>legend(20,110,c(&quot;Cars&quot;),col=&quot;blue&quot;,pch=4)</pre>
<figure><img alt="" src="https://i1.wp.com/cdn-images-1.medium.com/max/700/1*tFu6tn2B5ARYDXbgEFGf8Q.png?w=456&ssl=1" data-recalc-dims="1"><br>
<figcaption>Plot legend.</figcaption>
</figure>
<p>You want the legend symbol to match the symbol used in the plot. The legend takes in the same pch codes used in the plot() function for the symbols. In addition, you should of course have the same color for the symbols in the legend and the symbols in the plot. Here’s some of the options you can play around with in the legend() function:</p>
<pre>legend(xPosition: int, yPosition: int, labels: array, col :int|string, cex: int, pch: int)</pre>
<p>These are just what I call the essentials, a lot more in the documentation (see below).</p>
<p>And that’s it. Like I said before, there are several other options you can use like regression/trend lines, plot sizing etc… These are just the essentials when you want a little something extra on your visualization. In particular stages of the data analysis process, the less you add to your plots, the better.</p>
<p><em>Reference and Documentation</em></p>
<ul>
<li><a target="_blank" rel="nofollow" href="https://www.datazar.com/file/f7f286e86-4b5a-49c8-81be-d99639d5b96e">Datazar – Home to Research</a></li>
<li><a target="_blank" rel="nofollow" href="https://stat.ethz.ch/R-manual/R-devel/library/graphics/html/plot.html">R: Generic X-Y Plotting</a></li>
<li><a target="_blank" rel="nofollow" href="https://stat.ethz.ch/R-manual/R-devel/library/graphics/html/legend.html">R: Add Legends to Plots</a></li>
</ul>
<p><img height="1" width="1" src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=711d7f7caef5"><br>
<hr>
<p><a target="_blank" rel="nofollow" href="https://blog.datazar.com/r-plot-function-the-options-711d7f7caef5">R Plot Function — The Options</a> was originally published in <a target="_blank" rel="nofollow" href="https://blog.datazar.com/">Datazar Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</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://blog.datazar.com/r-plot-function-the-options-711d7f7caef5?source=rss----e2c7e6e1c75--r_language"> R Language in Datazar Blog on Medium</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/ql_edVVoW-w"></a></div>