<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>plotting Archives - Data Analytics</title>
	<atom:link href="https://dataanalytics.org.uk/tag/plotting/feed/" rel="self" type="application/rss+xml" />
	<link>https://dataanalytics.org.uk/tag/plotting/</link>
	<description>Understanding Data</description>
	<lastBuildDate>Thu, 28 Jul 2022 09:54:08 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.1</generator>

<image>
	<url>https://dataanalytics.org.uk/wp-content/uploads/2019/11/cropped-DA-graph-logo-2019-cyan-600-32x32.png</url>
	<title>plotting Archives - Data Analytics</title>
	<link>https://dataanalytics.org.uk/tag/plotting/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Spine plots using R</title>
		<link>https://dataanalytics.org.uk/spine-plots-using-r/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=spine-plots-using-r</link>
		
		<dc:creator><![CDATA[gmark]]></dc:creator>
		<pubDate>Thu, 28 Jul 2022 09:50:31 +0000</pubDate>
				<category><![CDATA[Introduction-to-R]]></category>
		<category><![CDATA[data visualisation]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[plot]]></category>
		<category><![CDATA[plotting]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[spine plot]]></category>
		<category><![CDATA[spineplot]]></category>
		<category><![CDATA[spinogram]]></category>
		<category><![CDATA[visualisation]]></category>
		<category><![CDATA[visualization]]></category>
		<guid isPermaLink="false">https://www.dataanalytics.org.uk/?p=39519</guid>

					<description><![CDATA[<p>Spine Plots using R A spine plot is similar to a mosaic plot and stacked bar chart. Use spineplot()  function to draw spine [&#8230;]</p>
<p>The post <a href="https://dataanalytics.org.uk/spine-plots-using-r/">Spine plots using R</a> appeared first on <a href="https://dataanalytics.org.uk">Data Analytics</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1>Spine Plots using R</h1>
<p>A spine plot is similar to a mosaic plot and stacked bar chart. Use <code>spineplot()</code>  function to draw spine plots using R. There are quite a number of potential arguments you can use:</p>
<pre><code class="language-r" lang="r"><span style="color: #0000ff;">spineplot</span>(x, y = NULL,
          breaks = NULL, tol.ylab = 0.05, off = NULL,
          ylevels = NULL, col = NULL,
          main = "", xlab = NULL, ylab = NULL,
          xaxlabels = NULL, yaxlabels = NULL,
          xlim = NULL, ylim = c(0, 1), axes = TRUE, ...)
</code></pre>
<p>The major parameters are:</p>
<figure>
<table>
<thead>
<tr>
<th>Parameter</th>
<th>Result</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>x</code></td>
<td>data (x,y) or formula <code>y~x</code></td>
</tr>
<tr>
<td><code>breaks</code></td>
<td>passed to <code>hist()</code></td>
</tr>
<tr>
<td><code>off</code></td>
<td>space between bars</td>
</tr>
<tr>
<td><code>ylevels</code></td>
<td>order of levels in <code>x</code></td>
</tr>
<tr>
<td><code>col</code></td>
<td>colors</td>
</tr>
<tr>
<td><code>xaxlabels</code></td>
<td>labels for x-axis</td>
</tr>
</tbody>
</table>
</figure>
<p>Your data might be in one of two forms, which affects the kind of plot you get:</p>
<ul>
<li><code>category ~ category</code> results in a spine plot (like a 100% stacked bar chart).</li>
<li><code>factor ~ numeric</code> results in a spinogram (like a histogram).</li>
</ul>
<h2>Spine plots</h2>
<p>If your data are <code>category ~ category</code> your <code>spineplot</code> results in a kind of stacked bar chart.</p>
<p>Look at the <code>VADeaths</code> dataset (a <code>matrix</code>) as an example:</p>
<pre><code class="language-r" lang="r">VADeaths
</code></pre>
<pre><code>      Rural Male Rural Female Urban Male Urban Female
50-54       11.7          8.7       15.4          8.4
55-59       18.1         11.7       24.3         13.6
60-64       26.9         20.3       37.0         19.3
65-69       41.0         30.9       54.6         35.1
70-74       66.0         54.3       71.1         50.0
</code></pre>
<pre><code class="language-r" lang="r"><span style="color: #0000ff;">spineplot</span>(VADeaths)
</code></pre>
<div id="attachment_39517" style="width: 510px" class="wp-caption aligncenter"><img fetchpriority="high" decoding="async" aria-describedby="caption-attachment-39517" class="wp-image-39517" src="https://www.dataanalytics.org.uk/wp-content/uploads/2022/06/spine-vad-300x300.png" alt="" width="500" height="500" srcset="https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-vad-300x300.png 300w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-vad-150x150.png 150w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-vad-768x768.png 768w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-vad-600x600.png 600w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-vad-100x100.png 100w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-vad.png 900w" sizes="(max-width: 500px) 100vw, 500px" /><p id="caption-attachment-39517" class="wp-caption-text">A simple spine plot from a categorical matrix</p></div>
<p>You can tinker with the graphical parameters to make the chart look &#8220;nicer&#8221;:</p>
<pre><code class="language-r" lang="r"># Custom colours, bar space, and axis labels
<span style="color: #0000ff;">spineplot</span>(VADeaths, col = terrain.colors(4),
          off = 5,
          xlab = "Age Class",
          ylab = "Category")
</code></pre>
<div id="attachment_39516" style="width: 510px" class="wp-caption aligncenter"><img decoding="async" aria-describedby="caption-attachment-39516" class="wp-image-39516" src="https://www.dataanalytics.org.uk/wp-content/uploads/2022/06/spine-vad-col-300x300.png" alt="" width="500" height="500" srcset="https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-vad-col-300x300.png 300w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-vad-col-150x150.png 150w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-vad-col-768x768.png 768w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-vad-col-600x600.png 600w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-vad-col-100x100.png 100w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-vad-col.png 900w" sizes="(max-width: 500px) 100vw, 500px" /><p id="caption-attachment-39516" class="wp-caption-text">Graphical parameters used to prettify a spine plot</p></div>
<p>It is hard to resize name labels, as <code>cex</code>, <code>las</code> and so on do not work! The solution is to set these parameters globally using <code>par()</code> and reset them after drawing your plot.</p>
<p>In the following example custom <code>names</code> are also used to help &#8220;fit&#8221; labels in the plot:</p>
<pre><code class="language-r" lang="r">opar &lt;- <span style="color: #0000ff;">par</span>(cex.axis = 0.6, las = 2)
<span style="color: #0000ff;">spineplot</span>(USPersonalExpenditure,
          xlab = "", ylab = "",
          xaxlabels = c("FT", "HO", "MH", "PC", "PE"))
<span style="color: #0000ff;">par</span>(opar)
</code></pre>
<div id="attachment_39515" style="width: 510px" class="wp-caption aligncenter"><img decoding="async" aria-describedby="caption-attachment-39515" class="wp-image-39515" src="https://www.dataanalytics.org.uk/wp-content/uploads/2022/06/spine-uspe-300x300.png" alt="" width="500" height="500" srcset="https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-uspe-300x300.png 300w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-uspe-150x150.png 150w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-uspe-768x768.png 768w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-uspe-600x600.png 600w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-uspe-100x100.png 100w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-uspe.png 900w" sizes="(max-width: 500px) 100vw, 500px" /><p id="caption-attachment-39515" class="wp-caption-text">Axis labels are set using par() before drawing a spineplot</p></div>
<h3>Multi-dimensional tables</h3>
<p>The <code>spineplot()</code> function can only deal with 2-dimensional objects. If you have a multi-dimensional <code>table</code> you need to collapse the table to 2D.</p>
<pre><code class="language-r" lang="r"><span style="color: #0000ff;">spineplot</span>(HairEyeColor)
</code></pre>
<pre><code>Error in spineplot.default(HairEyeColor) :
  a 2-way table has to be specified
</code></pre>
<pre><code class="language-r" lang="r">x &lt;- <span style="color: #0000ff;">margin.table</span>(HairEyeColor, margin = c(1,2))
x
</code></pre>
<pre><code>       Eye
Hair    Brown Blue Hazel Green
  Black    68   20    15     5
  Brown   119   84    54    29
  Red      26   17    14    14
  Blond     7   94    10    16
</code></pre>
<pre><code class="language-r" lang="r"><span style="color: #0000ff;">spineplot</span>(x, col = c("brown", "blue", "tan", "green"))
</code></pre>
<div id="attachment_39514" style="width: 510px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-39514" class="wp-image-39514" src="https://www.dataanalytics.org.uk/wp-content/uploads/2022/06/spine-table-300x300.png" alt="" width="500" height="500" srcset="https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-table-300x300.png 300w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-table-150x150.png 150w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-table-768x768.png 768w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-table-600x600.png 600w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-table-100x100.png 100w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-table.png 900w" sizes="(max-width: 500px) 100vw, 500px" /><p id="caption-attachment-39514" class="wp-caption-text">A multi-dimensional table needs to be collapsed to 2D for plotting</p></div>
<h2>Spinograms</h2>
<p>A spinogram is a <code>spineplot</code> where the data is in the form <code>factor ~ numeric</code>. A spinogram is analogous to a histogram.</p>
<pre><code class="language-r" lang="r"><span style="color: #0000ff;">spineplot</span>(tension ~ breaks, data = warpbreaks)
</code></pre>
<div id="attachment_39518" style="width: 510px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-39518" class="wp-image-39518" src="https://www.dataanalytics.org.uk/wp-content/uploads/2022/06/spine-warp-300x300.png" alt="" width="500" height="500" srcset="https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-warp-300x300.png 300w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-warp-150x150.png 150w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-warp-768x768.png 768w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-warp-600x600.png 600w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-warp-100x100.png 100w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-warp.png 900w" sizes="(max-width: 500px) 100vw, 500px" /><p id="caption-attachment-39518" class="wp-caption-text">A spinogram is a form of histogram</p></div>
<p>If you have <code>numeric</code> data you can use <code>factor()</code> to convert the data:</p>
<pre><code class="language-r" lang="r"># Use factor(x) to "convert" numeric
<span style="color: #0000ff;">spineplot</span>(<span style="color: #0000ff;">factor</span>(Month) ~ Ozone,
          data = airquality,
          col = <span style="color: #0000ff;">heat.colors</span>(5))
</code></pre>
<div id="attachment_39512" style="width: 510px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-39512" class="wp-image-39512" src="https://www.dataanalytics.org.uk/wp-content/uploads/2022/06/spine-air-300x300.png" alt="" width="500" height="500" srcset="https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-air-300x300.png 300w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-air-150x150.png 150w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-air-768x768.png 768w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-air-600x600.png 600w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-air-100x100.png 100w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-air.png 900w" sizes="(max-width: 500px) 100vw, 500px" /><p id="caption-attachment-39512" class="wp-caption-text">A spinogram where numeric data are converted to a factor before plotting</p></div>
<p>caption: : A spinogram where numeric data are converted to a factor before plotting</p>
<p>Use the <code>breaks</code> argument as you would for <code>hist()</code> to change the breakpoints (you can enter a single <code>integer</code> or a <code>numeric vector</code>).</p>
<pre><code class="language-r" lang="r"><span style="color: #0000ff;">spineplot</span>(feed ~ weight, data = chickwts, breaks = 4)
</code></pre>
<div id="attachment_39513" style="width: 510px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-39513" class="wp-image-39513" src="https://www.dataanalytics.org.uk/wp-content/uploads/2022/06/spine-chick-300x300.png" alt="" width="500" height="500" srcset="https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-chick-300x300.png 300w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-chick-150x150.png 150w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-chick-768x768.png 768w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-chick-600x600.png 600w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-chick-100x100.png 100w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/spine-chick.png 900w" sizes="(max-width: 500px) 100vw, 500px" /><p id="caption-attachment-39513" class="wp-caption-text">Using the breaks argument to alter the breakpoints in a spinogram</p></div>
<p>It can be tricky to read a spinogram and it is not trivial to add a legend for the colors. See <a href="https://www.dataanalytics.org.uk/legends-on-graphs-and-charts/">Tips and Tricks article about legends here</a>.</p>
<ul>
<li>This article is partly in support of my book <em>An Introduction to R</em> see the <a href="https://www.dataanalytics.org.uk/publications">publications page</a> for more information.</li>
<li>An Introduction to R will be published by <a href="https://pelagicpublishing.com/">Pelagic Publishing</a>. <a href="https://pelagicpublishing.com/pages/search-results-page?q=Mark%20Gardener">See all my books at Pelagic Publishing</a>.</li>
<li>For more articles visit the <a href="https://www.dataanalytics.org.uk/category/tips-and-tricks/">Tips and Tricks page</a> and look for the various categories or use the search box.</li>
<li>Visit our other site at <a href="https://www.gardenersown.co.uk/">GardenersOwn</a> for a more ecological matters.</li>
</ul>
<hr />
<p>This article is partly in support of my book <em>An Introduction to R</em> see the <a href="https://www.dataanalytics.org.uk/publications">publications page</a> for more information.</p>
<p>The post <a href="https://dataanalytics.org.uk/spine-plots-using-r/">Spine plots using R</a> appeared first on <a href="https://dataanalytics.org.uk">Data Analytics</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Drawing mathematical curves</title>
		<link>https://dataanalytics.org.uk/drawing-mathematical-curves/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=drawing-mathematical-curves</link>
		
		<dc:creator><![CDATA[gmark]]></dc:creator>
		<pubDate>Thu, 28 Jul 2022 09:48:42 +0000</pubDate>
				<category><![CDATA[Introduction-to-R]]></category>
		<category><![CDATA[curve]]></category>
		<category><![CDATA[data visualisation]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[math functions]]></category>
		<category><![CDATA[plot]]></category>
		<category><![CDATA[plotting]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[visualisation]]></category>
		<category><![CDATA[visualization]]></category>
		<guid isPermaLink="false">https://www.dataanalytics.org.uk/?p=39424</guid>

					<description><![CDATA[<p>Drawing mathematical curves using R is fairly easy. Here&#8217;s how to plot mathematical functions using R functions curve and plot. The main functions are [&#8230;]</p>
<p>The post <a href="https://dataanalytics.org.uk/drawing-mathematical-curves/">Drawing mathematical curves</a> appeared first on <a href="https://dataanalytics.org.uk">Data Analytics</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Drawing mathematical curves using R is fairly easy. Here&#8217;s how to plot mathematical functions using R functions curve and plot.</p>
<p>The main functions are <code>curve()</code> and <code>plot.function()</code> but you can simply use <code>plot()</code>.</p>
<pre><span style="color: #0000ff;">curve</span>(expr, from = NULL, to = NULL, n = 101, add = FALSE,
     type = "l", xname = "x", xlab = xname, ylab = NULL,
     log = NULL, xlim = NULL, ...)

<span style="color: #0000ff;">plot</span>(x, y = 0, to = 1, from = y, xlim = NULL, ylab = NULL, ...)</pre>
<p>Essentially you use (or make) a <code>function</code> that takes values of <code>x</code> and returns a single value. The arguments are largely self-explanatory but:</p>
<ul>
<li><code>expr</code>, <code>x</code> &#8212; an expression or <code>function</code> that returns a single result.</li>
<li><code>from</code>, <code>to</code> &#8212; the limits of the input (default <code>0</code>&#8211;<code>1</code>).</li>
<li><code>n</code> &#8212; the number of &#8220;points&#8221; to draw (these will be evenly spaced between <code>from</code> and <code>to</code>).</li>
<li><code>...</code> &#8212; regular graphical arguments can be used.</li>
</ul>
<h2>Simple Math and Trigonometry</h2>
<p>You can visualize built-in functions. <strong>Note</strong> that you can use regular graphics arguments to augment the basic plot.</p>
<p>Here is a plot of the <code>sqrt</code> function:</p>
<pre><span style="color: #0000ff;">curve</span>(sqrt, from = 0, to = 100, ylab = "Square Root", las = 1)</pre>
<div id="attachment_39482" style="width: 510px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-39482" class="wp-image-39482" src="https://www.dataanalytics.org.uk/wp-content/uploads/2022/06/crv-sqrt.png" alt="" width="500" height="500" srcset="https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-sqrt.png 900w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-sqrt-300x300.png 300w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-sqrt-150x150.png 150w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-sqrt-768x768.png 768w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-sqrt-600x600.png 600w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-sqrt-100x100.png 100w" sizes="(max-width: 500px) 100vw, 500px" /><p id="caption-attachment-39482" class="wp-caption-text">Plot of the square root function sqrt()</p></div>
<p>Here is a simple <code>log</code> plot:</p>
<pre><span style="color: #0000ff;">curve</span>(log, from = 0, to = 100, las = 1, lwd = 2, col = "blue")
</pre>
<div id="attachment_39479" style="width: 510px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-39479" class="wp-image-39479" src="https://www.dataanalytics.org.uk/wp-content/uploads/2022/06/crv-log-300x300.png" alt="" width="500" height="500" srcset="https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-log-300x300.png 300w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-log-150x150.png 150w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-log-768x768.png 768w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-log-600x600.png 600w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-log-100x100.png 100w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-log.png 900w" sizes="(max-width: 500px) 100vw, 500px" /><p id="caption-attachment-39479" class="wp-caption-text">Plot of log function using curve()</p></div>
<h3>Adding to plots</h3>
<p>Use the <code>add = TRUE</code> argument to add a <code>curve()</code> to an existing plot.</p>
<pre><span style="color: #0000ff;">curve</span>(sin, -pi*2, pi*2, lty = 2, lwd = 1.5, col = "blue",
      ylab = "Function", ylim = c(-1,1.5))

<span style="color: #0000ff;">curve</span>(cos, -pi*2, pi*2, lty = 3, col = "red", lwd = 2, add = TRUE)

<span style="color: #808080;"># Add legend and title</span>
<span style="color: #0000ff;">legend</span>(x = "topright", legend = c("Sine", "Cosine"),
      lty = c(2, 3), lwd = c(1.5, 2),
      col = c("blue", "red"), bty = "n")

<span style="color: #0000ff;">title</span>(main = "Sine and Cosine functions")</pre>
<div id="attachment_39484" style="width: 510px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-39484" class="wp-image-39484" src="https://www.dataanalytics.org.uk/wp-content/uploads/2022/06/crv-trig-300x300.png" alt="" width="500" height="500" srcset="https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-trig-300x300.png 300w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-trig-150x150.png 150w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-trig-768x768.png 768w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-trig-600x600.png 600w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-trig-100x100.png 100w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-trig.png 900w" sizes="(max-width: 500px) 100vw, 500px" /><p id="caption-attachment-39484" class="wp-caption-text">Plot of functions sin and cos using curve()</p></div>
<h2>Custom functions</h2>
<p>You can define your own <code>function</code> to plot. <strong>Remember</strong> that the result should be a single value. In this example we define two functions to convert between Celsius and Fahrenheit:</p>
<pre><span style="color: #808080;"># Conversion of temperature</span>
cels &lt;- <span style="color: #0000ff;">function</span>(x) (x-32) * 5/9
fahr &lt;- <span style="color: #0000ff;">function</span>(x) x*9/5 + 32</pre>
<p>Now you can use <code>from</code> and <code>to</code> arguments to set the limits for the input (the default is <code>0</code>&#8211;<code>1</code>).</p>
<pre><span style="color: #0000ff;">curve</span>(cels, from = 32, to = 100, xname = "Farenheit",
      ylab = "Celsius", las = 1)

<span style="color: #0000ff;">curve</span>(fahr, from = 0, to = 50, xname = "Celsius",
      ylab = "Fahrenheit", las = 1)</pre>
<div id="attachment_39483" style="width: 610px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-39483" class="wp-image-39483" src="https://www.dataanalytics.org.uk/wp-content/uploads/2022/06/crv-temperature-300x150.png" alt="" width="600" height="300" srcset="https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-temperature-300x150.png 300w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-temperature-1024x512.png 1024w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-temperature-768x384.png 768w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-temperature-1536x768.png 1536w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-temperature-600x300.png 600w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-temperature.png 1800w" sizes="(max-width: 600px) 100vw, 600px" /><p id="caption-attachment-39483" class="wp-caption-text">Plots using custom function, temperature conversion</p></div>
<h2>Function arguments</h2>
<p>If your function requires additional arguments you need to do something different. In this example you can see the Manning equation, which is used to estimate speed of fluids in pipes/tubes:</p>
<pre>manning &lt;- <span style="color: #0000ff;">function</span>(r, g, c = 0.1) (r^(2/3) * g^0.5/c)

curve(manning) <span style="color: #808080;"># fails</span></pre>
<pre>Error in manning(x) : argument "g" is missing, with no default</pre>
<p>The plotting fails. You need to pre-define all arguments as you cannot &#8220;pass-through&#8221; additional arguments to your function:</p>
<pre>manning &lt;- <span style="color: #0000ff;">function</span>(r, g = 0.01, c = 0.1) (r^(2/3) * g^0.5/c)

<span style="color: #0000ff;">curve</span>(manning) <span style="color: #808080;"># works</span></pre>
<div id="attachment_39480" style="width: 510px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-39480" class="wp-image-39480" src="https://www.dataanalytics.org.uk/wp-content/uploads/2022/06/crv-manning-300x300.png" alt="" width="500" height="500" srcset="https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-manning-300x300.png 300w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-manning-150x150.png 150w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-manning-768x768.png 768w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-manning-600x600.png 600w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-manning-100x100.png 100w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-manning.png 900w" sizes="(max-width: 500px) 100vw, 500px" /><p id="caption-attachment-39480" class="wp-caption-text">Plot of a custom function with parameters</p></div>
<p>In the following example you see a built-in <code>function</code> <code>pt()</code> used to visualize the Student&#8217;s t distribution.</p>
<pre><span style="color: #808080;"># pt needs df and lower.tail arguments</span>
PT &lt;- <span style="color: #0000ff;">function</span>(x) pt(q = x, df = 100, lower.tail = FALSE)

<span style="color: #0000ff;">curve</span>(PT, from = -3, to = 3, las = 1, xname = "t-value",
     n = 20, type = "o", pch = 16, ylab = "probability")</pre>
<div id="attachment_39494" style="width: 510px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-39494" class="wp-image-39494" src="https://www.dataanalytics.org.uk/wp-content/uploads/2022/06/crv-pt-300x300.png" alt="" width="500" height="500" srcset="https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-pt-300x300.png 300w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-pt-150x150.png 150w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-pt-768x768.png 768w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-pt-600x600.png 600w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-pt-100x100.png 100w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/crv-pt.png 900w" sizes="(max-width: 500px) 100vw, 500px" /><p id="caption-attachment-39494" class="wp-caption-text">Plot of Student&#8217;s t distribution using a function &#8220;wrapper&#8221;</p></div>
<p>The workaround is to create a &#8220;wrapper&#8221; <code>function</code> that calls the actual <code>function</code> you want with the appropriate arguments. Note that in this example the <code>n</code> argument was used to plot 20 points, along with <code>type</code> and <code>pch</code> to create a line with over-plotted points.</p>
<p>This post is part of the support for the new book <em>An Introduction to R</em>. See <a href="https://www.dataanalytics.org.uk/publications/">Publications home page</a> for more details.</p>
<ul>
<li>An Introduction to R will be published by <a href="https://pelagicpublishing.com/">Pelagic Publishing</a>. <a href="https://pelagicpublishing.com/pages/search-results-page?q=Mark%20Gardener">See all my books at Pelagic Publishing</a>.</li>
<li>For more articles visit the <a href="https://www.dataanalytics.org.uk/category/tips-and-tricks/">Tips and Tricks page</a> and look for the various categories or use the search box.</li>
<li>See also the <a>Knowledge Base</a> where there are other topics related to R and data science.</li>
<li>Look at our other books on the <a>Publications Page</a>.</li>
<li>Visit our other site at <a href="https://www.gardenersown.co.uk/">GardenersOwn</a> for a more ecological matters.</li>
</ul>
<p>The post <a href="https://dataanalytics.org.uk/drawing-mathematical-curves/">Drawing mathematical curves</a> appeared first on <a href="https://dataanalytics.org.uk">Data Analytics</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
