knitr::opts_chunk$set(
  collapse = TRUE,
  fig.width=7,
  fig.height=7,
  comment = "#>"
)

This vignette will introduce you to the package makeitprettier, which is fairly mundane affair, but will also be used to collect the tips and tricks that I frequently use when building plots/figures for presentation/publication and that could not be wrapped up in a function.

Installation

The easiest way is using devtools to install straight from github:

# install.packages("devtools")
devtools::install_github("adomingues/makeitprettier")

Usage

The package is organized in functions that can be added straighforwardly to any plot. Those functions are define as:

I kept the theme simple and clean so that we can focus on the data, but did not get rid of all gridlines because I find those helpful to read values in a plot, ins particular in a poster when people may actually want to look at the detail.

So how to actually use it? I have included a data set that mimics a typical IP experiment, with 3 replicates and 3 different antibodies to show what we can do with it. First let's plot the number of reads for each length :

library("makeitprettier")
head(readcounts)

p1 <- ggplot(readcounts, aes(x = readlength, y = N, color = Mapping)) +
  geom_line() +
  facet_grid(IP ~ Replicate)
p1

To make it prettier we just add our little functions:

p1 + 
  theme_poster() +
  scale_color_prettier()

We did away with most of the visual clutter to make a much cleaner plot. I am planning on publication function that does away with even more visual elements:

(brief) ggplot explanation

The plot above is a fairly complicated visualization in which we want to compare multiple things:

In ggplot we can map a certain visual attribute to a condition/factor. For instance, line color to the "Mapping". We can also use the factors to split the data in rows/columns as we did with facet_grid(IP ~ Replicate) where we are simply telling ggplot that we can split those variables in a grid with the IPs horizontally and the replicates vertically.

Further improvements

You would never put this in a final manuscript! The axis titles are either not readable, not informative or ugly. The values in a the y-axis are also pretty hard to read in a quick glance. Let's fix it.

p2 <- p1 + 
  theme_poster() +
  scale_color_prettier()+
  scale_y_continuous(labels = scales::comma) +
  labs(
    x = "Read length (nt)",
    y = "Number of reads") 

p2

There you have it. It could almost go like this into print.

Save plots

As you probably noticed, by default gpplot saves plots which requite resizing before inclusion in a poster. I have included a little function that saves the plots:

save_plot("~/test_save")

The function knows which plot was generated last and saves that one. Regardless, it can also be told which plot to save:

save_plot("~/test_save", p2)

I found that 15 x 15 cm was about the right size for figure to slapped straight into a poster. The function takes all the usual ggsave arguments so the plot dimensions can be changed at will.

It might (and it will be) useful to save the data in the plot to re-do it at a later stage without doing the analysis all over again. For that there is a little function:

save_data("~/test_save.data.txt")
save_data("~/test_save.data.txt", p2)

The above will do the same, and will this:

save_plot("~/test_save", save_data = TRUE)


adomingues/makeitprettier documentation built on June 28, 2019, 9:47 a.m.