【问题标题】:Remove categorical x axis names from margins plot从边距图中删除分类 x 轴名称
【发布时间】:2020-03-22 22:12:05
【问题描述】:

我想为从 margins 包返回的平均边际效应输出一个图。变量位于 x 轴上,但它们的顺序不正确,并且未显示所有变量名称。我想以正确的顺序显示所有变量名称并旋转 90 度。这是我的代码:

margins_logit <- margins(LOGIT, variables=c("AGE", "AGE_SQRD", "LOGINCOME", "LOGINCOME_SQRD", "LOGSPEND", "LOGSPEND_SQRD"))

categories <- c(
                "AGE",
                "AGE_SQRD",
                "LOGINCOME",
                "LOGINCOME_SQRD",
                "LOGSPEND",
                "LOGSPEND_SQRD"
)

plot(margins_var, main="", xlab ="", xaxt='n', ann=TRUE)
axis(1, at=1:6, labels=categories, las = 2, cex.axis = 0.8)

我关注了这个:Remove plot axis values,但它似乎对我不起作用。

非常感谢!

本都

The vertical x categorical values are correct but the horizontal are in the wrong place and all categories are not displayed. I'd like to remove these completely and replace them with axis.

【问题讨论】:

    标签: r plot


    【解决方案1】:

    我对这个包没有经验。我刚刚尝试使用mtcars 数据来重现您作为学习者的问题。

    问题

    x <- glm(am ~ cyl + hp + disp, data = mtcars, family = binomial)
    
    margins_var <-margins(x, type = "response", 
                          variables = c("disp", "cyl", "hp"))
    categories <- c(
      "disp label",
      "cyl label",  
      "hp label"
    )
    
    plot(margins_var, main="", xlab = "",  xaxt='n', ann=FALSE)
    
    axis(1, at=1:3, labels=categories, las = 2, cex.axis = 0.8)
    [![enter image description here][1]][1]
    
    

    修复

    1. 我猜顺序可能是由原模型决定的。
    2. 要添加垂直标签,我们需要先移除水平标签。
    x <- glm(am ~ cyl + hp + disp, data = mtcars, family = binomial)
    
    margins_var <-margins(x, type = "response", 
                          variables = c("cyl", "hp", "disp"))
    categories <- c(
      "Cyl",
      "Hp",  
      "Disp"
    )
    plot(margins_var, main="", xlab = "", labels = c(" ", " ", " "))
    axis(1, at=1:3, labels=categories, las = 2, cex.axis = 0.8)
    
    

    【讨论】:

      【解决方案2】:

      “xaxt”适用于大多数绘图,但对于您有一个边距对象并且您在运行此 plot(margins_var..) 时调用 margins:::plot.margins。在 margins:::plot.margins 内部,他们实际上使用轴命令将标签放在上面,因此您会看到两次标签。

      我没有您的数据,所以我使用 mtcars 示例向您展示 2 个解决方法:

      library(margins)
      fit <- lm(mpg ~ cyl + qsec+vs+am+carb, data = mtcars)
      # the order you want them
      categories <- c("cyl","qsec","am","carb")
      margins_lm <- margins(fit,variables=categories)
      
      df <- summary(margins_lm)
      df <- df[match(categories,df$factor),]
      LIMITS <- range(c(df$AME+df$SE,df$AME-df$SE))
      plot(NULL,xlim=c(1,nrow(df)),ylim=LIMITS,xaxt='n',
      xlab="",ylab="Average Marginal Effect")
      with(df,points(1:nrow(df),AME,pch=20))
      with(df,segments(x0 = 1:nrow(df),y0=AME-SE,y1=AME+SE))
      axis(1, at=1:length(categories), labels=categories, 
      las = 2, cex.axis = 0.8)
      

      或者简单地使用上面的df,然后应用ggplot2:

      ggplot(df,aes(x=factor,y=AME)) + 
      geom_point() + 
      geom_segment(aes(y=lower,yend=upper,x=factor,xend=factor)) + 
      theme_bw() + 
      scale_x_discrete(limits=categories)
      

      【讨论】:

        猜你喜欢
        • 2013-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-31
        • 2016-07-10
        • 2019-04-19
        • 1970-01-01
        • 2023-04-05
        相关资源
        最近更新 更多