如何将几何平滑线添加到构面网格但排除 R 中的特定构面?
How do I add geom-smooth lines to a facet grid but excluding specific facets in R?
我想从以下三个方面排除趋势线:
草坪;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")
这是图表当前的样子:
这有点难看,但应该会给出预期的结果
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")
我想从以下三个方面排除趋势线:
草坪;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")
这是图表当前的样子:
这有点难看,但应该会给出预期的结果
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")