首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在R coefplot::multiplot中按系数而不是模型进行着色

如何在R coefplot::multiplot中按系数而不是模型进行着色
EN

Stack Overflow用户
提问于 2019-05-13 11:10:07
回答 1查看 770关注 0票数 0

我正在使用R中的coefplot::multiplot绘制多个模型的系数图;下面的图像是我目前拥有的。

这是我用来创建它的代码:

代码语言:javascript
复制
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("")

我希望通过变量(例如,温度)而不是模型来对系数进行颜色编码,但不知道怎么做。任何关于如何做到这一点的建议都将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2019-05-15 07:57:20

如果这对任何人都有用,我已经使用下面的代码使用ggplot创建了我想要的图形;这可能不是最有效的方法,但它很有效。我已经包含了另一个术语子模型,它允许您将模型分成两种类型(下图中的圆形和三角形)。

首先创建一个从模型中提取所需信息并将其存储在dataframe中的函数:

代码语言:javascript
复制
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)
}

然后将您希望使用的所有模型发送到此函数,并将它们绑定在一起:

代码语言:javascript
复制
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绘制绘图。

代码语言:javascript
复制
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()

这将生成以下图:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56105328

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档