<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/detailed-introduction-of-myprettyreport-r-package/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://blog.myhappydata.com/2018/11/10/detailed-introduction-of-myprettyreport-r-package/"> R – MYHAPPYDATA BLOG</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><strong>Package introduction:</strong><br>
Package myprettyreport helps to export ggplot2 graphs into a good-looking PDF file in a clear and easy way with a wide range of flexibility. It has a modular structure so the report elements can be combined in many ways. </p>
<p><strong>Installation:</strong><br>
This package currently only available on <a rel="nofollow" href="https://github.com/tarkomatas/myprettyreport" target="_blank">Github</a> so the proper way to install it:</p>
<pre>#install.packages(&quot;devtools&quot;)
devtools::install_github(&quot;tarkomatas/myprettyreport&quot;)</pre>
<p><strong>Quick overview:</strong></p>
<p>Functions of the package:<br>
<span>start_report()</span> is the first mandatory function to generate the report.<br>
<span>add_cover_page()</span> function generates the cover page of the report.<br>
<span>add_new_page()</span> function adds a single report page to the report.<br>
<span>add_multiple_page()</span> function adds multiple report pages to the report.<br>
<span>end_report()</span> function generates the final output and closes the process.</p>
<p>Quick example:</p>
<pre>library(ggplot2)
sample_plot &lt;- ggplot(data = mtcars, mapping = aes(x = wt, y = mpg)) +
  geom_point() +
  stat_smooth(method = 'lm', color = &quot;#f44242&quot;, fill = &quot;#fd9068&quot;)

library(magick)
sample_logo &lt;- image_read(&quot;http://myhappydata.com/img/1.png&quot;)

library(myprettyreport)
start_report() %>%

  add_cover_page(
    logo = sample_logo,
    logo_size = 0.3
  ) %>%

  add_new_page(
    plot = sample_plot,
    need_header = TRUE,
    logo = sample_logo,
    logo_size = 0.2
  ) %>%

  end_report()</pre>
<p>Result:</p>
<p><img alt="" width="410" src="https://i2.wp.com/raw.githubusercontent.com/tarkomatas/myprettyreport/master/man/figures/README_example1.png?resize=410%2C584&ssl=1" data-recalc-dims="1" height="584"><br>
<img alt="" width="410" src="https://i2.wp.com/raw.githubusercontent.com/tarkomatas/myprettyreport/master/man/figures/README_example2.png?resize=410%2C584&ssl=1" data-recalc-dims="1" height="584"></p>
<h2><strong> Basics </strong></h2>
<p>A minimal example which generates a PDF file but it contains just a blank page:</p>
<pre>start_report() %>%
  end_report()</pre>
<p><a rel="nofollow" href="http://blog.myhappydata.com/wp-content/uploads/2018/09/report_output.pdf" target="_blank">report output</a></p>
<p>As we can see each elements can be added to each other with the magrittr::%>% (pipe) function.</p>
<p>Insert a new report page to a PDF file which creates a skeleton ggplot2 object as default.</p>
<pre>start_report() %>%
  add_new_page() %>%
  end_report()</pre>
<p>Now let’s create our first very own ggplot2 object and export it. The report syntax is almost the same except we have to add our ggplot2 object to a report as a plot parameter value.</p>
<pre>library(ggplot2)

sample_plot &lt;- ggplot(data = mtcars, mapping = aes(x = wt, y = mpg)) +
  geom_point() +
  stat_smooth(method = 'lm', color = &quot;#f44242&quot;, fill = &quot;#fd9068&quot;)

start_report() %>%
  add_new_page(plot = sample_plot) %>%
  end_report()</pre>
<p>The result will be equivalent so far if we just use the ggplot2 built in export function:</p>
<pre>ggsave(&quot;report_output.pdf&quot;, width = 21, height = 29.7, units = &quot;cm&quot;)</pre>
<p><a rel="nofollow" href="http://blog.myhappydata.com/wp-content/uploads/2018/09/output_ggsave.pdf" target="_blank">output_ggsave</a><br>
<a rel="nofollow" href="http://blog.myhappydata.com/wp-content/uploads/2018/09/output_myprettyreport.pdf" target="_blank">output_myprettyreport</a></p>
<p>Although the result is the same it is easier to modify the report layout a little bit thanks to the package myprettyreport. We could create a footer and a header section like this: </p>
<pre>start_report() %>%
    add_new_page(
        plot = sample_plot,
        need_header = TRUE,
        need_footer = TRUE,
        footer_text = &quot;Copyright © Tamas Marko | 2018 | myhappydata.com&quot;) %>%
    end_report()</pre>
<p><a rel="nofollow" href="http://blog.myhappydata.com/wp-content/uploads/2018/11/report_output.pdf" target="_blank">report_output</a></p>
<p>Also it could be possible to add a logo to a top left corner. All we need is to load a logo file into R and add it as a logo parameter. Optionally the size of the logo could be specified (valid values are between 0 and 1). </p>
<pre>library(magick)
sample_logo &lt;- image_read(&quot;http://myhappydata.com/img/1.png&quot;)

start_report() %>%
    add_new_page(
        plot = sample_plot,
        need_header = TRUE,
        need_footer = TRUE,
        footer_text = &quot;Copyright © Tamas Marko | 2018 | myhappydata.com&quot;,
        logo = sample_logo,
        logo_size = 0.2) %>%
    end_report()</pre>
<p><a rel="nofollow" href="http://blog.myhappydata.com/wp-content/uploads/2018/11/report_output-1.pdf" target="_blank">report_output</a></p>
<p>Currently 2 themes have been implemented yet. Now we are changing the theme from the default to a theme called flashy. In this case we have to modify the default values of the header and footer colors also because the default color of the text is white.</p>
<pre>start_report() %>%
    add_new_page(
        plot = sample_plot,
        need_header = TRUE,
        need_footer = TRUE,
        footer_text = &quot;Copyright © Tamas Marko | 2018 | myhappydata.com&quot;,
        logo = sample_logo,
        logo_size = 1,
        theme = &quot;flashy&quot;,
        header_color = &quot;#f44242&quot;,
        footer_color = &quot;#3d3c3c&quot;) %>%
    end_report()</pre>
<p><a rel="nofollow" href="http://blog.myhappydata.com/wp-content/uploads/2018/09/report_output-3.pdf" target="_blank">report_output</a></p>
<p>We may notice that if we use this theme, the position of the logo will also change (now it is on the bottom left corner). So the size parameter of the logo will probably need to be reconfigured also. </p>
<p>The next step could be adding one more page to our report. In this case the preferred way is to use the add_multiple_page() function because it results more simplified syntax:</p>
<pre>sample_plot2 &lt;- ggplot(airquality, aes(x=Temp)) + 
  geom_histogram(binwidth=1, fill = &quot;#f66f6f&quot;)
plot_list &lt;- list(sample_plot, sample_plot2)

start_report() %>%
    add_multiple_page(
        plot = plot_list,
        need_header = TRUE,
        need_footer = TRUE,
        page_number = rep(&quot;Copyright © Tamas Marko | 2018 | myhappydata.com&quot;, length(plot_list)),
        logo = sample_logo,
        logo_size = 1,
        theme = &quot;flashy&quot;,
        header_color = &quot;#f44242&quot;,
        footer_color = &quot;#3d3c3c&quot;) %>%
    end_report()</pre>
<p>Important to note that the add_multiple_page() function was executed (not the add_new_page()) and it has a page_number parameter instead of footer_text! So in this case we have to multiply the footer text value. </p>
<p><a rel="nofollow" href="http://blog.myhappydata.com/wp-content/uploads/2018/09/report_output-4.pdf" target="_blank">report_output</a></p>
<p>Now let’s add a cover page to our report. We have to insert the add_cover_page() function after the start_report() function.</p>
<pre>start_report() %>%
    add_cover_page() %>%
    add_multiple_page(
        plot = plot_list,
        need_header = TRUE,
        need_footer = TRUE,
        page_number = rep(&quot;Copyright © Tamas Marko | 2018 | myhappydata.com&quot;, length(plot_list)),
        logo = sample_logo,
        logo_size = 1,
        theme = &quot;flashy&quot;,
        header_color = &quot;#f44242&quot;,
        footer_color = &quot;#3d3c3c&quot;) %>%
    end_report()</pre>
<p><a rel="nofollow" href="http://blog.myhappydata.com/wp-content/uploads/2018/11/report_output-2.pdf" target="_blank">report_output</a></p>
<p>It is also possible to change the theme of the cover page if we want the same “flashy” theme everywhere:</p>
<pre>start_report() %>%
    add_cover_page(
        theme = &quot;flashy&quot;
    ) %>%
    add_multiple_page(
        plot = plot_list,
        need_header = TRUE,
        need_footer = TRUE,
        page_number = rep(&quot;Copyright © Tamas Marko | 2018 | myhappydata.com&quot;, length(plot_list)),
        logo = sample_logo,
        logo_size = 1,
        theme = &quot;flashy&quot;,
        header_color = &quot;#f44242&quot;,
        footer_color = &quot;#3d3c3c&quot;) %>%
    end_report()</pre>
<p><a rel="nofollow" href="http://blog.myhappydata.com/wp-content/uploads/2018/09/report_output-6.pdf" target="_blank">report_output</a></p>
<p>Now let’s add multiple plots to a single page:</p>
<pre>start_report() %>%
  add_new_page(
     plot = plot_list,
     plot_hpos = c(1, 2),
     plot_vpos = c(1, 2),
     plot_area_layout = grid::grid.layout(2, 2,
                                          width = c(1, 1),
                                          heigh = c(1, 1))
  ) %>%
  end_report()</pre>
<p>In this case we have to specify the layout of the plot area with the grid::grid.layout() function. Also we have to determine the position of every single plot (plot_hpos and plot_vpos). For example if we create a 2×2 plot area plot_hpos = c(1, 2) means that the first plot in the plot_list will be in the left side of the page, and second one will be on the other side.</p>
<p>Optionally of course we could also use other libraries to reach the same goal. My personal recommendation is the <a rel="nofollow" href="https://github.com/thomasp85/patchwork" target="_blank">patchwork</a> package. As I’ve mentioned already the result will be the same:</p>
<pre>library(patchwork)

multiple_plot_one_page &lt;- sample_plot + patchwork::plot_spacer() +
                          patchwork::plot_spacer() + sample_plot2 +
                          patchwork::plot_layout(ncol = 2, nrow = 2)

start_report() %>%
  add_new_page(
    plot = multiple_plot_one_page
  ) %>%
  end_report()</pre>
<p><a rel="nofollow" href="http://blog.myhappydata.com/wp-content/uploads/2018/11/report_output-3.pdf" target="_blank">report_output</a><br>
<a rel="nofollow" href="http://blog.myhappydata.com/wp-content/uploads/2018/11/report_output_pw.pdf" target="_blank">report_output_patchwork</a></p>
<p>Finally create a report which has multiple pages and also multiple plots in every page:</p>
<pre>plot_list &lt;- list(
  list(sample_plot, sample_plot2),
  list(sample_plot2, sample_plot)
)

start_report() %>%
 add_multiple_page(
    plot = plot_list,
    plot_hpos = c(1, 2),
    plot_vpos = c(1, 2),
    plot_area_layout = grid::grid.layout(2, 2,
                                         width = c(1, 1),
                                         heigh = c(1, 1))
 ) %>%
 end_report()</pre>
<p>So if we compare this syntax to the previous one we could see that the only difference is that we need to create sublists inside our list and every plot which has been defined inside a sublist will appear in the same report page. </p>
<h2>Advanced features</h2>
<p><strong>Export multiple ggplot2 objects into multiple files</strong></p>
<pre>create_multiple_report &lt;- function(db, xvar, yvar) {
  sample_plot &lt;- ggplot(data = db, mapping = aes_string(x = xvar, y = yvar)) +
    geom_point() +
    stat_smooth(method = 'lm', color = &quot;#f44242&quot;, fill = &quot;#fd9068&quot;)

  start_report(
    filename = paste0(xvar,&quot;.pdf&quot;)
  ) %>%
    add_new_page(plot = sample_plot) %>%
    end_report()
}

lapply(colnames(mtcars)[5:7], create_multiple_report, db = mtcars, yvar = &quot;mpg&quot;)</pre>
<p><a rel="nofollow" href="http://blog.myhappydata.com/wp-content/uploads/2018/09/drat.pdf" target="_blank">drat</a><br>
<a rel="nofollow" href="http://blog.myhappydata.com/wp-content/uploads/2018/09/qsec.pdf" target="_blank">qsec</a><br>
<a rel="nofollow" href="http://blog.myhappydata.com/wp-content/uploads/2018/09/wt.pdf" target="_blank">wt</a></p>
<p><strong>Customize the report with extra grid parameters</strong></p>
<p>The package can allows us to pass extra grid parameters to our report to customize the final output. It can be useful if we need e.g. an additional text field. First of all we have to know the default layout structure of the report. If you haven’t heard about the basics of grid layouts it is recommended to go deeper into that topic at first. I share the structure of the layouts which was produced by the grid::grid.show.layout() function:</p>
<p><a rel="nofollow" href="http://blog.myhappydata.com/wp-content/uploads/2018/11/cover_page_theme_basic.pdf" target="_blank">cover_page_theme_basic</a><br>
<a rel="nofollow" href="http://blog.myhappydata.com/wp-content/uploads/2018/11/report_page_theme_basic.pdf" target="_blank">report_page_theme_basic</a></p>
<p><a rel="nofollow" href="http://blog.myhappydata.com/wp-content/uploads/2018/11/cover_page_theme_flashy.pdf" target="_blank">cover_page_theme_flashy</a><br>
<a rel="nofollow" href="http://blog.myhappydata.com/wp-content/uploads/2018/11/report_page_theme_flashy.pdf" target="_blank">report_page_theme_flashy</a></p>
<p>So e.g. if we want to insert an extra text field in the report page header when the theme is the basic, the syntax is the following:</p>
<pre>rp_e_layout_params = function() {
    grid::grid.text(&quot;example text&quot;,
                    y = 0.7,
                    vp = grid::viewport(layout.pos.row = 1,
                                        layout.pos.col = 1))
 }
 start_report() %>%
   add_new_page(
     plot = sample_plot,
     need_header = TRUE,  
     extra_layout_params = rp_e_layout_params
   ) %>%
   end_report()</pre>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://blog.myhappydata.com/2018/11/10/detailed-introduction-of-myprettyreport-r-package/"> R – MYHAPPYDATA BLOG</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/qO2aR-rZPEQ" height="1"></a></div>