<?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>curve Archives - Data Analytics</title>
	<atom:link href="https://dataanalytics.org.uk/tag/curve/feed/" rel="self" type="application/rss+xml" />
	<link>https://dataanalytics.org.uk/tag/curve/</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>curve Archives - Data Analytics</title>
	<link>https://dataanalytics.org.uk/tag/curve/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<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 fetchpriority="high" 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 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 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>
