Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally change panel background with facet_grid?

Tags:

r

ggplot2

I'm using the "tips" data set in ggplot2. If I do

sp = ggplot(tips,aes(x=total_bill, y = tip/total_bill)) +          geom_point(shape=1) +          facet_grid(sex ~ day) 

The plot comes out fine. But I now want to change the panel background for just the plots under "Fri". Is there a way to do this?

Even better, can I conditionally change colors by passing parameters? For example if more than 3 points are below 0.1, then change panel background (for just that panel) to a certain color while all others remain the default light grey?

like image 868
broccoli Avatar asked Mar 23 '12 23:03

broccoli


1 Answers

The general rule for doing anything in ggplot2 is to,

  1. Create a data frame that encodes the information you want to plot
  2. Pass that data frame to a geom

This is made a bit more complicated in this case because of the particular aspect of the plot you want to alter. The Powers That Be designed ggplot2 in a way that separates data elements of the plot (i.e. geom's) from non-data elements (i.e. theme's), and it so happens that the plot background falls under the "non-data" category.

There is always the option of modifying the underlying grid object manually but this is tedious and the details may change with different versions of ggplot2. Instead, we'll employ the "hack" that Hadley refers to in this question.

#Create a data frame with the faceting variables # and some dummy data (that will be overwritten) tp <- unique(tips[,c('sex','day')]) tp$total_bill <- tp$tip <- 1  #Just Fri ggplot(tips,aes(x=total_bill, y = tip/total_bill)) +          geom_rect(data = subset(tp,day == 'Fri'),aes(fill = day),xmin = -Inf,xmax = Inf,             ymin = -Inf,ymax = Inf,alpha = 0.3) +         geom_point(shape=1) +          facet_grid(sex ~ day)  

enter image description here

#Each panel ggplot(tips,aes(x=total_bill, y = tip/total_bill)) +          geom_rect(data = tp,aes(fill = day),xmin = -Inf,xmax = Inf,             ymin = -Inf,ymax = Inf,alpha = 0.3) +         geom_point(shape=1) +          facet_grid(sex ~ day)  

enter image description here

like image 104
joran Avatar answered Oct 09 '22 01:10

joran