<?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>chi squared Archives - Data Analytics</title>
	<atom:link href="https://dataanalytics.org.uk/tag/chi-squared/feed/" rel="self" type="application/rss+xml" />
	<link>https://dataanalytics.org.uk/tag/chi-squared/</link>
	<description>Understanding Data</description>
	<lastBuildDate>Thu, 28 Jul 2022 15:43:52 +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>chi squared Archives - Data Analytics</title>
	<link>https://dataanalytics.org.uk/tag/chi-squared/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Association Plots in R</title>
		<link>https://dataanalytics.org.uk/association-plots-in-r/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=association-plots-in-r</link>
		
		<dc:creator><![CDATA[gmark]]></dc:creator>
		<pubDate>Thu, 28 Jul 2022 15:43:52 +0000</pubDate>
				<category><![CDATA[Introduction-to-R]]></category>
		<category><![CDATA[association]]></category>
		<category><![CDATA[association plot]]></category>
		<category><![CDATA[assocplot]]></category>
		<category><![CDATA[chi squared]]></category>
		<category><![CDATA[data visualisation]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[plot]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[visualisation]]></category>
		<category><![CDATA[visualization]]></category>
		<guid isPermaLink="false">https://www.dataanalytics.org.uk/?p=39538</guid>

					<description><![CDATA[<p>Association Plots Association plots in R. An association plot draws the results of an association test by charting the Pearson Residuals. Association plots in [&#8230;]</p>
<p>The post <a href="https://dataanalytics.org.uk/association-plots-in-r/">Association Plots in R</a> appeared first on <a href="https://dataanalytics.org.uk">Data Analytics</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1>Association Plots</h1>
<p>Association plots in R. An association plot draws the results of an association test by charting the Pearson Residuals.</p>
<p>Association plots in R are drawn using <code>assocplot()</code></p>
<pre><code class="language-r" lang="r"><span style="color: #0000ff;">assocplot</span>(x, col = c("black", "red"), space = 0.3,
          main = NULL, xlab = NULL, ylab = NULL)
</code></pre>
<figure>
<table>
<thead>
<tr>
<th>Parameter</th>
<th>Explanation</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>x</code></td>
<td>the data, usually a <code>numeric</code> <code>matrix</code>.</td>
</tr>
<tr>
<td><code>col</code></td>
<td>colors for positive and negative associations.</td>
</tr>
<tr>
<td><code>space</code></td>
<td>amount of space between the bars, as a fraction of average bar height and width (default = <code>0.3</code>).</td>
</tr>
<tr>
<td><code>main</code>, <code>xlab</code>, <code>ylab</code></td>
<td>title annotations.</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p>Essentially you need a 2-dimensional <code>matrix</code> to use <code>assocplot()</code>:</figure>
<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>
<p>Apart from the titles, the only graphical parameter you can alter directly is <code>col</code>, to alter the positive and negative bar colors:</p>
<pre><code class="language-r" lang="r"><span style="color: #0000ff;">assocplot</span>(VADeaths, col = c(<span style="color: #008000;">"lightblue"</span>, <span style="color: #008000;">"pink"</span>),
	  xlab = <span style="color: #008000;">"Age class"</span>, ylab = <span style="color: #008000;">"Driver actegory"</span>)
</code></pre>
<div id="attachment_39537" style="width: 510px" class="wp-caption aligncenter"><img fetchpriority="high" decoding="async" aria-describedby="caption-attachment-39537" class="wp-image-39537" src="https://www.dataanalytics.org.uk/wp-content/uploads/2022/06/assocplot-warp-300x300.png" alt="" width="500" height="500" srcset="https://dataanalytics.org.uk/wp-content/uploads/2022/06/assocplot-warp-300x300.png 300w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/assocplot-warp-150x150.png 150w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/assocplot-warp-768x768.png 768w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/assocplot-warp-600x600.png 600w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/assocplot-warp-100x100.png 100w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/assocplot-warp.png 900w" sizes="(max-width: 500px) 100vw, 500px" /><p id="caption-attachment-39537" class="wp-caption-text">Basic association plot using custom color for positive and negative bars</p></div>
<h2>Graphical parameters</h2>
<p>If you want to alter the general appearance of your association plot you&#8217;ll need to set the appropriate graphical parameters using <code>par()</code> before using <code>assocplot()</code>:</p>
<pre><code class="language-r" lang="r">opar &lt;- <span style="color: #0000ff;">par</span>(las = 1, cex = 0.8, mar = c(5,7,2,1))

<span style="color: #0000ff;">assocplot</span>(VADeaths, col = c(<span style="color: #008000;">"blue"</span>, <span style="color: #008000;">"tomato"</span>),
          space = 0.05, xlab = <span style="color: #008000;">"Age class"</span>)
<span style="color: #0000ff;">title</span>(ylab = <span style="color: #008000;">"Driver category"</span>, line = 6)

<span style="color: #0000ff;">par</span>(opar)
</code></pre>
<div id="attachment_39536" style="width: 510px" class="wp-caption aligncenter"><img decoding="async" aria-describedby="caption-attachment-39536" class="wp-image-39536" src="https://www.dataanalytics.org.uk/wp-content/uploads/2022/06/assocplot-par-300x300.png" alt="" width="500" height="500" srcset="https://dataanalytics.org.uk/wp-content/uploads/2022/06/assocplot-par-300x300.png 300w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/assocplot-par-150x150.png 150w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/assocplot-par-768x768.png 768w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/assocplot-par-600x600.png 600w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/assocplot-par-100x100.png 100w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/assocplot-par.png 900w" sizes="(max-width: 500px) 100vw, 500px" /><p id="caption-attachment-39536" class="wp-caption-text">Custom graphical parameters have to be applied using par() before using assocplot()</p></div>
<p>In the preceding example the margins were widened to allow the labels to &#8220;fit&#8221;. Note also how <code>title()</code> was used to place the y-axis annotation on an outer line.</p>
<h2>Data layout</h2>
<p>Essentially you need a 2D <code>matrix</code> for <code>assocplot()</code> to make an association plot in R. If you have something else you need to coerce it to the correct form.</p>
<p>Here are some options:</p>
<ul>
<li><code>data.frame</code> use <code>as.matrix()</code> to alter the form.</li>
<li><code>table</code> use <code>x[r, c, n, ...]</code> to &#8220;pick out&#8221; the appropriate 2D sub-table or..</li>
<li><code>table</code> use <code>margin.table</code> to &#8220;collapse&#8221; a table and combine across the margins you want.</li>
</ul>
<pre><code class="language-r" lang="r"><span style="color: #808080;"># 3D table</span>
HairEyeColor
</code></pre>
<pre><code>, , Sex = Male

       Eye
Hair    Brown Blue Hazel Green
  Black    32   11    10     3
  Brown    53   50    25    15
  Red      10   10     7     7
  Blond     3   30     5     8

, , Sex = Female

       Eye
Hair    Brown Blue Hazel Green
  Black    36    9     5     2
  Brown    66   34    29    14
  Red      16    7     7     7
  Blond     4   64     5     8
</code></pre>
<pre><code class="language-r" lang="r"><span style="color: #808080;"># Choose "Male"</span>
HairEyeColor[,,1]
</code></pre>
<pre><code>       Eye
Hair    Brown Blue Hazel Green
  Black    32   11    10     3
  Brown    53   50    25    15
  Red      10   10     7     7
  Blond     3   30     5     8
</code></pre>
<pre><code class="language-r" lang="r"><span style="color: #808080;"># Combine "Male" and "Female"</span>
<span style="color: #0000ff;">margin.table</span>(HairEyeColor, margin = c(1,2))
</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: #808080;"># Combine "Eye"</span>
<span style="color: #0000ff;">margin.table</span>(HairEyeColor, margin = c(1,3))
</code></pre>
<pre><code>       Sex
Hair    Male Female
  Black   56     52
  Brown  143    143
  Red     34     37
  Blond   46     81
</code></pre>
<h2>Alternatives to assocplot()</h2>
<p>The <code>assocplot()</code> function is not the only was to draw an association plot using R. You could run a <code>chisq.test()</code> and extract the Pearson residuals <code>$residuals</code>, which you then plot using <code>barplot()</code>.</p>
<pre><code class="language-r" lang="r">X &lt;- <span style="color: #0000ff;">chisq.test</span>(VADeaths)
X$residuals
</code></pre>
<pre><code>         Rural Male Rural Female Urban Male Urban Female
50-54 -0.0001229145  -0.09956533  0.2454344  -0.21106734
55-59  0.0422284686  -0.56107962  0.4550546  -0.06391943
60-64 -0.0951496863  -0.16808112  0.5368919  -0.40335827
65-69 -0.2718462679  -0.34870589  0.2349807   0.36003546
70-74  0.2624133483   0.73510055 -0.8898149   0.09370444
</code></pre>
<pre><code class="language-r" lang="r"><span style="color: #0000ff;">barplot</span>(X$residuals, beside = TRUE, col = <span style="color: #0000ff;">cm.colors</span>(5),
        ylim = c(-1,1), legend = TRUE,
	args.legend = <span style="color: #0000ff;">list</span>(x = "top", bty = "n", ncol = 5))
<span style="color: #0000ff;">title</span>(ylab = "Pearson residuals", xlab = "Category")
</code></pre>
<div id="attachment_39535" style="width: 510px" class="wp-caption aligncenter"><img decoding="async" aria-describedby="caption-attachment-39535" class="wp-image-39535" src="https://www.dataanalytics.org.uk/wp-content/uploads/2022/06/assocplot-bar-300x300.png" alt="" width="500" height="500" srcset="https://dataanalytics.org.uk/wp-content/uploads/2022/06/assocplot-bar-300x300.png 300w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/assocplot-bar-150x150.png 150w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/assocplot-bar-768x768.png 768w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/assocplot-bar-600x600.png 600w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/assocplot-bar-100x100.png 100w, https://dataanalytics.org.uk/wp-content/uploads/2022/06/assocplot-bar.png 900w" sizes="(max-width: 500px) 100vw, 500px" /><p id="caption-attachment-39535" class="wp-caption-text">Alternative to assocplot() is to use barplot() on the Pearson residuals</p></div>
<p>To get multiple rows, with a separate mini-plot for each row you would need to set-up <code>par(mfrow = c(rows, cols))</code>.</p>
<p>There are potential advantages to this method, for example you can add horizontal lines at +/- 2 to show the &#8220;significance band&#8221;. However, it is also somewhat more involved!</p>
<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>
<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 look at 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 href="#https://www.dataanalytics.org.uk/data-analytics-knowledge-base-tips-tricks-r-excel/">Knowledge Base</a> where there are other topics related to R and data science.</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/association-plots-in-r/">Association Plots in R</a> appeared first on <a href="https://dataanalytics.org.uk">Data Analytics</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
