ggplot2:将p值添加到图中 [英] ggplot2: add p-values to the plot

查看:3211
本文介绍了ggplot2:将p值添加到图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了这个阴谋



使用

  library(dplyr)
library(ggplot2)
library(ggpmisc)

df < - diamonds%>>%
dplyr :: filter(cut%in%c(Fair,Ideal))%>%
dplyr :: filter (清晰度以%c为单位(I1,SI2,SI1,VS2,VS1,VVS2))%>%
dplyr :: mutate(new_price = ifelse(cut ==公平,
价格* 0.5,
价格* 1.1))

公式< - y〜x
ggplot(df,aes(x = new_price,y = carat,color = cut))+
geom_point(alpha = 0.3)+
facet_wrap(〜clarity,scales =free_y)+
geom_smooth(method =lm ,公式=公式,se = F)+
stat_poly_eq(aes(label = paste(.. rr.label ..)),
label.x.npc =righ t,label.y.npc = 0.15,
formula = formula,parse = TRUE,size = 3)

除了R2之外,我还想将p值添加到方面。我可以通过首先运行回归然后获取p值并使用 geom_text()来添加这些p值


I got this plot

Using the code below

library(dplyr) 
library(ggplot2)
library(ggpmisc)

df <- diamonds %>%
  dplyr::filter(cut%in%c("Fair","Ideal")) %>%
  dplyr::filter(clarity%in%c("I1" ,  "SI2" , "SI1" , "VS2" , "VS1",  "VVS2")) %>%
  dplyr::mutate(new_price = ifelse(cut == "Fair", 
                                   price* 0.5, 
                                   price * 1.1))

formula <- y ~ x    
ggplot(df, aes(x= new_price, y= carat, color = cut)) +
  geom_point(alpha = 0.3) +
  facet_wrap(~clarity, scales = "free_y") +
  geom_smooth(method = "lm", formula = formula, se = F) +
  stat_poly_eq(aes(label = paste(..rr.label..)), 
               label.x.npc = "right", label.y.npc = 0.15,
               formula = formula, parse = TRUE, size = 3)

In addition to R2, I want to add p-values to the facets as well. I can do this manually through running the regression first then getting p-values and using geom_text() to add these p-values similar to the answer of this question.

Is there any faster or automated way to do that? e.g. similar to the way R2 values have been added.

Update

The p-value I'm talking about is the slope p-value. The trends are considered highly statistically significant when p < 0.005.

解决方案

Use stat_fit_glance which is part of the ggpmisc package in R. This package is an extension of ggplot2 so it works well with it.

ggplot(df, aes(x= new_price, y= carat, color = cut)) +
       geom_point(alpha = 0.3) +
       facet_wrap(~clarity, scales = "free_y") +
       geom_smooth(method = "lm", formula = formula, se = F) +
       stat_poly_eq(aes(label = paste(..rr.label..)), 
       label.x.npc = "right", label.y.npc = 0.15,
       formula = formula, parse = TRUE, size = 3)+
       stat_fit_glance(method = 'lm',
                       method.args = list(formula = formula),
                       geom = 'text',
                       aes(label = paste("P-value = ", signif(..p.value.., digits = 4), sep = "")),
       label.x.npc = 'right', label.y.npc = 0.35, size = 3)

stat_fit_glance basically takes anything passed through lm() in R and allows it to processed and printed using ggplot2. This website has the rundown of some of the functions like stat_fit_glance: http://rpackages.ianhowson.com/cran/ggpmisc/ . Also I believe this gives model p-value, not slope p-value (in general), which would be different for multiple linear regression. For simple linear regression they should be the same though.

Here is the plot:

这篇关于ggplot2:将p值添加到图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