英文:
How to rename x-axis labels in gam plot in R?
问题
我使用以下代码拟合了模型:
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)
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])
英文:
I fitted the model using the following code
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)
Here is the code used in producing plot
plot(mod1, pages = 1, all.terms = TRUE, rug = TRUE, residuals = TRUE, pch = 1, cex = 1, shade = TRUE, seWithMean = TRUE, shift = coef(mod1)[1])
I wanted to rename x-axis labels by renaming column names in the original data frame, using new name = old syntax, Mean temperature
= mean_temp,
Mean wind speed
= mean_ws,
Mean relative humidity
= mean_rh,
Mean rain per rainy day
= avg_daily_rain
Here is my code
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])
I am getting the following error message
Error in str2lang(termtext) : <text>:1:6: unexpected symbol
1: Mean relative
^
Apparently, the error message suggests that there should not be any spaces in the new names. Any leads on how to fix this? Thanks
NOTE:
I can rename a single term using the following code, but there is no way rename all predictors at once.
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])
答案1
得分: 1
使用gratia
包重命名各个图并使用patchwork
包组合它们。
英文:
Ended up using gratia
package to rename individual figures and then combine them using patchwork
package.
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论