<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Data Visualization on </title>
    <link>https://plantarum.ca/tags/data-visualization/</link>
    <description>Recent content in Data Visualization on </description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en</language>
    <managingEditor>tyler@plantarum.ca (Tyler Smith)</managingEditor>
    <webMaster>tyler@plantarum.ca (Tyler Smith)</webMaster>
    <lastBuildDate>Wed, 19 Feb 2014 00:00:00 +0000</lastBuildDate>
    
        <atom:link href="https://plantarum.ca/tags/data-visualization/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>Publication Quality R Figures</title>
      <link>https://plantarum.ca/2014/02/19/r-graphics/</link>
      <pubDate>Wed, 19 Feb 2014 00:00:00 +0000</pubDate>
      <author>tyler@plantarum.ca (Tyler Smith)</author>
      <guid>https://plantarum.ca/2014/02/19/r-graphics/</guid>
      <description>
&lt;script src=&#34;https://plantarum.ca/rmarkdown-libs/header-attrs/header-attrs.js&#34;&gt;&lt;/script&gt;

&lt;div id=&#34;TOC&#34;&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#introduction&#34;&gt;Introduction&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#learning-objectives&#34;&gt;Learning Objectives&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#pre-requisites&#34;&gt;Pre-requisites&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#motivation&#34;&gt;Motivation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#building-our-plot&#34;&gt;Building Our Plot&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#size&#34;&gt;Size&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#content&#34;&gt;Content&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#plot-symbols&#34;&gt;Plot Symbols&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#margins&#34;&gt;Margins&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#axes&#34;&gt;Axes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#the-finished-plot&#34;&gt;The finished plot&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#exercise-1-adding-a-legend&#34;&gt;Exercise 1: adding a legend&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#additional-customization&#34;&gt;Additional Customization&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#selecting-plot-symbols&#34;&gt;Selecting Plot Symbols&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#panels&#34;&gt;Panels&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#exercise-2-completing-the-panel&#34;&gt;Exercise 2: Completing the Panel&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#image-formats&#34;&gt;Image Formats&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#raster-images&#34;&gt;Raster Images&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#vector-images&#34;&gt;Vector Images&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;

