Matt Parker
Matt Parker

Reputation: 27359

Subset of data included in more than one ggplot facet

I have a population and a sample of that population. I've made a few plots comparing them using ggplot2 and its faceting option, but it occurred to me that having the sample in its own facet will distort the population plots (however slightly). Is there a way to facet the plots so that all records are in the population plot, and just the sampled records in the second plot?

Upvotes: 1

Views: 1547

Answers (1)

Chase
Chase

Reputation: 69221

Matt,

If I understood your question properly - you want to have a faceted plot where one panel contains all of your data, and the subsequent facets contain only a subset of that first plot?

There's probably a cleaner way to do this, but you can create a new data.frame object with the appropriate faceting variable that corresponds to each subset. Consider:

library(ggplot2)
df <- data.frame(x = rnorm(100), y = rnorm(100), sub = sample(letters[1:5], 100, TRUE))

df2 <-  rbind(
    cbind(df, faceter = "Whole Sample")
    , cbind(df[df$sub == "a" ,], faceter = "Subset A")
    #other subsets go here...
)

qplot(x,y, data = df2) + facet_wrap(~ faceter)

Let me know if I've misunderstood your question.

-Chase

Upvotes: 3

Related Questions