我试图通过一个有20个方面的数字拟合线性回归(实际上有9个)。每次我适合回归(使用geom_smooth使用method = lm),它适合20行,每个facet一个,但我希望每个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
答案 0 :(得分:1)
根据评论,您不想要place an identical smooth on each facet of a ggplot(您可以setting the faceting variable to NULL
in the smooth执行此操作。
做想要的是对所有方面进行单一回归。我认为如果没有一些黑客like that shown here,这是不可能的。你可以试试。
但相反,我建议您回过头来考虑为什么要这样做以及平稳意味着什么。也许这意味着方面不是正确的选择?在这种情况下,您可以考虑定义一个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")