&lt;div class=&#34;figure&#34;&gt;&lt;span style=&#34;display:block;&#34; id=&#34;fig:final&#34;&gt;&lt;/span&gt;
&lt;img src=&#34;https://plantarum.ca/tutorials/2020-07-20-base-R-plots_files/figure-html/final-1.png&#34; alt=&#34;A. Iris Sepal Size by Species. B. Iris Petal Width&#34; width=&#34;696&#34; /&gt;
&lt;p class=&#34;caption&#34;&gt;
Figure 1: A. Iris Sepal Size by Species. B. Iris Petal Width
&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;introduction&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Introduction&lt;/h1&gt;
&lt;div id=&#34;learning-objectives&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Learning Objectives&lt;/h2&gt;
&lt;p&gt;At the end of this lesson, you should be able to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Customize plots produced with the R base graphics system&lt;/li&gt;
&lt;li&gt;Design multi-panel plots&lt;/li&gt;
&lt;li&gt;Design plots to suit the publication requirements of a journal&lt;/li&gt;
&lt;li&gt;Save your plots as high-resolution raster or vector image files as
required by your publisher&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div id=&#34;pre-requisites&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Pre-requisites&lt;/h2&gt;
&lt;p&gt;You will need:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A recent version of R installed on your computer&lt;/li&gt;
&lt;li&gt;Familiarity editing R scripts and passing commands from a script to the R
interpreter&lt;/li&gt;
&lt;li&gt;Note that RStudio is not ideal for this lesson, due to limitations in how
it processes plotting commands; the default RGui installed on Windows or
Mac will work better&lt;/li&gt;
&lt;li&gt;These notes!&lt;/li&gt;
&lt;li&gt;Optionally, some of your own data to work with during the exercises&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div id=&#34;motivation&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Motivation&lt;/h2&gt;
&lt;p&gt;You have several options for plotting with R. The simplest is the built-in
or base graphics package. Base graphics are less powerful than newer
alternatives like lattice or ggplot2. On the other hand, it’s much easier
to customize base graphics than the others. For this reason, I prefer to
use the built-in functions when preparing single-panel plots.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;ggplot2&lt;/code&gt; is definitely worth investigating, especially if you want to
produce complex multi-panel faceted plots. The &lt;a href=&#34;http://ggplot2.org/&#34;&gt;official
website&lt;/a&gt; has all the documentation. Roger Peng has
also posted a very nice introductory &lt;a href=&#34;https://www.youtube.com/watch?v=HeqHMM4ziXA&amp;amp;feature=youtube_gdata_player&#34;&gt;video on
YouTube&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;building-our-plot&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Building Our Plot&lt;/h1&gt;
&lt;p&gt;For this example, we’ll use the guidelines provided by the &lt;a href=&#34;https://bsapubs.onlinelibrary.wiley.com/hub/journal/15372197/homepage/forauthors#ps&#34;&gt;American
Journal of
Botany&lt;/a&gt;. AJB
accepts figures 3.5 inches (1 column), 5-6 inches (1.5 columns), or 7.25
inches wide (2 columns). The height can be up to 9 inches. We’ll start with
a one-column plot, so the dimensions should be 3.5 inches wide.&lt;/p&gt;
&lt;p&gt;The figure we’ll plot is from the built-in &lt;code&gt;iris&lt;/code&gt; data set. We’ll do a
simple scatterplot of &lt;code&gt;Sepal.Length&lt;/code&gt; against &lt;code&gt;Sepal.Width&lt;/code&gt;.&lt;/p&gt;
&lt;div id=&#34;size&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Size&lt;/h2&gt;
&lt;p&gt;Let’s start with a square. If we need more height, we can increase the size
as necessary. Similarly, if we decide we need to stretch our figure over
two columns, we can change later.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note that RStudio isn’t the best environment for this exercise.&lt;/strong&gt;
Unfortunately, it’s not possible to create new plot windows in RStudio, so
you can’t specify the dimensions of the figure for the on-screen display.
Consequently, when you save your figure to an image file, you won’t
necessarily get exactly what you see on the screen. In many cases, this may
well be fine. But double-check the image file to make sure that you get
what you expected. If you didn’t, it may be because the on-screen display
and the saved image were not close enough to the same dimensions.&lt;/p&gt;
&lt;p&gt;To set up the canvas for our plot, start a new device:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;dev.new(height = 3.5, width = 3.5)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you are using RStudio, &lt;code&gt;dev.new()&lt;/code&gt; won’t work. Instead, drag the edges
of the plot window to get as close to a 3.5&#34; square as possible.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;content&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Content&lt;/h2&gt;
&lt;p&gt;Now that our canvas is ready, we can start placing our graphics. Let’s
start with the default plot.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;plot(Sepal.Length ~ Sepal.Width, data = iris)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://plantarum.ca/tutorials/2020-07-20-base-R-plots_files/figure-html/iris-fig-default-source-1.png&#34; width=&#34;336&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;plot-symbols&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Plot Symbols&lt;/h2&gt;
&lt;p&gt;The default plot uses the same symbol for each point. However, our data
frame includes samples from three different species:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;str(iris)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## &amp;#39;data.frame&amp;#39;:    150 obs. of  5 variables:
##  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
##  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
##  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
##  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
##  $ Species     : Factor w/ 3 levels &amp;quot;setosa&amp;quot;,&amp;quot;versicolor&amp;quot;,..: 1 1 1 1 1 1 1 1 1 1 ...&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Recall that a factor is just a vector of integers, with each integer having
it’s own label. We can display the underlying numbers by converting from
factor to numeric:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;as.numeric(iris$Species)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##   [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
##  [38] 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
##  [75] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3
## [112] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
## [149] 3 3&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is a very useful feature. It means we can set a different plot symbol
for each species:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;plot(Sepal.Length ~ Sepal.Width, pch = as.numeric(Species),
     data = iris)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://plantarum.ca/tutorials/2020-07-20-base-R-plots_files/figure-html/iris-fig-1-1.png&#34; width=&#34;336&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;margins&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Margins&lt;/h2&gt;
