我正在使用R中的coefplot::multiplot
用多个模型制作系数图;下图是我目前所拥有的。
这是我用来创建它的代码:
coefplot::multiplot(sc.mod.env.msrh, sc.mod.env.msrhmu, sc.mod.env.msrhat, sc.mod.env.msrhatmu,
coefficients=c("(Intercept)",'MeanSpeed', 'RH', 'MeanUpdraft', 'TKE','AirTemp'),
newNames=c(MeanSpeed='Horizontal Wind Speed', RH='Relative Humidity', MeanUpdraft='Vertical Wind Speed', AirTemp='Temperature'),
single=FALSE,
ncol=2,
names=c(sc.mod.env.msrhatmu="a) Global model w/ horizontal wind speed", sc.mod.env.tkerhatmu="b) Global model w/ TKE", sc.mod.env.msatmu="c) Global model w/ horizontal wind speed, \n RH removed", sc.mod.env.tkeatmu="d) Global model w/ TKE, \n RH removed"))+
theme_bw()+
theme(legend.position="none")+
ggtitle("")
我想用系数(例如温度)而不是模型对系数进行颜色编码,但是不知道如何编码。任何建议如何做到这一点表示赞赏。
答案 0 :(得分:0)
如果这对任何人都有用,我使用下面的代码使用ggplot创建了想要的图形;它可能不是最有效的方法,但它确实有效。我包括了另一个术语子模型,该子模型可让您分为两种类型的模型(下图中的圆形和三角形)。
首先创建一个函数,该函数从模型中提取所需的信息并将其存储在数据框中:
get_estimates_for_coefplot<- function(mod, time, modname){
test2<-data.frame(summary(mod)$coefficients)
test2['term']<-names(coef(mod))
test2['model']<-modname
test2['estimate']<-test2$Estimate
test2['submodel']<-time
test2['std.error']<-test2$Std..Error
test2['ub']<-test2$estimate+test2$std.error
test2['lb']<-test2$estimate-test2$std.error
Newdata <- test2 %>%
filter(!grepl(".*s.*",term))
return(Newdata)
}
然后将您要使用的所有模型发送到此功能并将它们绑定在一起:
df<-get_estimates_for_coefplot(sc.mod.env.msrhatmu, 'Time of day \n not included', 'a) Horizontal wind speed, \n RH included')
df<-rbind(df, get_estimates_for_coefplot(sc.mod.env.tkerhatmu, 'Time of day \n not included', 'b) TKE, RH included'))
modcoeff<-df[c('term','estimate','model','std.error','submodel','lb','ub')]
modcoeff <- modcoeff %>%
relabel_predictors(c(MeanSpeed = "Horizontal \n wind speed",
RH = "Relative \n Humidity",
AirTemp = "Temperature",
"I(hour^2)" = "Time of Day^2",
hour = "Time of Day",
MeanUpdraft = "Vertical \n wind speed",
TKE = "TKE"))
modcoeff$term<- factor(modcoeff$term, levels = c("Time of Day^2","Time of Day","Vertical \n wind speed","Temperature", "Relative \n Humidity","TKE", "Horizontal \n wind speed"))
modcoeff$submodel<- factor(modcoeff$submodel, levels = c('Time of day \n not included', 'Time of day \n included'))
这时,您应该有一个数据框,其中包含模型系数以及您需要的其他所有内容,包括lb和ub,它们是制作误差线的上限和下限。现在使用ggplot进行绘图。
pd <- position_dodge(width=0.5)
ggplot(modcoeff,
aes(x=term,
y=estimate,
color=term,
group=std.error,
ymin=lb,
ymax=ub)) +
geom_hline(yintercept = 0, color='darkgrey') +
geom_point(aes(shape=submodel),size=2.7, position=pd, stat="identity") +
geom_errorbar(aes(width=0.2),position=pd) +
facet_wrap(~model)+
xlab("")+
ylab('Standardised coefficient value')+
theme_bw()+
theme(legend.position = c(0.83, 0.25))+
theme(legend.title = element_blank())+
guides(colour=FALSE)+
coord_flip()
这将产生以下情节: