{"id":3052,"date":"2023-05-07T20:02:10","date_gmt":"2023-05-07T20:02:10","guid":{"rendered":"http:\/\/optimumsportsperformance.com\/blog\/?p=3052"},"modified":"2023-05-07T20:02:10","modified_gmt":"2023-05-07T20:02:10","slug":"displaying-tables-plots-together-part-2-adding-titles-captions","status":"publish","type":"post","link":"https:\/\/optimumsportsperformance.com\/blog\/displaying-tables-plots-together-part-2-adding-titles-captions\/","title":{"rendered":"Displaying Tables &#038; Plots Together Part 2: Adding Titles &#038; Captions"},"content":{"rendered":"<p>Yesterday&#8217;s post about <span style=\"color: #0000ff;\"><strong><a style=\"color: #0000ff;\" href=\"https:\/\/optimumsportsperformance.com\/blog\/displaying-tables-plots-together\/\">creating single page reports with tables and plots in the same display window<\/a><\/strong><\/span> got a lot of follow up questions and comments (which is great!). In particular, <strong><span style=\"color: #0000ff;\"><a style=\"color: #0000ff;\" href=\"https:\/\/twitter.com\/datadrivenAT\">Lyle Danley<\/a><\/span><\/strong> brought up a good point that adding titles to these reports, while important, can sometimes be tricky with these types of displays. So, I decided to do a quick follow up to show how to add titles and captions to your reports (in case you want to point out some key things to your colleagues or the practitioners you are working with).<\/p>\n<p>I&#8217;m going to use the exact same code as yesterday, so check out <strong><span style=\"color: #0000ff;\"><a style=\"color: #0000ff;\" href=\"https:\/\/optimumsportsperformance.com\/blog\/displaying-tables-plots-together\/\">that article<\/a><\/span><\/strong> to see the general approach to building these reports. As always, all of the code for yesterday&#8217;s article and today&#8217;s are on my <strong><span style=\"color: #0000ff;\"><a style=\"color: #0000ff;\" href=\"https:\/\/github.com\/pw2\/R-Tips-Tricks\/blob\/master\/Displaying%20Tables%20%26%20Plots%20Together%20Part%202%20-%20Adding%20Titles%20%26%20Captions.R\">GITHUB page<\/a><\/span><\/strong>.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Review<\/strong><\/span><\/p>\n<p>Recall that yesterday we\u00a0 constructed the below plot using both <strong>ggarrange() <\/strong> and the {<strong>patchwork<\/strong>} package.<a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-06-at-7.34.50-AM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-3049\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-06-at-7.34.50-AM-1024x608.png\" alt=\"\" width=\"625\" height=\"371\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-06-at-7.34.50-AM-1024x608.png 1024w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-06-at-7.34.50-AM-300x178.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-06-at-7.34.50-AM-768x456.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-06-at-7.34.50-AM-624x370.png 624w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><\/p>\n<p>I&#8217;m going to use both approaches and add a title and a bullet point caption box in the bottom right.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Titles &amp; Captions with ggarrange()<\/strong><\/span><\/p>\n<p>I wont rehash all of the code from yesterday, but the <strong>ggarrange() <\/strong>table that we created with was constructed with the following code.<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n## Build table into a nice ggtextable() to visualize it\r\ntbl &lt;- ggtexttable(fit, rows = NULL, theme = ttheme(&quot;blank&quot;)) %&gt;%\r\n  tab_add_hline(at.row = 1:2, row.side = &quot;top&quot;, linewidth = 2) %&gt;%\r\n  tab_add_hline(at.row = 4, row.side = &quot;bottom&quot;, linewidth = 3, linetype = 1)\r\n<\/pre>\n<p>To create a bullet point caption box I need to first create a string of text that I want to display, using the <strong>paste()<\/strong> function. I then wrap this text into the <strong>ggparagraph() <\/strong>function so that it can be appropriately displayed on the plot. Then, similar to yesterday, I use the <strong>ggarrange()<\/strong> function to put the two plots, the table, and the caption box, into a single canvas.<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n## Create text for a caption\r\ntext &lt;- paste(&quot;* It appears that gender and flipper length are important for estimating bill length.&quot;,\r\n              &quot; &quot;,\r\n              &quot;* Males have a bill length that is 2.073 mm greater than females on average.&quot;,\r\n              &quot; &quot;,\r\n              &quot;* Penguins on different islands should be tested to determine how well this model will perform out of sample.&quot;,\r\n              sep = &quot;\\n&quot;)\r\n\r\ntext.p &lt;- ggparagraph(text = text, \r\n                      #face = &quot;italic&quot;, \r\n                      size = 12,\r\n                      color = &quot;black&quot;) + \r\n  theme(plot.margin = unit(c(t = 1, b = -3, r = 1, l = 2),&quot;cm&quot;))\r\n\r\n\r\n## Plots &amp; Table together with the caption using ggarange()\r\nfinal_display &lt;- ggarrange(plt1, plt2, tbl, text.p,\r\n          ncol = 2, nrow = 2)\r\n<\/pre>\n<p>I saved the canvas as <strong>final_display<\/strong> which I can now wrap in the <strong>annotate_figure() <\/strong>function to add the common title to the report.<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n## add a common title\r\nannotate_figure(final_display, top = text_grob(&quot;Investigation of Penguin Bill Lengths&quot;, \r\n                                      color = &quot;blue&quot;, face = &quot;bold&quot;, size = 18))\r\n<\/pre>\n<p>The finished product looks like this:<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-07-at-12.25.59-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-3053\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-07-at-12.25.59-PM-1024x573.png\" alt=\"\" width=\"625\" height=\"350\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-07-at-12.25.59-PM-1024x573.png 1024w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-07-at-12.25.59-PM-300x168.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-07-at-12.25.59-PM-768x430.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-07-at-12.25.59-PM-624x349.png 624w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Titles &amp; Captions with patchwork<\/strong><\/span><\/p>\n<p>Now, we will do the same thing with {<strong>patchwork<\/strong>}. Just like yesterday, to use {<strong>patchwork<\/strong>} we need to change the table from a <strong>ggtextable<\/strong> to a <strong>tableGrob<\/strong>. After that we can wrap it together with our two plots.<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n# Need to build the table as a tableGrob() instead of ggtextable\r\n# to make it work with patch work\r\ntbl2 &lt;- tableGrob(fit, rows = NULL, theme = ttheme(&quot;blank&quot;)) %&gt;%\r\n  tab_add_hline(at.row = 1:2, row.side = &quot;top&quot;, linewidth = 2) %&gt;%\r\n  tab_add_hline(at.row = 4, row.side = &quot;bottom&quot;, linewidth = 3, linetype = 1)\r\n\r\n# now visualize together\r\nfinal_display2 &lt;- wrap_plots(plt1, plt2, tbl2, \r\n           ncol = 2,\r\n           nrow = 2)\r\n<\/pre>\n<p>We stored the final canvas in the element <strong>final_display2<\/strong>. We can add a title, subtitle, caption, and bullet point box to this using patchwork&#8217;s <strong>plot_annotation() <\/strong>function by simply specifying the text that we would like.<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\nfinal_display2 + plot_annotation(\r\n  title = &quot;Investigation of Penguin Bill Lengths&quot;,\r\n  subtitle = &quot;Careful, sometimes the Penguins bite!!&quot;,\r\n  caption = &quot;data courtesty of {palmerpenguins} R package&quot;) + \r\n  grid::textGrob(hjust = 0, x = 0,\r\n                   &quot;* It appears that gender and flipper length are important for estimating bill length.\\n* Males have a bill length that is 2.073 mm greater than females on average.\\n* Penguins on different islands should be tested to determine\\nhow well this model will perform out of sample.&quot;)\r\n<\/pre>\n<p>Ads here is our final report:<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-07-at-12.57.32-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-3054\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-07-at-12.57.32-PM-1024x628.png\" alt=\"\" width=\"625\" height=\"383\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-07-at-12.57.32-PM-1024x628.png 1024w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-07-at-12.57.32-PM-300x184.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-07-at-12.57.32-PM-768x471.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2023\/05\/Screenshot-2023-05-07-at-12.57.32-PM-624x383.png 624w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Wrapping up<\/strong><\/span><\/p>\n<p>There are two simple ways using two different R packages to create single page reports with plots, data tables, and even bullet point notes for the reader. Happy report constructing!<\/p>\n<p>For the complete code to the blog article check out my <strong><span style=\"color: #0000ff;\"><a style=\"color: #0000ff;\" href=\"https:\/\/github.com\/pw2\/R-Tips-Tricks\/blob\/master\/Displaying%20Tables%20%26%20Plots%20Together%20Part%202%20-%20Adding%20Titles%20%26%20Captions.R\">GITHUB page<\/a><\/span><\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday&#8217;s post about creating single page reports with tables and plots in the same display window got a lot of follow up questions and comments (which is great!). In particular, Lyle Danley brought up a good point that adding titles to these reports, while important, can sometimes be tricky with these types of displays. So, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45,43],"tags":[],"class_list":["post-3052","post","type-post","status-publish","format-standard","hentry","category-r-tips-tricks","category-sports-analytics"],"_links":{"self":[{"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/posts\/3052","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/comments?post=3052"}],"version-history":[{"count":1,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/posts\/3052\/revisions"}],"predecessor-version":[{"id":3055,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/posts\/3052\/revisions\/3055"}],"wp:attachment":[{"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/media?parent=3052"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/categories?post=3052"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/tags?post=3052"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}