Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding stat_smooth in to only 1 facet in ggplot2

Tags:

r

ggplot2

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?

like image 754
jebyrnes Avatar asked Oct 15 '09 05:10

jebyrnes


People also ask

What does Facet_wrap () help us do when added to a 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.

How do you add a facet in R?

Adding facets Then, to add facets, use the function facet_grid(). This function allows you to create a two-dimensional grid that defines the facet variables. You write the argument to facet_grid() as a formula of the form rows ~ columns. In other words, a tilde (~) separates the row variable from the column variable.

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 facet in ggplot2?

The facet approach partitions a plot into a matrix of panels. Each panel shows a different subset of the data. This R tutorial describes how to split a graph using ggplot2 package. There are two main functions for faceting : facet_grid()


1 Answers

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)
like image 186
hadley Avatar answered Oct 12 '22 01:10

hadley