<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/replicating-plots-boxplot-exercises/"></div></div><div><a data-url="https://www.r-bloggers.com/replicating-plots-boxplot-exercises/" data-counturl="https://www.r-bloggers.com/replicating-plots-boxplot-exercises/" data-count="horizontal" data-text="Replicating Plots – Boxplot Exercises" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-exercises.com/2016/09/26/replicating-plots-boxplot-exercises/"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>R’s <code>boxplot</code> function has a lot of useful parameters allowing us to change the behaviour and appearance of the boxplot graphs. In this exercise we will try to use those parameters in order to replicate the visual style of Matlab’s boxplot. Before trying out this exercise please make sure that you are familiar with the following functions: <code>bxp</code>, <code>boxplot</code>, <code>axis</code>, <code>mtext</code></p>
<p>Here is the plot we will be replicating:<br>
<a target="_blank" href="https://i2.wp.com/r-exercises.com/wp-content/uploads/2016/09/matlab_boxplot.png?resize=456%2C342"><img sizes="(max-width: 672px) 100vw, 672px" alt="matlab_boxplot" class="wp-image-2025" src="https://i2.wp.com/r-exercises.com/wp-content/uploads/2016/09/matlab_boxplot.png?resize=456%2C342" data-recalc-dims="1"></a></p>
<p>We will be using the same <strong>iris</strong> dataset which is available in R by default in the variable of the same name – <code>iris</code>. The exercises will require you to make incremental changes to the default boxplot style.</p>
<p>Answers to the exercises are available <a target="_blank" href="http://r-exercises.com/2016/09/26/replicating-plots-boxplot-solutions/">here</a>.</p>
<p>If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.</p>
<p><strong>Exercise 1</strong><br>
Make a default boxplot of Sepal.Width stratified by Species.</p>
<p><strong>Exercise 2</strong><br>
Change the range of the y-axis so it starts at 2 and ends at 4.5.</p>
<p><strong>Exercise 3</strong><br>
Modify the boxplot function so it doesn’t draw ticks nor labels of the x and y axes.</p>
<p><strong>Exercise 4</strong><br>
Add notches (triangular dents around the median representing confidence intervals) to the boxes in the plot.</p>
<p><strong>Exercise 5</strong><br>
Increase the distance between boxes in the plot.</p>
<p><strong>Exercise 6</strong><br>
Change the color of the box borders to blue.</p>
<p><strong>Exercise 7</strong><br>
a. Change the color of the median lines to red.<br>
b. Change the line width of the median line to 1.</p>
<p><strong>Exercise 8</strong><br>
a. Change the color of the outlier points to red.<br>
b. Change the symbol of the outlier points to “+”.<br>
c. Change the size of the outlier points to 0.8.</p>
<p><strong>Exercise 9</strong><br>
a. Add the title to the boxplot (try to replicate the style of matlab’s boxplot).<br>
b. Add the y-axis label to the boxplot (try to replicate the style of matlab’s boxplot).</p>
<p><strong>Exercise 10</strong><br>
a. Add x-axis (try to make it resemble the x-axis in the matlab’s boxplot)<br>
b. Add y-axis (try to make it resemble the y-axis in the matlab’s boxplot)<br>
c. Add the y-axis ticks on the other side.</p>
<p>NOTE: You can use <code>format(as.character(c(2, 4.5)), drop0trailing=TRUE, justify=&quot;right&quot;)</code> to obtain the text for y-axis labels.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/replicating-plots-boxplot-exercises/"></div></div><div><a data-url="https://www.r-bloggers.com/replicating-plots-boxplot-exercises/" data-counturl="https://www.r-bloggers.com/replicating-plots-boxplot-exercises/" data-count="vertical" data-text="Replicating Plots – Boxplot Exercises" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://r-exercises.com/2016/09/26/replicating-plots-boxplot-exercises/"> R-exercises</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/H_n9pGmxcG8"></a></div>