【发布时间】:2016-08-06 04:20:12
【问题描述】:
我试图通过一个有 20 个方面的图形来拟合线性回归(实际上是 9 个)。每次我拟合回归时(使用 geom_smooth,使用 method = lm),它拟合 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
【问题讨论】:
-
请让您的问题可重现。
-
在这方面,请提供一些数据,以便我们重现您的问题。
-
ggplotGrob 的东西和所有主题元素都与您的问题无关,只会让帖子看起来很困难。
geom_point()里面的东西也是没必要的。我建议您使用任何线来拟合回归(stat_smooth 或其他东西?),您在该层内专门设置group美学。请注意,在您最初的ggplot()调用中,您如何设置将由所有其他层继承的分组和着色美学,除非您另有指定。你可以添加类似+geom_smooth(method = "lm", aes(group=1)) -
编辑添加了一个小的模拟数据集!谢谢!
-
This question/answer 可能密切相关。