Thursday, July 30, 2015

cowplot: arrange ggplot2 figures in a grid

Sadly, cowplot doesn't create plots of cows, but it would be fun if it did! So I thought the next best thing would be to include some cow data. :p

What is cowplot? cowplot is a fun little R package that labels and arranges figures created by ggplot2 into a grid. It also has a nice publication-ready theme for ggplot2 plots. I started using cowplot a few weeks ago and thought I would write a short blog post on the handy features.

For the last few years if I wanted to arrange plots created by ggplot2 into a grid, I would use either the multiplot() function from the Cookbook for R or the arrange_ggplot2() function from Stephen Turner's blog Getting Genetics Done. If I were creating these plots for a publication, I would also want to add labels (e.g. ABC, etc) to the arranged plots to be able to refer to individual plots. I would also typically fiddle with the default theme in ggplot2 to remove the default gray background and grid lines (personal preference thing).

Why is it useful? cowplot offers two really cool features:
  1. The default theme in cowplot has no gray background and no grid lines. It is very similar to ggplot2's theme_classic(), but the main difference is mostly related to the font sizes.
  2. There are two functions plot_grid() and ggdraw() that allow the user to arrange and label figures in grid. The plots can be arranged according to a specified number of columns (or rows), horizontally (or vertically), and can be labeled. 
For this blogpost, I found a data set that measures the butterfat content from 100 cows of different breeds and different ages. Very appropriate! :) The data set is located in the PAWSR R package. 

Here used ggplot2 to create box plots comparing the butterfat content of the different breeds along the x-axis and colored by the age of the cows.  The first set of box plots uses the default cowplot theme. The second uses ggplot2's classic theme and the default theme. To get ggplot2's classic theme and default theme in cowplot, use classic_theme() and theme_gray(), respectively.  Notice how the default theme in cowplot does not contain the gray background and gridlines.  Also, the color of the axis ticks are black instead of gray the ggplot2 default gray theme. 

Density plots of the butter fat content colored by age and faceted by breed.

Now, sometimes there is a reason to  have the grid lines loaded. In that case, you can use the background_grid() function.

The second thing I really like about cowplot is the built in functions to arrange and label figures in a grid. There are two ways of doing this.  The simplest way is to use plot_grid() which takes in a list of ggplot2 plot names and arranges them in a grid according to the specified number of columns (or rows) and how to arrange the plots (vertically or horizontally), etc.  You can also provide a set of labels (and label sizes).

Because figure D is more wide than the other three figures, we may want to manually create the grid of plots to be able to specify the width and height of each figure individually.  In that case, you want to use the ggdraw() function with the draw_plot() function.  The ggdraw() function is the base layer and then you can place individual plots at arbitrary locations using the draw_plot() function.

As a final note, you can also check out the functions draw_label(), draw_line(), and  draw_text() which allow you to draw lines and text on top of the individual plots.

Monday, July 20, 2015

Backed my first Kickstarter: Reader

Today I backed my first Kickstarter which will create Reader: Dragging Science Publications into the 21st Century.  As someone who reads a lot of science publications, I think this is a fantastic idea! Reader is a viewer that will render manuscripts in a web browser (phone, tablet and computer) to allow the reader to interact and discuss papers, bookmark papers & citations, load figures and inline context of the paper (no software or app to install).  

It's always a hassle to flip back and forth between the results section, figures, figure legend, and references when reading a paper in the traditional format. Now all of that will be changed with Reader. Here is an demo of a publication from their website. I look forward to start using Reader in August 2015 (expected date of completion).  

Saturday, July 4, 2015

Angel food cake with chocolate whipped cream

Happy Fourth of July! One of my favorite summer desserts (and one of my favorite Fourth of July desserts) is angel food cake with whipped cream and fresh fruit.  In the hot days of summer, there is something particularly nice about this cake.  It's light, fluffy and just sweet enough to balance out the whipped cream and fruit.  Technically, I'm cheating by starting with a box cake mix (very rare for me), but the boxed cake mix is pretty good in this case!

- 14oz heavy cream
- 1 box angel food cake mix
- strawberries, diced
- blueberries

- 1/4 cup granulated sugar
- 2 tbsp cocco powder


1) Mix box cake mix and 1 1/4 cups water on high for 1 min. Start the blender slowly and then move faster to avoid cake mix flying everywhere.  Place cake mix in an angel food cake bundt pan.

2) Bake angel food cake at 350 degrees for 32-35 mins (until golden brown). Be careful to not under bake the cake mix.

Let the cake cool completely.

3) Whip the heavy cream for 1min on high. Slowly add granulated sugar and coco powder.  Continue to whip the cream until stiff peaks have formed. Be careful to not over mix (or you'll end up with butter!).

My favorite way to serve the cake is simply with a dollop of chocolate whipped cream and fresh fruit.

Happy 4th of July everyone!