<?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>visual &#8211; Quadtrees</title>
	<atom:link href="http://quadtrees.lu/tag/visual/feed/" rel="self" type="application/rss+xml" />
	<link>http://quadtrees.lu</link>
	<description>Quantitative Urban Analytics and Spatial Data Research - Luxembourg</description>
	<lastBuildDate>Fri, 08 Nov 2019 09:02:49 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.4.5</generator>

<image>
	<url>http://quadtrees.lu/wp-content/uploads/2018/08/logo-150x150.png</url>
	<title>visual &#8211; Quadtrees</title>
	<link>http://quadtrees.lu</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Creating graphs in R with ggplot2</title>
		<link>http://quadtrees.lu/creating-graphs-in-r-with-ggplot2/</link>
		
		<dc:creator><![CDATA[Paul Kilgarriff]]></dc:creator>
		<pubDate>Thu, 07 Nov 2019 15:47:08 +0000</pubDate>
				<category><![CDATA[Quadtrees]]></category>
		<category><![CDATA[ggplot]]></category>
		<category><![CDATA[graphs]]></category>
		<category><![CDATA[r]]></category>
		<category><![CDATA[visual]]></category>
		<guid isPermaLink="false">http://quadtrees.lu/?p=314</guid>

					<description><![CDATA[Learn how to create clean graphs using ggplot in R]]></description>
										<content:encoded><![CDATA[
<p>In this post we will use ggplot to create some nice and clean looking graphs. How to colour points by groups, edit both the x and y axis, clean the legend and change and customise various aspects of the graph.</p>



<p>First of all we load the mtcars data set. Then we want to see what is contained within the data set and also view what type of data each variable is.</p>



<p>The raw code is also available from my github, see:</p>



<p><a href="https://github.com/granger89/SCALEITUP/blob/master/graphs%20in%20ggplot2">https://github.com/granger89/SCALEITUP/blob/master/graphs%20in%20ggplot2</a></p>



<pre class="wp-block-verse"><code>library(ggplot2)<br>cars &lt;- mtcars<br>cars<br>str(cars)</code></pre>



<p>We can see there is no categorical variable. We are going to create one which we will use for plotting we are going to create two; one for the make of car and another for its weight class.</p>



<pre class="wp-block-verse">cars$car_type &lt;- rownames(cars)</pre>



<p>Next we use summary to help us when deciding upon the thresholds.</p>



<pre class="wp-block-verse"><code>summary(cars$wt)<br>cars$wt_class &lt;- NA <br>indx &lt;- cars$wt&lt;2.5 <br>cars[indx, "wt_class"] &lt;- "light"<br>indx &lt;- cars$wt&gt;=2.5 &amp; cars$wt&lt;3.4<br>cars[indx, "wt_class"] &lt;- "medium"<br>indx &lt;- cars$wt&gt;=3.4<br>cars[indx, "wt_class"] &lt;- "heavy"</code></pre>



<p>We now have the data frame in the correct format we want for plotting</p>



<pre class="wp-block-verse"><code>str(cars)</code></pre>



<p>The following code gives us a simple plot of two variables mpg (x-axis) and hp (y-axis) using the dataset cars.</p>



<pre class="wp-block-verse"><code>ggplot(cars) +<br>geom_point(aes(mpg, hp))</code></pre>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="656" height="602" src="http://quadtrees.lu/wp-content/uploads/2019/11/Rplot1.png" alt="" class="wp-image-317" srcset="http://quadtrees.lu/wp-content/uploads/2019/11/Rplot1.png 656w, http://quadtrees.lu/wp-content/uploads/2019/11/Rplot1-300x275.png 300w, http://quadtrees.lu/wp-content/uploads/2019/11/Rplot1-450x413.png 450w" sizes="(max-width: 656px) 100vw, 656px" /></figure>



<p>We now introduce a third variable. We can change the point shapes using a categorical variable. For this we use the weight class variable we created earlier.</p>



<pre class="wp-block-verse"><code>ggplot(cars, aes(x=mpg, y=hp, group=wt_class))+<br>geom_point(aes(shape=wt_class), size=2, alpha=1)</code></pre>



<figure class="wp-block-image size-large"><img decoding="async" width="656" height="602" src="http://quadtrees.lu/wp-content/uploads/2019/11/Rplot2.png" alt="" class="wp-image-320" srcset="http://quadtrees.lu/wp-content/uploads/2019/11/Rplot2.png 656w, http://quadtrees.lu/wp-content/uploads/2019/11/Rplot2-300x275.png 300w, http://quadtrees.lu/wp-content/uploads/2019/11/Rplot2-450x413.png 450w" sizes="(max-width: 656px) 100vw, 656px" /></figure>



<p>Now we have the graph we next steps involved editing the axes, legend, title and background so that we get a cleaner graph.</p>



<h3 class="wp-block-heading">Editing the axes and background</h3>



<p>Firstly we will import new fonts.</p>



<pre class="wp-block-verse"><code>library(gridExtra)<br>library(extrafont)<br>library(ggthemes) # Load<br>fonts()<br>windowsFonts("Arial" = windowsFont("Arial"))<br>windowsFonts("Times New Roman" = windowsFont("Times New Roman"))<br>windowsFonts("Goudy Stout" = windowsFont("Goudy Stout"))</code></pre>



<p>Give the plot a white background and change the fonts</p>



<pre class="wp-block-verse"><code>ggplot(cars, aes(x=mpg, y=hp, group=wt_class))+<br>geom_point(aes(shape=wt_class), size=2, alpha=1)+<br>theme_bw()+<br>theme(panel.background = element_rect(fill = "white", colour = "white"))+<br>theme(axis.text=element_text(size=11, family="Arial"),axis.title=element_text(size=12,face="bold", family="Arial"))+<br>theme(panel.grid=element_line(color = "grey80"))</code></pre>



<p>Change the x and y axis names and also remove spacing around the origin. We can also change the limits of the x and y axes.</p>



<pre class="wp-block-verse"><code>ggplot(cars, aes(x=mpg, y=hp, group=wt_class))+<br>geom_point(aes(shape=wt_class), size=2, alpha=1)+<br>theme_bw()+<br>theme(panel.background = element_rect(fill = "white", colour = "white"))+<br>theme(axis.text=element_text(size=11, family="Arial"),axis.title=element_text(size=12,face="bold", family="Arial"))+<br>theme(panel.grid=element_line(color = "grey80")) +<br>labs(x = "Miles per Gallon (mpg)", y = "Horse Power (HP)" )+<br>labs(title = "Cars power, weight and fuel economy")+<br>scale_x_continuous(limits = c(0,35), expand = c(0, 0)) +<br>scale_y_continuous(limits = c(0,350), expand = c(0, 0))</code></pre>



<figure class="wp-block-image size-large"><img decoding="async" width="656" height="602" src="http://quadtrees.lu/wp-content/uploads/2019/11/Rplot4.png" alt="" class="wp-image-325" srcset="http://quadtrees.lu/wp-content/uploads/2019/11/Rplot4.png 656w, http://quadtrees.lu/wp-content/uploads/2019/11/Rplot4-300x275.png 300w, http://quadtrees.lu/wp-content/uploads/2019/11/Rplot4-450x413.png 450w" sizes="(max-width: 656px) 100vw, 656px" /></figure>



<h3 class="wp-block-heading">Fixing the legend</h3>



<p>One thing I would like to fix is the legend as it does not look that pleasant. I would like the first letters to be capitals and &#8216;wt_class&#8217; does not explain what the values show.</p>



<pre class="wp-block-verse"><code>scale_shape_manual(values=c(1, 2, 3), name = "Weight Group", breaks=c("heavy","medium","light"), labels=c("Heavy &gt;3.4 Tons", "Medium 2.5-3.4 Tons", "Light &lt;2.5 Tons"))</code></pre>



<p>To explain this code, the values are the type of shapes used to represent the different classes. See the link for what the different values mean. </p>



<p>Next &#8216;name&#8217; is the title of the legend.<br> &#8216;breaks&#8217; are the different classes. Once we know the values we can then put them in whatever order we want.  (&#8220;heavy&#8221;,&#8221;medium&#8221;,&#8221;light&#8221;) or  (&#8220;light&#8221;,&#8221;medium&#8221;,&#8221;heavy&#8221;) . That is the order they appear in the legend.<br> Next we use labels to replace the raw data values with whatever value we want.<br><a href="http://www.sthda.com/english/wiki/ggplot2-point-shapes">http://www.sthda.com/english/wiki/ggplot2-point-shapes</a></p>



<pre class="wp-block-verse"><code>ggplot(cars, aes(x=mpg, y=hp, group=wt_class))+<br>   geom_point(aes(shape=wt_class), size=2, alpha=1)+<br>   theme_bw()+<br>   theme(panel.background = element_rect(fill = "white", colour = "white"))+<br>   theme(axis.text=element_text(size=11, family="Arial"),axis.title=element_text(size=12,face="bold", family="Arial"))+<br>   theme(panel.grid=element_line(color = "grey80")) +<br>   labs(x = "Miles per Gallon (mpg)", y = "Horse Power (HP)" )+<br>   labs(title = "Cars power, weight and fuel economy")+<br>   scale_x_continuous(limits = c(0,35), expand = c(0, 0)) +<br>   scale_y_continuous(limits = c(0,350), expand = c(0, 0))+<br>   scale_shape_manual(values=c(1, 2, 3), name = "Weight Group",<br>                      breaks=c("heavy","medium","light"),<br>                      labels=c("Heavy &gt;3.4 Tons", "Medium 2.5-3.4 Tons", "Light &lt;2.5 Tons"))</code></pre>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="656" height="602" src="http://quadtrees.lu/wp-content/uploads/2019/11/Rplot5.png" alt="" class="wp-image-326" srcset="http://quadtrees.lu/wp-content/uploads/2019/11/Rplot5.png 656w, http://quadtrees.lu/wp-content/uploads/2019/11/Rplot5-300x275.png 300w, http://quadtrees.lu/wp-content/uploads/2019/11/Rplot5-450x413.png 450w" sizes="(max-width: 656px) 100vw, 656px" /></figure>



<p>Finally we can position the legend. Currently it is squeezing the size of the plot area. We are likeyl to have some empty space where we can place it. The position varies on the x and y axes from 0 to 1. So how about we place it at 0.2 on the x-axis and 0.2 on the y-axis. I&#8217;ve also given it a border and made the background white.</p>



<pre class="wp-block-verse"><code>ggplot(cars, aes(x=mpg, y=hp, group=wt_class))+<br>   geom_point(aes(shape=wt_class), size=2, alpha=1)+<br>   theme_bw()+<br>   theme(panel.background = element_rect(fill = "white", colour = "white"))+<br>   theme(axis.text=element_text(size=11, family="Arial"),axis.title=element_text(size=12,face="bold", family="Arial"))+<br>   theme(panel.grid=element_line(color = "grey80")) +<br>   labs(x = "Miles per Gallon (mpg)", y = "Horse Power (HP)" )+<br>   labs(title = "Cars power, weight and fuel economy")+<br>   scale_x_continuous(limits = c(0,35), expand = c(0, 0)) +<br>   scale_y_continuous(limits = c(0,350), expand = c(0, 0))+<br>   scale_shape_manual(values=c(1, 2, 3), name = "Weight Group",<br>                      breaks=c("heavy","medium","light"),<br>                      labels=c("Heavy &gt;3.4 Tons", "Medium 2.5-3.4 Tons", "Light &lt;2.5 Tons"))+<br>   theme(legend.position=c(0.2,0.2))+<br>   theme(legend.text.align = 0)+<br>   theme(legend.title=element_text(size=10))+<br>   theme(legend.text=element_text(size=10))+<br>   theme(legend.background = element_rect(colour = 'black', fill = 'white', size = 1, linetype='solid'))</code></pre>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="656" height="602" src="http://quadtrees.lu/wp-content/uploads/2019/11/Rplot6.png" alt="" class="wp-image-327" srcset="http://quadtrees.lu/wp-content/uploads/2019/11/Rplot6.png 656w, http://quadtrees.lu/wp-content/uploads/2019/11/Rplot6-300x275.png 300w, http://quadtrees.lu/wp-content/uploads/2019/11/Rplot6-450x413.png 450w" sizes="(max-width: 656px) 100vw, 656px" /></figure>



<h3 class="wp-block-heading">Vary size by value of variable</h3>



<p>Finally lets introduce a fourth variable. Instead of all points having a fixed size let us vary the size based on the number of gears.</p>



<pre class="wp-block-verse"><code>ggplot(cars, aes(x=mpg, y=hp, group=wt_class, size=gear))+<br>   geom_point(aes(shape=wt_class), alpha=1)+<br>   theme_bw()+<br>   theme(panel.background = element_rect(fill = "white", colour = "white"))+<br>   theme(axis.text=element_text(size=11, family="Arial"),axis.title=element_text(size=12,face="bold", family="Arial"))+<br>   theme(panel.grid=element_line(color = "grey80")) +<br>   labs(x = "Miles per Gallon (mpg)", y = "Horse Power (HP)" )+<br>   labs(title = "Cars power, weight and fuel economy")+<br>   scale_x_continuous(limits = c(0,35), expand = c(0, 0)) +<br>   scale_y_continuous(limits = c(0,350), expand = c(0, 0))+<br>   scale_shape_manual(values=c(1, 2, 3), name = "Weight Group",<br>                      breaks=c("heavy","medium","light"),<br>                      labels=c("Heavy &gt;3.4 Tons", "Medium 2.5-3.4 Tons", "Light &lt;2.5 Tons"))+<br>   theme(legend.position=c(0.2,0.3))+<br>   theme(legend.text.align = 0)+<br>   theme(legend.title=element_text(size=10))+<br>   theme(legend.text=element_text(size=10))+<br>   theme(legend.background = element_rect(colour = 'black', fill = 'white', size = 1, linetype='solid'))+<br>   scale_size_continuous(range = c(3,5),  <br>                         breaks= c(3,4,5), name="Number of \nGears")</code></pre>



<h3 class="wp-block-heading">Label specific points</h3>



<p>Also I would like to label the outliers at the upper and lower end. For this we use ggrepel</p>



<p><code>library(ggrepel)</code></p>



<pre class="wp-block-verse"><code>ggplot(cars, aes(x=mpg, y=hp, group=wt_class, size=gear))+<br>   geom_point(aes(shape=wt_class), alpha=1)+<br>   theme_bw()+<br>   theme(panel.background = element_rect(fill = "white", colour = "white"))+<br>   theme(axis.text=element_text(size=11, family="Arial"),axis.title=element_text(size=12,face="bold", family="Arial"))+<br>   theme(panel.grid=element_line(color = "grey80")) +<br>   labs(x = "Miles per Gallon (mpg)", y = "Horse Power (HP)" )+<br>   labs(title = "Cars power, weight and fuel economy")+<br>   scale_x_continuous(limits = c(0,35), expand = c(0, 0)) +<br>   scale_y_continuous(limits = c(0,350), expand = c(0, 0))+<br>   scale_shape_manual(values=c(1, 2, 3), name = "Weight Group",<br>                      breaks=c("heavy","medium","light"),<br>                      labels=c("Heavy &gt;3.4 Tons", "Medium 2.5-3.4 Tons", "Light &lt;2.5 Tons"))+   theme(legend.position=c(0.2,0.3))+   theme(legend.text.align = 0)+   theme(legend.title=element_text(size=10))+   theme(legend.text=element_text(size=10))+   theme(legend.background = element_rect(colour = 'black', fill = 'white', size = 1, linetype='solid'))+   scale_size_continuous(range = c(3,5),                           breaks= c(3,4,5), name="Number of \nGears")+   labs(size="Number of Gears")+   geom_text_repel(data = subset(cars, mpg &gt; 25 | mpg &lt;16), size=3,aes(label = car_type))</code></pre>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="656" height="602" src="http://quadtrees.lu/wp-content/uploads/2019/11/Rplot7.png" alt="" class="wp-image-329" srcset="http://quadtrees.lu/wp-content/uploads/2019/11/Rplot7.png 656w, http://quadtrees.lu/wp-content/uploads/2019/11/Rplot7-300x275.png 300w, http://quadtrees.lu/wp-content/uploads/2019/11/Rplot7-450x413.png 450w" sizes="(max-width: 656px) 100vw, 656px" /></figure>



<p>For other customisable options please refer to the ggplot2 documentation which is excellent at explaining how to add further options.</p>



<p><a href="https://cran.r-project.org/web/packages/ggplot2/ggplot2.pdf">https://cran.r-project.org/web/packages/ggplot2/ggplot2.pdf</a></p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
