tags:

views:

203

answers:

2

I have some data for which, at one level of a factor, there is a significant correlation. At the other level, there is none. Plotting these side-by-side is simple. Adding a line to both of them with stat_smooth, also straightforward. However, I do not want the line or its fill displayed in one of the two facets. Is there a simple way to do this? Perhaps specifying a blank color for the fill and colour of one of the lines somehow?

+1  A: 

Of course, I later answered my own question. Although, is there a less hack-y way to do this? I wonder if one could even fit different functions to different panels.

One technique is to use + scale_fill_manual and scale_colour_manual. They allow one to specify what colors will be used. So, in this case, let's say you have

a<-qplot(x, y, facets=~z)+stat_smooth(method="lm", aes(colour=z, fill=z))

You can specify colors for the fill and colour using the following. Note, the second color is clear, as it is using a hex value with the final two numbers representing transparency. So, 00=clear.

a+stat_fill_manual(values=c("grey", "#11111100"))+scale_colour_manual(values=c("blue", "#11111100"))
jebyrnes
That sounds like a reasonable solution. In general, if you're using facets, you really do want to put the same thing in each facet. If you don't, you're likely better off using alternative approaches. Check out this question (and my answer) for more information: http://stackoverflow.com/questions/1532535/showing-multiple-axis-labels-using-ggplot2-with-facetwrap-in-r
Harlan
+6  A: 

Don't think about picking a facet, think supplying a subset of your data to stat_smooth:

ggplot(df, aes(x, y)) +
  geom_point() + 
  geom_smooth(data = subset(df, z =="a")) + 
  facet_wrap(~ z)
hadley