&lt;p&gt;Now we can see what we’re working with, but the layout isn’t ideal. In
particular, the plot is very small relative to the size of the figure. We
can fix this with the &lt;code&gt;mar&lt;/code&gt; parameter. &lt;code&gt;mar&lt;/code&gt; takes a vector of four
integers, which set the width of the margin on the bottom, left, top and
right sides respectively (remember clockwise from the bottom!). These
numbers refer to the width of each margin in &lt;code&gt;lines&lt;/code&gt; — i.e., the width
required for a single line of text. The default is &lt;code&gt;c(5, 4, 4, 2) + 0.1&lt;/code&gt;.
The top margin in particular is usually too wide. We will very rarely add a
title to a published figure, so we don’t need to set aside space for it.&lt;/p&gt;
&lt;p&gt;Set the value of &lt;code&gt;mar&lt;/code&gt; with the &lt;code&gt;par&lt;/code&gt; function.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;par(mar = c(3, 3, 0.5, 0.5))
plot(Sepal.Length ~ Sepal.Width, pch = as.numeric(Species),
     data = iris)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://plantarum.ca/tutorials/2020-07-20-base-R-plots_files/figure-html/iris-fig-2-1.png&#34; width=&#34;336&#34; /&gt;&lt;/p&gt;
&lt;p&gt;That’s better. But we’ve lost our axis labels. They aren’t actually lost,
but they are plotted outside of the margins we’ve set, so they are no
longer visible. I find the defaults that R uses for the axes to be larger
than we need. Better to turn off the axes entirely and replot them
ourselves.&lt;/p&gt;
&lt;p&gt;Note that once the margins are set with &lt;code&gt;par()&lt;/code&gt;, they will keep their value
until we open a new plot window, or reset them.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;axes&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Axes&lt;/h2&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;par(mar = c(3, 3, 0.5, 0.5))
plot(Sepal.Length ~ Sepal.Width, pch = as.numeric(Species),
     data = iris, 
     ann = FALSE,      # turn off axis labels
     axes = FALSE)     # turn off axis ticks&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://plantarum.ca/tutorials/2020-07-20-base-R-plots_files/figure-html/iris-fig-3-1.png&#34; width=&#34;336&#34; /&gt;&lt;/p&gt;
&lt;p&gt;Notice that &lt;code&gt;axes = FALSE&lt;/code&gt; has turned of the box around our plot. We can put it back easily:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;box()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we can explicitly add each axis with their size and placement specified:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;axis(side = 1, tcl = -0.2, mgp = c(3, 0.3, 0),
     cex.axis = 0.8) 
axis(side = 2, tcl = -0.2, mgp = c(3, 0.3, 0),
     cex.axis = 0.8) &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://plantarum.ca/tutorials/2020-07-20-base-R-plots_files/figure-html/iris-fig-3b-1.png&#34; width=&#34;336&#34; /&gt;&lt;/p&gt;
&lt;p&gt;What just happened? Let’s breakdown the arguments to &lt;code&gt;axis&lt;/code&gt;:&lt;/p&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;code&gt;side&lt;/code&gt;:&lt;/dt&gt;
&lt;dd&gt;which side of the plot, clockwise from bottom, same as for &lt;code&gt;mar&lt;/code&gt;
above
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;tcl&lt;/code&gt;:&lt;/dt&gt;
&lt;dd&gt;length of the ticks. Negative values indicate extending outwards
from plot, positive values extend inward. The default is -0.5, which I
find a bit too long.
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;mgp&lt;/code&gt;:&lt;/dt&gt;
&lt;dd&gt;&lt;code&gt;margin line&lt;/code&gt;, a vector of three numbers, which indicate the
position of the axis title, axis labels, and axis line, respectively. The
values are the number of &lt;code&gt;lines&lt;/code&gt; away from the plot border to place each
item, with &lt;code&gt;0&lt;/code&gt; indicating the margin of the plot area. Note that title
doesn’t matter here, since we aren’t using an axis title (yet).
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;cex.axis&lt;/code&gt;:&lt;/dt&gt;
&lt;dd&gt;axis character expansion. Scale the size of the tick labels.
&amp;lt; 1 reduces the size, &amp;gt; 1 increases the size.
&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;Now we can add our axis titles back in:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;mtext(&amp;quot;Sepal Width&amp;quot;, side = 1, line = 1.5)
mtext(&amp;quot;Sepal Length&amp;quot;, side = 2, line = 1.5)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here, we can use &lt;code&gt;line&lt;/code&gt; to adjust the distance between the label text and the axis.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;the-finished-plot&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;The finished plot&lt;/h2&gt;
&lt;p&gt;Putting this altogether gives us the following plot.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;par(mar = c(3, 3, 0.5, 0.5))
plot(Sepal.Length ~ Sepal.Width, pch = as.numeric(Species),
     data = iris, 
     ann = FALSE,      # turn off axis labels
     axes = FALSE)     # turn off axis ticks
