Comments (1)
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)
- Integrate `latex2exp::TeX` into `add_pvalue`? HOT 4
- Cant hide legend HOT 1
- adding scale_y_log10 changes y-axis to a weird range HOT 1
- Planned features
- Issues with next version of ggplot2 HOT 2
- Can't seem to adjust x-position for brackets in interaction plots with add_pvalue HOT 1
- Release ggprism 1.0.4
- Unable to display legend title HOT 2
- The `legend.text.align` theme element is not defined in the element hierarchy. HOT 6
- theme_prism() not working with ggplot2 HOT 2
- Release ggprism 1.0.5
- adding new themes HOT 1
- An "i" in the label string is forced to be capitalized.
- Error on add_pvalue with geom_jitter() aes HOT 2
- Release ggprism 1.0.1 HOT 1
- Release ggprism 1.0.2
- hide.NS option for add_pvalue function HOT 4
- Issue with the next release of ggplot2 HOT 1
- Release ggprism 1.0.3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ggprism.