{"id":1663,"date":"2014-01-12T21:46:35","date_gmt":"2014-01-13T02:46:35","guid":{"rendered":"http:\/\/lukemiller.org\/?p=1663"},"modified":"2014-01-12T21:46:35","modified_gmt":"2014-01-13T02:46:35","slug":"part-2-make-your-r-figures-legible-in-powerpointkeynote-presentations","status":"publish","type":"post","link":"https:\/\/lukemiller.org\/index.php\/2014\/01\/part-2-make-your-r-figures-legible-in-powerpointkeynote-presentations\/","title":{"rendered":"Part 2: Make your R figures legible in Powerpoint\/Keynote presentations"},"content":{"rendered":"<p>In the <a href=\"https:\/\/lukemiller.org\/?p=1632\">previous post<\/a>, I outlined some tips for increasing the size of figure labels for figures that are meant to be displayed on a projector. The previous post used the base R <code>plot()<\/code> function, but the procedure when plotting with <a href=\"http:\/\/ggplot2.org\/\" target=\"_blank\">ggplot2<\/a> is different and usually quite a bit simpler than the stock R plotting functions. As before, I&#8217;m outputting the figures here as 1024&#215;768 PNG image files, since they&#8217;re sure to work in whatever version of PowerPoint or Keynote you&#8217;re stuck using. <\/p>\n<p>I&#8217;ll begin by generating some random data and dates to use in the plots. Since ggplot2 is an add-on package, it also needs be loaded by R before plotting.<br \/>\n[code lang=&#8221;r&#8221;]<br \/>\ndf = data.frame(x = rnorm(30), y = runif(30,min = 10,max = 10000),<br \/>\n\t\tdates = seq(as.Date(&#8216;2013-12-01&#8217;),(as.Date(&#8216;2013-12-01&#8217;)+29), by = 1))<br \/>\n# load libraries<br \/>\nrequire(ggplot2)<br \/>\n[\/code]<\/p>\n<p>To output a PNG file, you start by calling the <code>png()<\/code> function and feeding it some arguments to lay out the size of the output figure. Follow that with your ggplot2 commands, then a <code>print()<\/code> to print the figure to disk, and finish with <code>dev.off()<\/code> to save the file. <\/p>\n<p>[code lang=&#8221;r&#8221;]<br \/>\npng(file = &quot;pngtest9.png&quot;, width = 1024, height = 768, units = &#8216;px&#8217;)<br \/>\n# lay out basic figure<br \/>\nmygraph = ggplot(data = df, aes(x = dates, y = y))<br \/>\nmygraph = mygraph + geom_point()<br \/>\nprint(mygraph)<br \/>\ndev.off() # close plot and save to disk<br \/>\n[\/code]<br \/>\n<figure id=\"attachment_1666\" aria-describedby=\"caption-attachment-1666\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2014\/01\/pngtest9.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2014\/01\/pngtest9-300x225.png\" alt=\"The basic plot in a png file. All the labels are tiny. \" width=\"300\" height=\"225\" class=\"size-medium wp-image-1666\" srcset=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2014\/01\/pngtest9-300x225.png 300w, https:\/\/lukemiller.org\/wp-content\/uploads\/2014\/01\/pngtest9.png 1024w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-1666\" class=\"wp-caption-text\">The basic plot in a png file. All the labels are tiny.<\/figcaption><\/figure><\/p>\n<p>We ought to add some axis labels to the existing <code>graph<\/code> object:<br \/>\n[code lang=&#8221;r&#8221;]<br \/>\nmygraph = mygraph + xlab(&quot;Sample dates, 2013&quot;)<br \/>\nmygraph = mygraph + ylab(&quot;Some response variable&quot;)<br \/>\npng(file = &quot;pngtest10.png&quot;, width = 1024, height = 768, units = &#8216;px&#8217;)<br \/>\nprint(mygraph)<br \/>\ndev.off()<br \/>\n[\/code]<br \/>\n<figure id=\"attachment_1667\" aria-describedby=\"caption-attachment-1667\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2014\/01\/pngtest10.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2014\/01\/pngtest10-300x225.png\" alt=\"The plot with some more descriptive axis titles. Still too small to read. \" width=\"300\" height=\"225\" class=\"size-medium wp-image-1667\" srcset=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2014\/01\/pngtest10-300x225.png 300w, https:\/\/lukemiller.org\/wp-content\/uploads\/2014\/01\/pngtest10.png 1024w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-1667\" class=\"wp-caption-text\">The plot with some more descriptive axis titles. Still too small to read.<\/figcaption><\/figure><\/p>\n<p>The default font sizes in the output figure are far too small to read on a projector. To correct this, you can quickly modify the existing <code>graph<\/code> object to inflate the label sizes. The built-in formatting in ggplot2 makes this procedure much simpler than in the standard <code>plot()<\/code> function, since ggplot2 shoves the margins around to make things fit nicely for you. The <code>size = rel(2)<\/code> argument increases the labels to 2x the default size.<br \/>\n[code lang=&#8221;r&#8221;]<br \/>\n# Increase size of all axis labels<br \/>\nmygraph = mygraph + theme(axis.text = element_text(size = rel(2)))<br \/>\nmygraph = mygraph + theme(axis.title = element_text(size = rel(2)))<br \/>\npng(file = &quot;pngtest11.png&quot;, width = 1024, height = 768, units = &#8216;px&#8217;)<br \/>\nprint(mygraph)<br \/>\ndev.off()<br \/>\n[\/code]<br \/>\n<figure id=\"attachment_1668\" aria-describedby=\"caption-attachment-1668\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2014\/01\/pngtest11.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2014\/01\/pngtest11-300x225.png\" alt=\"Now the axis labels are large enough to read, but they could probably be larger. \" width=\"300\" height=\"225\" class=\"size-medium wp-image-1668\" srcset=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2014\/01\/pngtest11-300x225.png 300w, https:\/\/lukemiller.org\/wp-content\/uploads\/2014\/01\/pngtest11.png 1024w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-1668\" class=\"wp-caption-text\">Now the axis labels are large enough to read, but they could probably be larger.<\/figcaption><\/figure><\/p>\n<p>With large labels on the x-axis like this, if you want to print them any larger it becomes necessary to rotate them to squeeze a decent number of labels in. Thankfully this is simple to do in ggplot2.<br \/>\n[code lang=&#8221;r&#8221;]<br \/>\n# Rotate x axis tick labels and increase size even further<br \/>\nmygraph = mygraph + theme(axis.text.x = element_text(angle = 45, vjust = 0.9,<br \/>\n\t\t\t\thjust = 0.9, size = rel(3)))<br \/>\npng(file = &quot;pngtest12.png&quot;, width = 1024, height = 768, units = &#8216;px&#8217;)<br \/>\nprint(mygraph)<br \/>\ndev.off()<br \/>\n[\/code]<br \/>\n<figure id=\"attachment_1669\" aria-describedby=\"caption-attachment-1669\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2014\/01\/pngtest12.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2014\/01\/pngtest12-300x225.png\" alt=\"Now it&#039;s just getting silly, but here are some large x-axis labels. \" width=\"300\" height=\"225\" class=\"size-medium wp-image-1669\" srcset=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2014\/01\/pngtest12-300x225.png 300w, https:\/\/lukemiller.org\/wp-content\/uploads\/2014\/01\/pngtest12.png 1024w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-1669\" class=\"wp-caption-text\">Now it&#8217;s just getting silly, but here are some large x-axis labels.<\/figcaption><\/figure><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous post, I outlined some tips for increasing the size of figure labels for figures that are meant to be displayed on a projector. The previous post used the base R plot() function, but the procedure when plotting with ggplot2 is different and usually quite a bit simpler than the stock R plotting [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[218],"tags":[91,175,17,174],"class_list":["post-1663","post","type-post","status-publish","format-standard","hentry","category-r-project","tag-ggplot2","tag-keynote","tag-plotting","tag-powerpoint"],"_links":{"self":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/1663","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/comments?post=1663"}],"version-history":[{"count":8,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/1663\/revisions"}],"predecessor-version":[{"id":1675,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/1663\/revisions\/1675"}],"wp:attachment":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/media?parent=1663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/categories?post=1663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/tags?post=1663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}