我正在使用精彩的
gratia
包,使用 mgcv
函数来可视化 compare_smooths
包中的多个 gam 模型。虽然我能够正确创建游戏并绘制它们,但我找不到将图例强制到图形底部的方法。
我的代码如下:
library(tidyverse)
library(gratia)
library(mgcv)
library(ggpubr)
gam1 <- gam(mpg ~ am + s(hp),data = mtcars |> filter(cyl >= 6))
gam2 <- gam(mpg ~ am + s(hp),data = mtcars |> filter(cyl < 6))
gam3 <- gam(mpg ~ am + s(hp),data = mtcars |> filter(qsec >= 18))
gam4 <- gam(mpg ~ am + s(hp),data = mtcars |> filter(qsec < 18))
plot1 <- draw(compare_smooths(gam1,gam2)) + theme(legend.position = "bottom")
plot2 <- draw(compare_smooths(gam3,gam4)) + theme(legend.position = "bottom")
gamplot <- ggarrange(plotlist = list(plot1,plot2),ncol = 2)
gamplot
这会产生以下结果:
尽管指定如此,但底部没有图例。我还尝试使用
ggarrange
中的图例位置,但这也不起作用:
gamplot2 <- ggarrange(plotlist = list(plot1,plot2),ncol = 2,legend = "bottom")
gamplot2
即使这些集合是由它们自己组成的,这也无济于事:
plot1
我想把两个图例都放在底部。我怎样才能做到这一点?在
gratia
内使用 compare_smooths
这可能吗,还是不可行?
gratis::draw
返回的绘图对象是patchwork
。因此,您可以使用 +
来代替 &
将主题修改应用于单个 ggplot
对象。
library(tidyverse)
library(gratia)
library(mgcv)
library(ggpubr)
gam1 <- gam(mpg ~ am + s(hp), data = mtcars |> filter(cyl >= 6))
gam2 <- gam(mpg ~ am + s(hp), data = mtcars |> filter(cyl < 6))
gam3 <- gam(mpg ~ am + s(hp), data = mtcars |> filter(qsec >= 18))
gam4 <- gam(mpg ~ am + s(hp), data = mtcars |> filter(qsec < 18))
plot1 <- draw(compare_smooths(gam1, gam2)) & theme(legend.position = "bottom")
plot2 <- draw(compare_smooths(gam3, gam4)) & theme(legend.position = "bottom")
gamplot <- ggarrange(plotlist = list(plot1, plot2), ncol = 2)
gamplot