【问题标题】:display nonsignificant coefficients with ggplot2 and legend display用 ggplot2 和图例显示显示不显着的系数
【发布时间】:2020-11-29 01:14:55
【问题描述】:

我试图为不重要的系数 (p>0.05) 和重要的系数显示不同的颜色。另外,如果有人有办法显示图例或表示颜色,那也不错..

有什么想法吗?

示例代码:

library(nycflights13)
library(dplyr)
library(dotwhisker)
library(MASS)

flights <- nycflights13::flights
flights<- sample_n (flights, 500)

m1<- glm(formula = arr_delay ~ dep_time + origin+ air_time+ distance , data = flights)
#m1<- glm(formula = arr_delay ~ . , data = flights)

m1<- stepAIC(m1)
  p<- dotwhisker::dwplot(m1)
  z<- p + 
    geom_vline(xintercept=0, linetype="dashed")+
    geom_segment(aes(x=conf.low,y=term,xend=conf.high,
                     yend=term,col=p.value<0.05)) + 
    geom_point(aes(x=estimate,y=term,col=p.value<0.05)) +
  xlab("standardized coefficient") + 
  ylab("coefficient") +
  ggtitle("coefficients in the model and significance")
  print(z)

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您的代码已经某种可以满足您的需求。问题是由dwplot 生成的对象p 已经有一个geom_segment 层和一个带有许多美学映射的geom_point 层。它们的颜色当前映射到变量model,这只是一个因素水平,允许在并排比较模型时使用不同的颜色。但是可以覆盖它们:

    p$layers[[1]]$mapping[5] <- aes(color = p.value < 0.05)
    p$layers[[2]]$mapping[4] <- aes(color = p.value < 0.05)
    

    您可以使用

    更改图例标签
    p$labels$colour <- "Significant"
    

    默认情况下,dwplot 也会隐藏图例,但我们可以使用以下命令重置它:

    p$theme <- list()
    

    因此,无需添加任何新几何图形或创建对象z,我们就有:

    p
    

    请注意,p 仍然是有效且内部一致的 ggplot,因此您可以继续根据需要对其进行样式设置,例如:

    p + theme_bw() + geom_vline(xintercept = 0, lty = 2)
    

    【讨论】:

    • 嗨回到这个图表来实现这个。是否可以将图例更改为重要和不重要?
    猜你喜欢
    • 2023-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多