box()
axis(side = 1, tcl = -0.2, mgp = c(3, 0.3, 0),
     cex.axis = 0.8) 
axis(side = 2, tcl = -0.2, mgp = c(3, 0.3, 0),
     cex.axis = 0.8) 
mtext(&amp;quot;Sepal Width&amp;quot;, side = 1, line = 1.5)
mtext(&amp;quot;Sepal Length&amp;quot;, side = 2, line = 1.5)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://plantarum.ca/tutorials/2020-07-20-base-R-plots_files/figure-html/iris-finished-plot-1.png&#34; width=&#34;336&#34; /&gt;&lt;/p&gt;
&lt;div id=&#34;exercise-1-adding-a-legend&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Exercise 1: adding a legend&lt;/h3&gt;
&lt;p&gt;We now have a complete figure. We could provide an explanation of the
symbols in the caption, but it might be nicer to have a legend plotted on
the figure. This is easily done with the &lt;code&gt;legend()&lt;/code&gt; function.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;legend(legend = levels(iris$Species), x=&amp;quot;topleft&amp;quot;,
       pch = 1:3)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://plantarum.ca/tutorials/2020-07-20-base-R-plots_files/figure-html/legend-code-1.png&#34; width=&#34;336&#34; /&gt;&lt;/p&gt;
&lt;p&gt;Since we set the &lt;code&gt;pch&lt;/code&gt; argument in our plots using the factor
&lt;code&gt;iris$Species&lt;/code&gt;, we can use &lt;code&gt;levels()&lt;/code&gt; function to extract the labels for
the legend. &lt;code&gt;pch&lt;/code&gt; indicates the actual symbols to use, and &lt;code&gt;x&lt;/code&gt; is the
location of the legend.&lt;/p&gt;
&lt;p&gt;This is clearly not “publication quality.” Our plot needs a bit more space
for the legend. See if you can make an attractive plot. The following
options might be helpful:&lt;/p&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;code&gt;dev.new()&lt;/code&gt;:&lt;/dt&gt;
&lt;dd&gt;&lt;code&gt;width, height&lt;/code&gt;
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;plot()&lt;/code&gt;:&lt;/dt&gt;
&lt;dd&gt;&lt;code&gt;xlim, ylim, cex&lt;/code&gt;
&lt;/dd&gt;
&lt;dt&gt;&lt;code&gt;legend()&lt;/code&gt;:&lt;/dt&gt;
&lt;dd&gt;&lt;code&gt;x, y, bty, horiz, cex, pt.cex, text.width&lt;/code&gt;
&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;If you need a hint, take a look at the next section.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;additional-customization&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Additional Customization&lt;/h1&gt;
&lt;div id=&#34;selecting-plot-symbols&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Selecting Plot Symbols&lt;/h2&gt;
&lt;p&gt;If you want to select different symbols, it’s easy to do using R’s
subsetting syntax. By default, for three levels of our &lt;code&gt;Species&lt;/code&gt; factor, we
get symbols 1, 2, and 3. If instead we wanted to use symbols&lt;a href=&#34;#fn1&#34; class=&#34;footnote-ref&#34; id=&#34;fnref1&#34;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt; 19, 5, and
3, we could do this:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;par(mar = c(3, 3, 0.5, 0.5))
mysymbols &amp;lt;- c(19, 5, 3)
plot(Sepal.Length ~ Sepal.Width,
     pch = mysymbols[as.numeric(Species)], data = iris,
     ylim = c(4.0, 8.5), ann = FALSE, axes = FALSE)
