Coder Social home page Coder Social logo

Comments (1)

csdaw avatar csdaw commented on August 24, 2024

Without a reproducible example it's a little hard to know that exact problem. And ultimately I think this is an issue with the rstatix::add_y_position() function so you might like to raise an issue in the rstatix repo instead.

However, I propose a solution below. Hopefully this helps.

library(ggplot2)

# PROBLEM
# make fake grouped data with similar means
set.seed(2022)
df1 <- data.frame(
  x = rep(paste0("group", 1:3), each = 100),
  y = rnorm(300, mean = 0, sd = 5)
)

# perform the stat test and move right bracket down
stat.test1 <- rstatix::wilcox_test(df1, y ~ x)
stat.test1 <- rstatix::add_y_position(stat.test1)
stat.test1$y.position[3] = stat.test1$y.position[1]

# plot (brackets are a bit low but spacing is ok)
ggplot(df1, aes(x = x, y = y)) + 
  geom_boxplot(fill = "grey80") + 
  geom_jitter() + 
  theme_bw() + 
  ggprism::add_pvalue(stat.test1)

# make fake grouped data with different means
df2 <- data.frame(
  x = rep(paste0("group", 1:3), each = 100),
  y = c(rnorm(100, mean = 0, sd = 5), rnorm(200, mean = 30, sd = 5))
)

# perform the stat test and move right bracket down
stat.test2 <- rstatix::wilcox_test(df2, y ~ x)
stat.test2 <- rstatix::add_y_position(stat.test2)
stat.test2$y.position[3] = stat.test2$y.position[1]

# plot (brackets are high enough but spacing is too far)
ggplot(df2, aes(x = x, y = y)) + 
  geom_boxplot(fill = "grey80") + 
  geom_jitter() + 
  theme_bw() + 
  ggprism::add_pvalue(stat.test2)

# SOLUTION:
# write a function to produce more reasonable y positions
make_ypos <- function(df) {
  max_y <- max(df$y) # get highest point
  min_y <- min(df$y) # get lowest point
  range_multiplier <- (max_y - min_y) / 10 # get 10% of the y axis range
  
  # output the bracket y positions
  # (bottom left, top, bottom right)
  out <- c(max_y + range_multiplier, max_y + range_multiplier * 1.5, max_y + range_multiplier)
  out
}

# override y.position with a vector of your own calculated values
# works well in both example situations
ggplot(df1, aes(x = x, y = y)) + 
  geom_boxplot(fill = "grey80") + 
  geom_jitter() + 
  theme_bw() + 
  ggprism::add_pvalue(stat.test1, y.position = make_ypos(df1))

ggplot(df2, aes(x = x, y = y)) + 
  geom_boxplot(fill = "grey80") + 
  geom_jitter() + 
  theme_bw() + 
  ggprism::add_pvalue(stat.test2, y.position = make_ypos(df2))

Created on 2022-01-17 by the reprex package (v2.0.1)

from ggprism.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.