我正在尝试通过具有20个方面的图形来拟合线性回归(实际上是9个)。每次我拟合回归时(使用方法= lm使用geom_smooth),它都适合20条线,每个面一个,但是我希望每种ReefSpecies组合的一条线都可以遍历所有20个面。
这是我的身影:
这是我到目前为止的内容:
Biomass <- c(20, 10, 5, 4, 5, 7, 8, 22, 13, 13, 15, 18, 2, 5, 7, 10)
Season <- c("Winter", "Spring", "Summer", "Fall")
Year <- c("1", "2", "3", "4")
ReefSpecies <- c("Admiral Ma", "Jaap Mf", "Grecian Ma", "Alligator Mf", "Jaap Mf", "Grecian Ma", "Alligator Mf", "Admiral Ma", "Grecian Ma", "Alligator Mf", "Admiral Ma", "Jaap Mf", "Alligator Mf", "Admiral Ma", "Jaap Mf","Grecian Ma")
Seasonal <- data.frame(Biomass, Season, Year, ReefSpecies)
testp <- ggplot(data = Seasonal, aes(x = Season, y = Biomass, group = ReefSpecies, fill = ReefSpecies, colour = ReefSpecies))
testp <- testp + geom_point(stat = "identity", position="identity", inherit.aes = TRUE)
testp <- testp + facet_grid(. ~ Year, scales="fixed")
testp <- testp + theme(axis.text.x = element_text(angle = 90))
testp <- testp + theme(panel.margin.x = unit(0, "lines"))
testp <- testp + theme(legend.position = "top")
testp
根据意见,你不想要放置在ggplot的每个面相同的平滑(您可以通过做的小面变量设置为NULL
在光滑。
您想要做的是在所有方面进行单一回归。我认为如果没有此处所示的黑客攻击,这是不可能的。您可以尝试一下。
但是,相反,我建议您退一步考虑为什么要这样做以及平滑意味着什么。也许这意味着刻面不是正确的选择?在这种情况下,您可以考虑定义一个Time
变量,该变量说明跨年的季节并以此为基础进行回归(无方面)。
一个示例(具有经过调整的数据,因为您的示例数据每年最多观察一次):
Year <- sort(rep(Year, 4))
Seasonal <- data.frame(Biomass, Season, Year, ReefSpecies)
Seasonal$Time <- interaction(Season, Year)
ggplot(Seasonal, aes( Time, Biomass, color=ReefSpecies)) +
geom_point() +
geom_smooth(aes(group=ReefSpecies), method="lm")
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句