Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2: add p-values to the plot

Tags:

r

ggplot2

p-value

I got this plot

enter image description here

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.

like image 927
shiny Avatar asked May 31 '16 04:05

shiny


People also ask

How do I add p values to a ggplot in R?

Add manually p-values to a ggplot: stat_pvalue_manual () [in ggpubr package] This function can be used to add manually p-values to a ggplot, such as box blots, dot plots, stripcharts, line plots and bar plots.

How do you add a p value to a box plot?

Add p-values onto grouped box plots, bar plots and line plots. Examples, containing two and three groups by x position, are shown. We will follow the steps below for adding significance levels onto a ggplot: Auto-compute p-value label positions using the function add_xy_position () [in rstatix package].

How to add p-values to a plot using ggpubr?

Add the p-values to the plot using the function stat_pvalue_manual () [in ggpubr package]. The following key options are illustrated in some of the examples: The option bracket.nudge.y is used to move up or to move down the brackets. The option step.increase is used to add more space between brackets.

What is Stat_compare_means in ggplot?

stat_compare_means () This function extends ggplot2 for adding mean comparison p-values to a ggplot, such as box blots, dot plots, bar plots and line plots. The simplified format is as follow: stat_compare_means(mapping = NULL, comparisons = NULL hide.ns = FALSE,


Video Answer


1 Answers

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. The user-guide has the rundown of some of the functions like stat_fit_glance: https://cran.r-project.org/web/packages/ggpmisc/vignettes/user-guide.html. 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:

enter image description here

like image 51
akash87 Avatar answered Oct 26 '22 21:10

akash87