Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2: How to get merge functionality of facet_grid()'s labeller=label_both and facet_wrap()'s ncol options?

I am creating a boxplot using ggplot2. I am able to create the boxplot successfully but I am having problems when merging two features with faceting in ggplot2:

  1. When faceting, strip text should contain column name & column value (as given by labeller=label_both argument in facet_grid()).
  2. The facets should be split into as many rows & columns as I want (as given by nrow and ncol argument in facet_wrap()).

Here is a subset of my dataframe (The actual dataframe is quite large):

>tmpdf

    value  treat FoldChange Gene Pvalue.Adj
 523.8589 Normal     -1.899    A    0.02828
 489.7638 Normal     -1.899    A    0.02828
 642.0126 Cancer     -1.899    A    0.02828
 928.8136 Cancer     -1.899    A    0.02828
 624.7892 Normal     -1.899    A    0.02828
  53.8685 Normal     -7.135    B    0.00012
 184.6473 Normal     -7.135    B    0.00012
  76.2712 Cancer     -7.135    B    0.00012
  48.0607 Cancer     -7.135    B    0.00012
 177.9528 Normal     -7.135    B    0.00012
4581.2847 Normal     -1.886    C    0.04924
7711.3411 Normal     -1.886    C    0.04924
6007.9852 Cancer     -1.886    C    0.04924
5940.9232 Cancer     -1.886    C    0.04924
4433.0949 Normal     -1.886    C    0.04924
 171.9172 Normal     -4.594    D    0.00093
 316.3900 Cancer     -4.594    D    0.00093
 231.3177 Cancer     -4.594    D    0.00093
 295.1096 Normal     -4.594    D    0.00093
 322.8346 Normal     -4.594    D    0.00093

This is my code:

#Here I am using facet_grid and labeller=label_both, this gives me column names & column values in the facet strip text but I am unable to divide the plot into columns & rows.

ggplot(tmpdf,aes(x=treat,y=log(value+1),fill=treat)) + geom_boxplot(show_guide=F) + 
facet_grid(~Gene+FoldChange+Pvalue.Adj,labeller=label_both) + ylab("log2(Expression)\n") + 
ggtitle("Boxplot with facet_grid & labeller\n\nlabeller=label_both shows column names with column values\n") +
theme(axis.text.x=element_text(size=14,color="black"),
    axis.text.y=element_text(size=14,color="black"),
    strip.text=element_text(size=14,color="black"),
    axis.title.x=element_blank(),
    axis.title.y=element_text(size=14,color="black"),
    plot.title=element_text(size=16,color="black"))

#This creates the following plot:

#And here is the second code:
#This divides the plot into as many columns as I want but doesn't show the column name in the facet strip text. 
#Strip text contains comma-separated values that are less informative.

ggplot(tmpdf,aes(x=treat,y=log(value+1),fill=treat)) + geom_boxplot(show_guide=F) + 
facet_wrap(~Gene+FoldChange+Pvalue.Adj,ncol=2) + ylab("log2(Expression)\n") + 
ggtitle("Boxplot with facet_wrap & ncol\n\nDoesn't show column name\n") +
theme(axis.text.x=element_text(size=14,color="black"),
    axis.text.y=element_text(size=14,color="black"),
    strip.text=element_text(size=14,color="black"),
    axis.title.x=element_blank(),
    axis.title.y=element_text(size=14,color="black"),
    plot.title=element_text(size=16,color="black"))

#This creates the following plot:

How can I split my plot into multiple columns/rows as well as have a descriptive strip text containing the column name & value in the facets?

like image 852
Komal Rathi Avatar asked Aug 01 '14 16:08

Komal Rathi


People also ask

What is the function of Facet_grid () in Ggplot ()?

facet_grid() forms a matrix of panels defined by row and column faceting variables. It is most useful when you have two discrete variables, and all combinations of the variables exist in the data.

What is the difference between Facet_wrap and Facet_grid?

While facet_grid shows the labels at the margins of the facet plot, facet_wrap creates a label for each plot panel.

What does Facet_wrap do in Ggplot?

facet_wrap() makes a long ribbon of panels (generated by any number of variables) and wraps it into 2d. This is useful if you have a single variable with many levels and want to arrange the plots in a more space efficient manner. You can control how the ribbon is wrapped into a grid with ncol , nrow , as.


1 Answers

This is a bit of a hack but it does seem to provide what you are asking for.

tmpdf$label <- with(tmpdf,paste("Gene:",Gene,"\n","FoldChange:",FoldChange,"\n","Pvalue.Adj:",Pvalue.Adj))

ggplot(tmpdf,aes(x=treat,y=log(value+1),fill=treat)) + geom_boxplot(show_guide=F) + 
  facet_wrap(~label,ncol=2) + ylab("log2(Expression)\n") + 
  ggtitle("Boxplot with facet_wrap & ncol") +
  theme(axis.text.x=element_text(size=14,color="black"),
        axis.text.y=element_text(size=14,color="black"),
        strip.text=element_text(size=14,color="black"),
        axis.title.x=element_blank(),
        axis.title.y=element_text(size=14,color="black"),
        plot.title=element_text(size=16,color="black"))

like image 161
jlhoward Avatar answered Oct 17 '22 22:10

jlhoward