我想从以下三个方面排除趋势线:
草坪; 2014
草丛; 2013
草丛; 2015
我曾尝试使用subset(),但我无法弄清楚如何在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")
答案 0 :(得分:0)
这有点难看,但应该给出理想的结果
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")
或者你可以先制作一个虚拟变量
data$dum<-ifelse(data$CoverType == "Lawn" & data$Year == "2014" | data$CoverType == "Tussock" & data$Year %in% c("2013","2015"),1,0)
然后用
创建情节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")