<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/tips-for-great-graphics/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://aghaynes.wordpress.com/2018/05/26/graphics_tips/"> R – Insights of a PhD</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>R is a great program for generating top-notch graphics. But to get the best out of it, you need to put in a little more work. Here are a few tips for adapting your R graphics to make them look a little better.</p>
<p>1) Dont use the “File/Save as…/” menu.</p>
<p>If you set up your graphic in the first place then theres no need to post-process (eg crop, scale etc) the graphic in other software.</p>
<p>Use the graphic devices (jpeg(), tiff(), postscript(), etc), set your height and width to whatever you want the finished product to be and then create the graph.</p>
<pre>tiff(&quot;~/Manuscript/Figs/Fig1.tiff&quot;, width =2, height =2, units =&quot;in&quot;, res = 600)
plot(dist ~ speed, cars) # cars is a base R dataset - data(cars)
dev.off()</pre>
<p>The first argument to a graphic device such as tiff or jpeg is the filename, to which you can include the path. So that I dont have to worry about the order of arguments, I include the argument name. Width and height specify the width and height of the graphic in the units provided, in this case inches, but pixels, cm or mm can also be used. The resolution tells R how higher quality the graphic should be, the higher the better, but if you go too high, you could find that you have problems running out of space. I find 600 a nice compromise. Nice crisp lines, smooth curves and sharp letters. You can then import that straight into MS Word or whatever other word processor you use, or upload to go with that manuscript youve worked so hard on. Although, you could find yourself with BIG files. A 4×6 inch figure I made recently was 17.1MB! And for the web, 100 or 200 is probably enough.</p>
<p>This technique also provides you with the same output every time, which is not the case if you adjust the size of the default device window produced by plot()</p>
<p>2) Dont be afraid to change the default settings!</p>
<p>Personally, I find that a 1 inch margin at the base of the graphic is a bit generous. I also find the ticks a bit long and the gap between tick and the axis label a bit big. Luckily, these things are easy to change!</p>
<pre>jpeg(&quot;t1.jpeg&quot;, width=3, height=3, units=&quot;in&quot;, res=100)
plot(dist~speed, cars) 
dev.off()</pre>
<p>The above code produces this figure.<br>
<a rel="nofollow" href="https://aghaynes.files.wordpress.com/2012/06/t1.jpeg" target="_blank"><img data-orig-file="https://aghaynes.files.wordpress.com/2012/06/t1.jpeg?w=456" data-orig-size="300,300" data-medium-file="https://aghaynes.files.wordpress.com/2012/06/t1.jpeg?w=456?w=300" data-attachment-id="31" data-image-meta="{" data-image-description="" src="https://aghaynes.files.wordpress.com/2012/06/t1.jpeg?w=456" data-permalink="https://aghaynes.wordpress.com/2018/05/26/graphics_tips/t1/" data-large-file="https://aghaynes.files.wordpress.com/2012/06/t1.jpeg?w=456?w=300" alt="" data-image-title="t1" title="t1" sizes="(max-width: 300px) 100vw, 300px" data-comments-opened="1" class="wp-image-31" data-recalc-dims="1"></a><br>
See what I mean about the margins?</p>
<p>Heres how to change it!</p>
<pre>par(mai=c(0.5, 0.5, 0.1, 0.1) )
plot(dist ~ speed, cars, tck = -0.01, las=1, mgp=c(1.4,0.2,0))</pre>
<p><a rel="nofollow" href="https://aghaynes.files.wordpress.com/2012/06/t2.jpeg" target="_blank"><img data-orig-file="https://aghaynes.files.wordpress.com/2012/06/t2.jpeg?w=456" data-orig-size="300,300" data-medium-file="https://aghaynes.files.wordpress.com/2012/06/t2.jpeg?w=456?w=300" data-attachment-id="33" data-image-meta="{" data-image-description="" src="https://aghaynes.files.wordpress.com/2012/06/t2.jpeg?w=456" data-permalink="https://aghaynes.wordpress.com/2018/05/26/graphics_tips/t2/" data-large-file="https://aghaynes.files.wordpress.com/2012/06/t2.jpeg?w=456?w=300" alt="" data-image-title="t2" title="t2" sizes="(max-width: 300px) 100vw, 300px" data-comments-opened="1" class="wp-image-33" data-recalc-dims="1"></a></p>
<p>That call to par changes the “MArgin in Inches” setting. The tck argument to par deals with TiCK length (negative for outside, positive for inside) while mgp controls on which line certain things are printed (titles are the first argument, labels are the second and the axis itself is the third). The las argument controls the rotation of the labels (1 for horizontal, 2 for perpendicular and 3 for vertical).</p>
<p>This leads me nicely to number 3: Dont be afraid to have separate lines for different parts of your plot.</p>
<p>This allows far more freedom and flexibility than setting par arguments in the plot argument. You can have different mgp settings for each axis for instance.</p>
<pre>par(mai=c(0.4, 0.5, 0.1, 0.1))
plot(dist ~ speed, cars, xaxt=&quot;n&quot;, mgp=c(1.4,0.2,0), las=1, tck=-0.01)
axis(side=1, tck = -0.01, las=1, mgp=c(0.5,0.2,0))
mtext(&quot;speed&quot;, side=1, line= 1)</pre>
<p>This plots the same graph, but allows different distances for the x and y axes, in terms of margin and where the title is situated. The axis function places an axis on the side determined by its side argument and mtext places Margin TEXT, again at the side in its argument and in this case on line 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://aghaynes.wordpress.com/2018/05/26/graphics_tips/"> R – Insights of a PhD</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 alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/vX2_sWmROKE" height="1"></a></div>