Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change options on a per-facet basis

Tags:

r

ggplot2

I'm using facets in ggplot2 to plot the distribution of expression in a large number of genes. My plotting commands are pretty generic:

p <- ggplot(top_n,aes(x=value,fill=ptype))
p <- p + geom_density(alpha = 0.2)
p <- p + facet_wrap(~probe,...)

they just plot the data in top_n as distributions coloured according to the ptype variable. It looks great.

Some of these genes, however, are more important than others. It would be really cool to highlight those genes which are transcription factors (TFs), for example. One way would be for me to change the colour title box over each facet corresponding to a TF, or the background colour, or something like that.

Is there a way for me to set options of the plots on a per-facet basis?

Digging around in the p object hasn't turned up anything I can use, though I'm probably missing something!

like image 326
Mike Dewar Avatar asked Jul 02 '10 16:07

Mike Dewar


People also ask

How do you change a facet label?

Facet labels can be modified using the option labeller , which should be a function. In the following R code, facets are labelled by combining the name of the grouping variable with group levels. The labeller function label_both is used.

What is the difference between Facet_wrap and Facet_grid?

The facet_grid() function will produce a grid of plots for each combination of variables that you specify, even if some plots are empty. The facet_wrap() function will only produce plots for the combinations of variables that have values, which means it won't produce any empty plots.

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. If you have only one variable with many levels, try facet_wrap() .

How does facet wrap work r?

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

You could use geom_rect with infinite extents and a fill colour that varies from facet to facet.

This has been done in a solution here: Conditionally change panel background with facet_grid?

like image 183
hadley Avatar answered Oct 31 '22 02:10

hadley