Santiago99
Santiago99

Reputation: 163

How to display statistical significance and the mean score using ggstatsplot

I have the following R code:

##Installe the package
install.packages("ggstatsplot")
library(ggstatsplot)

## Create data
df <- data.frame(replicate(2,sample(0:10,100,rep=TRUE)),
group=sample(c("A", "B", "C", "D", "F"), size=100, replace=TRUE))

##Code
ggbetweenstats(
  data = df,
  x = group,
  y = X1,
  plot.type = "box", # for boxplot
  pairwise.comparisons = TRUE,
  type = "nonparametric", # for wilcoxon
  centrality.plotting = TRUE, # remove median
  sphericity.correction = FALSE,
  pairwise.display = "significant",
  title = "Title"
) +
  ggplot2::scale_y_continuous(
    limits = c(0, 10),
    breaks = seq(from = 0, to = 10, by = 1)
  ) 

which produces this picture Picture

I would like to display the difference between the groups with a line over bars if the difference is statistically significant. I also would like to show the mean score with a dashed line. I tried a few codes but none of them worked.

Upvotes: 0

Views: 803

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 173998

There are two potential issues here. The first, and most obvious, is that your sample data doesn't have any significant pairwise differences, so none are drawn. The second is that by adding the scale_y_continuous, you are cutting the lines off even if they were there. If we pass a sample data set that has significant differences and remove the scale_y_continuous, the comparisons are there:

library(ggstatsplot)

set.seed(1)

## Create data
df <- data.frame(X1 = rnorm(100, rep(c(4, 5.1, 5, 4.9, 6), each = 20)),
                 group = rep(LETTERS[1:5], each = 20))

##Code
ggbetweenstats(
  data = df,
  x = group,
  y = X1,
  plot.type = "box", # for boxplot
  pairwise.comparisons = TRUE,
  pairwise.display = "significant",
  type = "nonparametric", # for wilcoxon
  centrality.plotting = TRUE, # remove median
  sphericity.correction = FALSE,
  title = "Title"
) 

enter image description here

Upvotes: 1

Related Questions