box()
axis(side = 1, tcl = -0.2, mgp = c(3, 0.3, 0),
     cex.axis = 0.8) 
axis(side = 2, tcl = -0.2, mgp = c(3, 0.3, 0),
     cex.axis = 0.8) 
mtext(&amp;quot;Sepal Width&amp;quot;, side = 1, line = 1.5)
mtext(&amp;quot;Sepal Length&amp;quot;, side = 2, line = 1.5)
legend(legend = levels(iris$Species), x = &amp;quot;top&amp;quot;,
       pch = mysymbols, horiz = TRUE, bty = &amp;#39;n&amp;#39;,
       cex = 0.9, text.width = c(0.6, 0.7, 0.6))&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://plantarum.ca/tutorials/2020-07-20-base-R-plots_files/figure-html/symbols-1.png&#34; width=&#34;336&#34; /&gt;&lt;/p&gt;
&lt;p&gt;That’s an important application of &lt;code&gt;R&lt;/code&gt;’s subsetting commands, so make sure
you follow what happened — we subset the &lt;code&gt;mysymbols&lt;/code&gt; vector with the
longer &lt;code&gt;as.numeric(iris$Species)&lt;/code&gt; vector, which converted the values from
&lt;code&gt;(1, 2, 3)&lt;/code&gt; to &lt;code&gt;(19, 5, 3)&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;mysymbols&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 19  5  3&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;as.numeric(iris$Species)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##   [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
##  [38] 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
##  [75] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3
## [112] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
## [149] 3 3&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;mysymbols[as.numeric(iris$Species)]&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##   [1] 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19
##  [26] 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19
##  [51]  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5
##  [76]  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5
## [101]  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3
## [126]  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&#34;panels&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Panels&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;ggplot2&lt;/code&gt; provides a very sophisticated system for producing multi-panel
plots. But it’s easy enough to create a simple panel using the base
graphics. For this example, let’s do a two-plot horizontal panel, with our
scatter plot in the first position, and a boxplot of petal widths in the
second position. A two-column plot in AJB is 7.25 inches wide:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;dev.new(width = 7.25, height = 3.5)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next we need, to inform R that we’re splitting the figure into two panels:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;par(mfrow = c(1, 2))&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;mfrow&lt;/code&gt; sets the graphics device for rows and columns, in this case one
row, two columns. We can now put our first plot in the first spot:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://plantarum.ca/tutorials/2020-07-20-base-R-plots_files/figure-html/panel1-1.png&#34; width=&#34;696&#34; /&gt;&lt;/p&gt;
&lt;p&gt;After dividing a plot device into panels with &lt;code&gt;mfrow&lt;/code&gt;, the first high-level
plot (i.e., &lt;code&gt;plot, boxplot&lt;/code&gt; etc.) command will be placed in the first
panel. All subsequent low-level plotting commands (i.e., &lt;code&gt;legend, axis, mtext&lt;/code&gt; etc.) will be added to this same panel. When the next high-level
command is called, it will be placed in the next panel, and focus shifts
with it. So we can now add our boxplot to the second panel:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;boxplot(Petal.Width ~ Species, data = iris)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://plantarum.ca/tutorials/2020-07-20-base-R-plots_files/figure-html/panel2-1.png&#34; width=&#34;696&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;Note that the margins we set for the first panel are still in effect.
Consequently, we’ve lost the axis labels on our second plot. It’s going to
need some attention to make it look right. We’ll leave that for the next
exercise.&lt;/p&gt;
&lt;p&gt;In the meantime, we have one more requirement to meet. On multi-figure
panels, AJB requires an uppercase letter (A, B, etc) to label each plot.
This label should go in the upper-left corner of each panel. This is easy
to do with the &lt;code&gt;text&lt;/code&gt; command. At the moment, we don’t have space in the
upper-left corner, so we’ll put the labels in the lower-right temporarily.
For example:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;## For the first panel:
text(&amp;quot;A&amp;quot;, x = 4.2, y = 4.5, cex = 2)&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;## For the second panel:
text(&amp;quot;B&amp;quot;, x = 3.2, y = 0.24, cex = 2)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://plantarum.ca/tutorials/2020-07-20-base-R-plots_files/figure-html/panel2B-1.png&#34; width=&#34;696&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;Unfortunately, each figure is plotted on different scales, so placing the
letters in the same position is not straightforward. Luckily, R provides a
function for getting “universal” coordinates for every plot.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;## For the first panel:
text(&amp;quot;A&amp;quot;, x = grconvertX(0.9, from=&amp;quot;npc&amp;quot;, to=&amp;quot;user&amp;quot;), 
     y = grconvertY(0.1, from = &amp;quot;npc&amp;quot;, to=&amp;quot;user&amp;quot;), cex = 2)&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;## For the second panel:
