Modifying cummeRbund Plots

Don’t know why it took me so long but I realized that the RNAseq plots in cummeRbund are merely ggplot2 objects that can be modified.  Here’s an example of how you can clean up and alter the appearance of cummeRbund plots.

Say I have a list of genes and I want to draw an expression barplot for each gene and each sample.  There’s a nice command in cummeRbund, expressionBarplot(), that does this for you.  You can even feed as input a getGenes() command to extract only a subset of genes.  So if I have a vector of gene symbols named ctxList and a vector giving the order I want the samples displayed, named sampleIdList, I can issue this command:

expressionBarplot(getGenes(cuff,ctxList,sampleIdList=sampOrder))

I get this plot:

rplotexample1

But I want to fix a number of things.  I don’t like the default grey background.  I want to control how the error bars are drawn–in this case I want them based on confidence interval.  And I wanted to clean up colors and fonts.  So I wrote this function in R:

myPlotStyle=function(p, leg=T){
  if(leg){
    pos=c(.9,.8)
  } else {
    pos="none"
  }
  p+theme(axis.title.y=element_text(family="Arial",size=18),
          axis.text.y=element_text(family="Arial",size=16),
          axis.line.y=element_line(color="black",size=1),
          axis.text.x=element_text(family="Arial",size=18,color="black",vjust=.5),
          axis.line.x=element_line(color="black",size=1),
          axis.ticks.x=element_blank(),
          legend.title=element_blank(),
          legend.text=element_text(family="Arial",size=18),
          legend.position=pos,
          panel.background=element_rect(fill="white"))+
    scale_fill_brewer(palette="Set1")+
    geom_errorbar(mapping=aes(ymin=conf_lo,ymax=conf_hi),size=.2,na.rm = F,
                  width=.5,stat="identity",position=position_dodge(width=.9))
}

I wanted:

  • An option whether to include a legend or not, and to place it in the upper right corner if present
  • The ability to use the system font “Arial” (this requires the library extrafont, and I’ve already imported Arial into R before this step)
  • Set sizes of font and choices to exclude ticks and legend title
  • Use a white background
  • Bars colored using the color brewer palette
  • Finally, errorbars based on confidence intervals.  I figured out how the expressionBarplot function stored alternate variation metrics and picked the one I wanted.  There are others if you look. For example, you could use standard deviation by specifying fpkm-stdev and fpkm+stdev for ymin and ymax in the geom_errorbar() function

After loading the function into your environment, just re-issue your barplot command like this:

myPlotStyle(p=expressionBarplot(getGenes(cuff,ctxList,sampleIdList=sampOrder),
 showErrorbars=F),leg=T)

rplotexample2

This gives me something publishable.  I tell expressionBarplot not to plot error bars and add my own.  I can easily re-use the function for other plots with other lists of genes.  The colors are much more vibrant than the defaults.

The data are taken from Oni et al., 2016 — See Supplemental Figure 3.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s