{"id":501,"date":"2015-07-31T17:16:53","date_gmt":"2015-07-31T16:16:53","guid":{"rendered":"http:\/\/pcool.dyndns.org:8080\/statsbook\/?page_id=501"},"modified":"2025-07-01T09:52:03","modified_gmt":"2025-07-01T08:52:03","slug":"box-plot","status":"publish","type":"page","link":"https:\/\/pcool.dyndns.org\/index.php\/box-plot\/","title":{"rendered":"Box Plot"},"content":{"rendered":"\n<p>Box and whisker plots, or just box plots are used for the presentation of continuous data, that can be grouped in categories. The box represent the interquartile range and the horizontal line within it the median value. The whiskers represent the upper and lower adjacent values that lie within 1.5 times the interquartile range (although the definition may very between software programs). Any value outside the whiskers is marked separately as an &#8216;outlier&#8217;.<\/p>\n\n\n\n<p>Download and open the <a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/data_files\/plotbox.rda\" target=\"_blank\" rel=\"noreferrer noopener\">plotbox.rda<\/a> dataset for this example. The data set contains the maximum flexion in 20 patients before and after a manipulation under anaesthesia. The data can be shown by:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#f80505\" class=\"has-inline-color\">plotbox\n<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0510f7\" class=\"has-inline-color\">   Pre Post\n1   94  115\n2   95  103\n3   89  113\n4   89  122\n5  101  103\n6   76   93\n7  102  118\n8  104  102\n9  103   81\n10  93  102\n11 101  124\n12  92  130\n13  77  103\n14  95  125\n15  89  108\n16  95  106\n17  92  105\n18  84   89\n19  83  113\n20  86   86<\/mark><\/em><\/code><\/pre>\n\n\n\n<p>To create a box and whisker plot of the pre-mua data:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><span style=\"color: #ff0000;\">pre &lt;- ggplot(<em><span style=\"color: #ff0000;\">data=plotbox<\/span><\/em>,<\/span><\/em> <em><span style=\"color: #ff0000;\">aes(y=Pre,x='PreMUA')) +<\/span><\/em>\n<em><span style=\"color: #ff0000;\">geom_boxplot()<\/span><\/em>\n<em><span style=\"color: #ff0000;\">pre<\/span><\/em><\/code><\/pre>\n\n\n\n<p>Add a black and white theme, a title and axes labels:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><span style=\"color: #ff0000;\">pre &lt;- pre +<\/span><\/em>\n<em><span style=\"color: #ff0000;\">ggtitle('Preoperative flexion') +<\/span><\/em>\n<em><span style=\"color: #ff0000;\">xlab(label='Status') +<\/span><\/em>\n<em><span style=\"color: #ff0000;\">ylab(label='max flexion &#91;deg]') +<\/span><\/em>\n<em><span style=\"color: #ff0000;\">theme_bw()<\/span><\/em>\n<em><span style=\"color: #ff0000;\">pre<\/span><\/em><\/code><\/pre>\n\n\n\n<p>To create a similar box and whisker plot of the post-mua flexion:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\"><em>post &lt;- ggplot(<span style=\"color: #ff0000;\"><em>data=plotbox, aes(y=Post,x='PostMUA')<\/em><\/span>) +<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>geom_boxplot() +<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>ggtitle('Postoperative flexion') +<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>xlab(label='Status') +<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>ylab(label='max flexion &#91;deg]') +<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>theme_bw()<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>post<\/em><\/span><\/code><\/pre>\n\n\n\n<p>The preMUA plot looks symmetrical with the median in the middle of the box. However, the postMUA plot is clearly skewed as the median is located in the lower part of the box (right skewed, because the tail is to the &#8216;right&#8217; or higher values). This is also reflected in the descriptive statistics:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#f60606\" class=\"has-inline-color\">summary(plotbox$Pre)\n<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0518f5\" class=\"has-inline-color\">   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. \n  76.00   88.25   92.50   92.00   96.50  104.00 <\/mark><mark style=\"background-color:rgba(0, 0, 0, 0);color:#f60606\" class=\"has-inline-color\">\nsummary(plotbox$Post)\n<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0);color:#1305f5\" class=\"has-inline-color\">   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. \n   81.0   102.0   105.5   107.0   115.8   130.0 <\/mark><\/em><\/code><\/pre>\n\n\n\n<p>The mean and median are approximately the same in preMUA suggesting the data might be Normally distributed. However, postMUA this does not seem likely.<\/p>\n\n\n\n<p>It would be nice to show both plots in one:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\">both &lt;- ggplot(<span style=\"color: #ff0000;\">data=plotbox<\/span>) + <\/span>\n<span style=\"color: #ff0000;\">geom_boxplot(<span style=\"color: #ff0000;\"><span style=\"color: #ff0000;\">aes(y=Pre,x='Pre MUA')<\/span><\/span>) +<\/span>\n<span style=\"color: #ff0000;\">geom_boxplot(aes(y=Post,x='Post MUA')) +<\/span>\n<span style=\"color: #ff0000;\">ggtitle('Flexion before and after MUA')<\/span> <span style=\"color: #ff0000;\">+<\/span>\n<span style=\"color: #ff0000;\">xlab(label='Status') + <\/span>\n<span style=\"color: #ff0000;\">ylab(label='max flexion &#91;deg]')+<\/span>\n<span style=\"color: #ff0000;\">theme_bw()<\/span>\n<span style=\"color: #ff0000;\">both<\/span><\/code><\/pre>\n\n\n\n<p>This will show the plot with the x-axis in <strong>alphabetical order<\/strong>. The easiest way to <strong>change this is by regrouping the data<\/strong> into <a href=\"https:\/\/vita.had.co.nz\/papers\/tidy-data.pdf\" data-type=\"link\" data-id=\"https:\/\/vita.had.co.nz\/papers\/tidy-data.pdf\" target=\"_blank\" rel=\"noreferrer noopener\">tidy format<\/a>. Create a data-frame called &#8216;muapre&#8217; for all preoperative data; the first collumn is called &#8216;flexion&#8217; and the second &#8216;group&#8217; (which are all &#8220;Pre&#8221;).\u00a0 Similarly, a &#8216;muapost&#8217; data-frame is created for all postoperative data with the same column names:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\"><em>muapre&lt;-data.frame(flexion=plotbox$Pre, group='Pre')<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>muapre<\/em><\/span>\n<em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#210cf1\" class=\"has-inline-color\">   flexion group\n1       94   Pre\n2       95   Pre\n.....<\/mark>\n.....<mark style=\"background-color:rgba(0, 0, 0, 0);color:#210cf1\" class=\"has-inline-color\">\n19      83   Pre\n20      86   Pre<\/mark><\/em>\n<em><span style=\"color: #ff0000;\">muapost&lt;-data.frame(flexion=plotbox$Post, group='Post')<\/span><\/em>\n<em><span style=\"color: #ff0000;\">muapost<\/span><\/em>\n<em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#1204f9\" class=\"has-inline-color\">   flexion group\n1      115  Post\n2      103  Post\n.....\n.....\n19     113  Post\n20      86  Post<\/mark><\/em><\/code><\/pre>\n\n\n\n<p>Now, bind the rows (rbind) together to create a new data-frame called mua:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em><span style=\"color: #ff0000;\">mua&lt;-rbind(muapre,muapost)<\/span><\/em>\n<em><span style=\"color: #ff0000;\">mua<\/span><\/em>\n<mark style=\"background-color:rgba(0, 0, 0, 0);color:#0507f1\" class=\"has-inline-color\"><em>   flexion group\n1       94   Pre\n2       95   Pre\n.....\n.....\n39     113  Post\n40      86  Post<\/em><\/mark><\/code><\/pre>\n\n\n\n<p>Now the &#8216;mua&#8217; data-frame can be used to create a grouped box and whisker plot:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><span style=\"color: #ff0000;\">muaplot &lt;- ggplot(<em><span style=\"color: #ff0000;\">data=mua,<\/span><\/em> <em><span style=\"color: #ff0000;\">aes(y = flexion, x = group)<\/span><\/em>) + <\/span><\/em>\n<em><span style=\"color: #ff0000;\">stat_boxplot() + <\/span><\/em>\n<em><span style=\"color: #ff0000;\">ggtitle(label='Flexion Following MUA') + <\/span><\/em>\n<em><span style=\"color: #ff0000;\">ylab(label='Flexion &#91;deg]') +<\/span><\/em>\n<em><span style=\"color: #ff0000;\">theme_bw()<\/span><\/em>\n<em><span style=\"color: #ff0000;\">muaplot<\/span><\/em><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" src=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/plotmua-1024x768.png\" alt=\"\" class=\"wp-image-3438\" srcset=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/plotmua-1024x768.png 1024w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/plotmua-300x225.png 300w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/plotmua-768x576.png 768w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/plotmua.png 1355w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Box and whisker plots, or just box plots are used for the presentation of continuous data, that can be grouped in categories. The box represent the interquartile range and the horizontal line within it the median value. The whiskers represent the upper and lower adjacent values that lie within 1.5 times the interquartile range (although [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"inline_featured_image":false,"footnotes":""},"class_list":["post-501","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/501","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/comments?post=501"}],"version-history":[{"count":8,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/501\/revisions"}],"predecessor-version":[{"id":4667,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/501\/revisions\/4667"}],"wp:attachment":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/media?parent=501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}