text(&amp;quot;B&amp;quot;, x = grconvertX(0.9, from=&amp;quot;npc&amp;quot;, to=&amp;quot;user&amp;quot;), 
     y = grconvertY(0.1, from = &amp;quot;npc&amp;quot;, to=&amp;quot;user&amp;quot;), cex = 2)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The functions &lt;code&gt;grconvertX&lt;/code&gt; and &lt;code&gt;grconvertY&lt;/code&gt; convert between different
coordinate systems. &lt;code&gt;npc&lt;/code&gt; is “normalized plot coordinates.” In this system,
(0, 0) is the lower left corner of the plot, and (1, 1) is the upper right
corner. &lt;code&gt;user&lt;/code&gt;, on the other hand, is the coordinate system in effect for
the actual plotted data. Which, for our Panel A, means the lower right
corner is ca. (2.0, 4.0) and the upper left corner is ca. (4.5, 8.5). So
&lt;code&gt;grconvertX(0.9, from = &#34;npc&#34;, to = &#34;user&#34;)&lt;/code&gt; returns the X coordinate to
plot our text 90% of the way to the left side of the plot, regardless of
the scale used in that plot. With this addition, we have the following
code, and the generated panels:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;par(mfrow = c(1, 2))
par(mar = c(3, 3, 0.5, 0.5))
mysymbols &amp;lt;- c(19, 5, 3)
plot(Sepal.Length ~ Sepal.Width,
     pch = mysymbols[as.numeric(Species)], data = iris,
     ylim = c(4.0, 8.5), ann = FALSE, axes = FALSE)
box()
axis(side = 1, tcl = -0.2, mgp = c(3, 0.3, 0),
     cex.axis = 0.8) 
axis(side = 2, tcl = -0.2, mgp = c(3, 0.3, 0),
     cex.axis = 0.8) 
mtext(&amp;quot;Sepal Width&amp;quot;, side = 1, line = 1.5)
mtext(&amp;quot;Sepal Length&amp;quot;, side = 2, line = 1.5)
legend(legend = levels(iris$Species), x = &amp;quot;top&amp;quot;,
       pch = mysymbols, horiz = TRUE, bty = &amp;#39;n&amp;#39;,
       cex = 0.9, text.width = c(0.6, 0.7, 0.6))
## For the first panel:
text(&amp;quot;A&amp;quot;, x = grconvertX(0.9, from=&amp;quot;npc&amp;quot;, to=&amp;quot;user&amp;quot;), 
     y = grconvertY(0.1, from = &amp;quot;npc&amp;quot;, to=&amp;quot;user&amp;quot;), cex = 2)
boxplot(Petal.Width ~ Species, data = iris)
## For the second panel:
text(&amp;quot;B&amp;quot;, x = grconvertX(0.9, from=&amp;quot;npc&amp;quot;, to=&amp;quot;user&amp;quot;), 
     y = grconvertY(0.1, from = &amp;quot;npc&amp;quot;, to=&amp;quot;user&amp;quot;), cex = 2)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://plantarum.ca/tutorials/2020-07-20-base-R-plots_files/figure-html/panel2Bc-1.png&#34; width=&#34;696&#34; /&gt;&lt;/p&gt;
&lt;div id=&#34;exercise-2-completing-the-panel&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Exercise 2: Completing the Panel&lt;/h3&gt;
&lt;p&gt;There are still a few problems with our panel:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The title of the Y axis on the second panel is not visible&lt;/li&gt;
&lt;li&gt;The panel labels (A and B) are in the wrong positions — they should be
in the top left corners&lt;/li&gt;
&lt;li&gt;Fixing the panel labels will require moving the legend for the first figure&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you change &lt;code&gt;ylim&lt;/code&gt;, you can put the legend on the bottom, and make space
for the label at the top. Go ahead and see what you can do with this. You
can use my example, Figure 1 at the top of this article as a model. There
is a trick to formatting the x-axis of boxplots. When calling the function
&lt;code&gt;axis&lt;/code&gt;, you’ll have to set the &lt;code&gt;at&lt;/code&gt; argument to indicate where to plot the
labels (which should be &lt;code&gt;c(1, 2, 3)&lt;/code&gt;, and you’ll have to set the &lt;code&gt;labels&lt;/code&gt;
argument to indicate what the labels should be.&lt;/p&gt;
&lt;p&gt;Alternatively, try formatting your own data according to the requirements
of a journal in your field.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;image-formats&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Image Formats&lt;/h1&gt;
&lt;p&gt;R can save graphics to a variety of formats, including anything your target
journal might require. In general, you can store your images in one of two
classes of file format:&lt;/p&gt;
&lt;dl&gt;
&lt;dt&gt;raster:&lt;/dt&gt;
&lt;dd&gt;images are stored as a matrix of values, with each value indicating the color of a single pixel in the grid. Best used for photographs. Examples: jpg, tiff, png.
&lt;/dd&gt;
&lt;dt&gt;vector:&lt;/dt&gt;
&lt;dd&gt;images are stored as a series of mathematical instructions for re-creating the display: lines, polygons, text etc. Best used for line drawings. Examples: eps, svg.
&lt;/dd&gt;
&lt;/dl&gt;
&lt;div id=&#34;raster-images&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Raster Images&lt;/h2&gt;
&lt;p&gt;Raster images are stored as a grid of numbers called pixels. Each number
records the colour of a single pixel in the image. As a consequence, the
image resolution is limited by the number of pixels recorded in the file.
In our example, we need a figure 3.5 inches wide. AJB requires a resolution
of 1000 dots per inch (DPI) for line drawings, which means we need a source
image 3500 pixels in the x and y dimension. We don’t actually need to do
these calculations, though, R will handle it for us. We just need to pick a
format and set the final resolution.&lt;/p&gt;
&lt;p&gt;AJB prefers TIFF format for raster files. To generate one we will use the
&lt;code&gt;tiff()&lt;/code&gt; function. Note that this function only sets the file details for
our plot; we need to add the plotting code after we open the file, and
close it when we’re done with &lt;code&gt;dev.off()&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;tiff(filename = &amp;quot;iris.tiff&amp;quot;, width = 3.5, height = 3.5,
     units=&amp;quot;in&amp;quot;, res = 1000, compression = &amp;quot;lzw&amp;quot;)
par(mar = c(3, 3, 0.5, 0.5))
plot(Sepal.Length ~ Sepal.Width, pch = as.numeric(Species),
     data = iris, 
     ann = FALSE,      # turn off axis labels
     axes = FALSE)     # turn off axis ticks
## Insert additional plot code here
dev.off()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;width&lt;/code&gt;, &lt;code&gt;height&lt;/code&gt; and &lt;code&gt;units&lt;/code&gt; set the size of the image, &lt;code&gt;res&lt;/code&gt; sets the
resolution in points per inch. &lt;code&gt;compression&lt;/code&gt; reduces the size of the file.
The &lt;code&gt;lzw&lt;/code&gt; options is only available for &lt;code&gt;tiff&lt;/code&gt; files. It’s lossless, which
means the compressed image is just as good as the original, so there’s no
reason not to use it. In this case, it reduces the file size from 36Mb to
366K — a 99% reduction!&lt;/p&gt;
&lt;p&gt;To create the same image as a &lt;code&gt;jpg&lt;/code&gt; with the same resolution we’d use:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;jpeg(filename = &amp;quot;iris.jpg&amp;quot;, width = 3.5, height = 3.5,
     units = &amp;quot;in&amp;quot;, res = 1000, quality = 85)
## insert plot code here!
dev.off()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;jpg&lt;/code&gt; files are always compressed, and they use a lossy compression. That
means there is some degradation of the image quality associated with the
compression. The &lt;code&gt;quality&lt;/code&gt; argument determines how aggressively the image
is compressed. Higher values produce larger, less-degraded images. As a
rule of thumb, 85 usually produces fine images at a reasonable size. In
this case, the file is 550K, so a little larger than the compressed TIFF
file.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;vector-images&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Vector Images&lt;/h2&gt;
&lt;p&gt;Vector images are stored as a list of instructions: ‘draw a line from here
to here, put a circle at this coordinate’ etc. As a consequence, they don’t
have an inherent resolution; rather, they can be printed at any resolution
necessary. So we don’t worry about the resolution when creating them, just
the size and width. There are other options we need to be concerned with
here:&lt;/p&gt;
&lt;dl&gt;
&lt;dt&gt;paper:&lt;/dt&gt;
&lt;dd&gt;&lt;code&gt;&#34;special&#34;&lt;/code&gt; indicates that we are making a single image, not a full-page
&lt;/dd&gt;
&lt;dt&gt;onefile:&lt;/dt&gt;
&lt;dd&gt;&lt;code&gt;FALSE&lt;/code&gt; indicates that we are making a new file for each image (probably
not necessary with a single image)
&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;horizontal:
:&lt;code&gt;FALSE&lt;/code&gt; indicates we don’t want a landscape-orientation&lt;/p&gt;
&lt;p&gt;To create an &lt;code&gt;eps&lt;/code&gt; file:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;postscript(&amp;quot;iris.eps&amp;quot;, height = 3.5, width = 3.5,
           paper = &amp;quot;special&amp;quot;, onefile = FALSE,
           horizontal = FALSE) 
## insert plot code here!
dev.off()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This file is only 11K, and can be printed at any resolution. That makes
&lt;code&gt;eps&lt;/code&gt; a very convenient format to use. However, you may run into issues
with fonts. By default, &lt;code&gt;eps&lt;/code&gt; files produced by R don’t include the fonts,
just the position of the letters to place on the image. If you need to
embed the fonts, you need to explicitly request this:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;embedFonts(&amp;quot;iris.eps&amp;quot;, outfile=&amp;quot;iris-embed.eps&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This command creates a new file, &lt;code&gt;iris-embed.eps&lt;/code&gt;, that has the font
information embedded in the file. Fonts can be tricky, and specific details
vary between Windows, Mac and Linux. It’s easiest to stick to the default
font settings, and only dive into custom fonts and settings if you are
required by the publisher.&lt;/p&gt;
&lt;p&gt;Note that, &lt;code&gt;pdf&lt;/code&gt; files are more common than &lt;code&gt;eps&lt;/code&gt;. You can create &lt;code&gt;pdf&lt;/code&gt;
image files directly from &lt;code&gt;R&lt;/code&gt; as well, using:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;pdf(&amp;quot;iris.pdf&amp;quot;, height = 3.5, width = 3.5,
    paper = &amp;quot;special&amp;quot;, onefile = FALSE) 
## insert plot code here!
dev.off()&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;footnotes&#34;&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id=&#34;fn1&#34;&gt;&lt;p&gt;How did I pick 19, 5 and 3? You can see all 25 symbols
with &lt;code&gt;plot(1:25, pch = 1:25)&lt;/code&gt;.&lt;a href=&#34;#fnref1&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
</description>
    </item>
    
  </channel>
</rss>
