Geoffrey
Geoffrey

Reputation: 196

ggplot combined stat_smooth for some factor levels in R

My data has 3 factor levels, each with an x and y coordinate. I'm using ggplot to create a scatter plot, to which I'd like to add a smooth. However, I want the smooth only to use 2 of the 3 factor levels. The result should be a single fit of the data points in those 2 factor levels, but with all 3 levels scattered.

Example:

library(ggplot2)
set.seed(123)
data <- data.frame(x=rnorm(300), y=rnorm(300), f=rep(c("a","b","c"), 100))

ggplot(data, aes(x,y,color=f)) +
  geom_point() +
  stat_smooth(method="gam", formula=y~s(x), se=F)

ggplot(data[data$f %in% c("a","b"),], aes(x,y)) +
  geom_point(aes(color=f)) +
  stat_smooth(method="gam", formula=y~s(x), se=F)

enter image description here enter image description here

As you can see, the first image has smooths for all factors individually. The second combines data from factor levels "a" and "b", but that's because I've removed "c" from the data.

How can I retain the scatterplot from the first image and overlay the smooth fit from the second?

Upvotes: 2

Views: 1915

Answers (1)

lukeA
lukeA

Reputation: 54277

stat_smooth has a data argument, which you can use to build a subset:

ggplot(data, aes(x,y,color=f)) +
  geom_point() +
  stat_smooth(data=subset(data, f!="c"), 
              inherit.aes=F, aes(x, y),
              method="gam", formula=y~s(x), 
              se=F)

In addition, you can use inherit.aes=FALSE to change the aesthetics mapping.

Upvotes: 2

Related Questions