0

I want to exclude trend lines from the following three facets:

Lawn;2014
Tussock;2013
Tussock;2015

I have tried to use subset() but I cannot figure out how to drop or exclude specific observations within geom_smooth().

plot <- ggplot(data, aes(x=CWD, y=Amount)) + geom_point(aes(colour = Year),size=2) + 
geom_smooth(data = subset(data, Year =="2014"),method = "lm", formula = y ~ splines::bs(x, 3), color = "black",se = FALSE) +
facet_grid(CoverType~Year,margins=FALSE,scales="free_x",space = "fixed")

This is what the graph currently looks like: enter image description here

Dominique
  • 107
  • 2
  • 13

1 Answers1

0

This is a bit ugly but should give the desired outcome

plot <- ggplot(data, aes(x=CWD, y=Amount)) + geom_point(aes(colour = Year),size=2) + 
geom_smooth(data = data[-which(data$CoverType == "Lawn" & data$Year == "2014" | data$CoverType == "Tussock" & data$Year %in% c("2013","2015")),] ,method = "lm", formula = y ~ splines::bs(x, 3), color = "black",se = FALSE) +
facet_grid(CoverType~Year,margins=FALSE,scales="free_x",space = "fixed")

Alternatively you can start by making a dummy variable

data$dum<-ifelse(data$CoverType == "Lawn" & data$Year == "2014" | data$CoverType == "Tussock" & data$Year %in% c("2013","2015"),1,0)

And then create the plot with

plot <- ggplot(data, aes(x=CWD, y=Amount)) + geom_point(aes(colour = Year),size=2) + 
geom_smooth(data = subset(data, dum==0) ,method = "lm", formula = y ~ splines::bs(x, 3), color = "black",se = FALSE) +
facet_grid(CoverType~Year,margins=FALSE,scales="free_x",space = "fixed")
Niek
  • 1,594
  • 10
  • 20
  • Thanks for this! It works but unfortunately gives me the opposite of what I want. I want to EXCLUDE those three facets but have trend lines for all the others. – Dominique Mar 16 '17 at 10:53
  • Sorry, misread the question. The easiest solution is to change dum == 1 to dum == 0 . I'll edit my answer to give the correct solution. Could you accept it if it is an answer to your problem? – Niek Mar 16 '17 at 11:00