如何在R中重命名gam图中的x轴标签?

f4t66c6m  于 2023-02-20  发布在  其他
关注(0)|答案(1)|浏览(162)

我使用以下代码拟合模型

mod1 <- gam(severity ~  s(mean_rh, k = 8) + s(mean_temp, k = 10) + s(mean_ws, k =7) + s(avg_daily_rain, k = 7), family = betar(),  data = dat_seasonal)

下面是用于生成绘图的代码

plot(mod1, pages = 1, all.terms = TRUE, rug = TRUE, residuals = TRUE, pch = 1, cex = 1, shade = TRUE, seWithMean = TRUE, shift = coef(mod1)[1])

我想通过重命名原始数据框中的列名来重命名x轴标注,使用新名称=旧语法Mean temperature = mean_temp,Mean wind speed = mean_ws,Mean relative humidity = mean_rh,Mean rain per rainy day = avg_daily_rain
下面是我的代码

names(dat_seasonal) <- c("disease_severity", "Mean relative humidity", "Mean temperature", "Mean wind speed", "Mean rain per rainy day")

mod1 <- gam(disease_severity ~  s(`Mean relative humidity`, k = 8) + s(`Mean temperature`, k = 10) + s(`Mean wind speed`, k =7) + s(`Mean rain per rainy day`, k = 7), family = betar(),  data = dat_seasonal_gam)

summary(mod1)

plot(mod1, pages = 1, all.terms = TRUE, rug = TRUE, residuals = TRUE, pch = 1, cex = 1, shade = TRUE, seWithMean = TRUE, shift = coef(mod1)[1])

我收到以下错误消息

Error in str2lang(termtext) : <text>:1:6: unexpected symbol
1: Mean relative
         ^

显然,错误消息建议在新名称中不应该有任何空格。有没有解决这个问题的线索?谢谢
注意:我可以使用下面的代码重命名单个项,但无法一次重命名所有预测器。

plot(mod1, select = 4, xlab = "Mean rain per rainy day (mm)", rug = TRUE, residuals = TRUE, pch = 1, cex = 1, shade = TRUE, seWithMean = TRUE, shift = coef(mod1)[1])
umuewwlo

umuewwlo1#

最后使用gratia包重命名各个图,然后使用patchwork包将它们组合在一起。

p1 <- draw(mod1, select = "s(mean_rh)") +
      labs(x= "Mean relative humidity (%)", title = "")

p2 <- draw(mod1, select = "s(mean_temp)") +
   labs(x= "Mean temperature (°C)", title = "") 

p3 <- draw(mod1, select = "s(mean_ws)") +
  labs(x= "Mean wind speed (m/s)", title = "") 

p4 <- draw(mod1, select = "s(avg_daily_rain)") +
  labs(x= "Mean rain per rainy day (mm)", title = "") 

p1 + p2 + p3 + p4 + plot_layout(ncol = 2, nrow = 2)

相关问题