代码之家  ›  专栏  ›  技术社区  ›  owl

根据R中的p值绘制p值

  •  0
  • owl  · 技术社区  · 7 年前

    我正试图在R中的回归图上绘制r2和p值,借助于 this page . 这是我的密码。

    DF <- data.frame(X <- c(1, 2, 3, 4, 5, 6, 7), Y <- c(1.5, 2.1, 1.2, 4.4, 1, 6.5, 8.4))
    
    plot(Y ~ X, data = DF)
    
    # Add regression line
    regline <- lm(DF$Y ~ DF$X)
    intercept <- coef(regline)[1]
    slope <- coef(regline)[2]
    abline(regline)
    
    # Get stats
    summary(regline)
    
    # Get these values
    names(summary(regline))
    # Get adjusted R-square
    R2 <- summary(regline)$adj.r.squared
    
    # Get pPvalue
    P <- summary(regline)$coefficients[2,4]
    P <- ifelse(P < 0.05, '< 0.05', P)
    
    # Plot R2 and P-value
    r2p = vector('expression', 2)
    r2p[1] = substitute(expression(italic(R)^2 == RSQ), list(RSQ = format(R2, dig = 2)))[2]
    r2p[2] = substitute(expression(italic(P) == PVALUE), list(PVALUE = format(P, digits = 2)))[2]
    legend('topleft', legend = r2p, bty = 'n', y.intersp = 1.3)
    

    我想做的不同是,如果p值小于0.05,将p值更改为“p<0.05”。我可以转换该值,但它仍然像这样输出“=”符号。

    enter image description here

    如何在保持斜体P的同时打印“P<0.05”而不是“P=<0.05”?谢谢你的帮助。

    1 回复  |  直到 7 年前
        1
  •  2
  •   Darren Tsai    7 年前

    修改表达式 r2p[2] 以下表格之一:

    • expression(italic(P) ~ PVALUE)
    • expression(italic(P) * PVALUE)
    • expression(paste(italic(P), PVALUE))

    也就是说,把上面的代码放到这个地方 xxxxxx :

    r2p[2] = substitute(xxxxxx, list(PVALUE = format(P, digits = 2)))[2]
    

    enter image description here

    请检查 ?plotmath demo(plotmath) 详情。