I would like to restrict the end of the y-axis without restricting the range of the shown data in this plot on the y-axis
from 0
to 32
. Specifically, the axis should not be shown for higher values but the significance comparison should be included and the part in red should not be shown.
library(ggpubr)
data("ToothGrowth")
head(ToothGrowth)
# Visualize: Specify the comparisons you want
my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )
ggboxplot(ToothGrowth, x = "dose", y = "len",
color = "dose", palette = "jco")
stat_compare_means(comparisons = my_comparisons) # Add pairwise comparisons p-value
stat_compare_means(label.y = 50) # Add global p-value
CodePudding user response:
Well, I tried. And it's not perfect - but it is a start ;)
library(ggpubr)
data("ToothGrowth")
head(ToothGrowth)
# Visualize: Specify the comparisons you want
my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )
ggboxplot(ToothGrowth, x = "dose", y = "len",
color = "dose", palette = "jco")
scale_y_continuous(breaks = c(10,20,30), limits = c(0,50))
theme(axis.line.y = element_line(size = 1, colour = "white"))
stat_compare_means(comparisons = my_comparisons) # Add pairwise comparisons p-value
stat_compare_means(label.y = 50, label.x = 0.3) # Add global p-value
geom_rect(aes(xmin=0,xmax=0.02,ymin=0,ymax=30), size=0, fill="black")
CodePudding user response:
You may try below solution. It limits the y-axis to 35, then tells ggplot to not 'clip' the rest of the data, and to show the annotations that are outside the range of c(0,35), we expand the margins of the plot. Then re-position the legend to your needs. Modify according to your needs:
library(ggpubr)
data("ToothGrowth")
head(ToothGrowth)
# Visualize: Specify the comparisons you want
my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )
ggboxplot(ToothGrowth, x = "dose", y = "len",
color = "dose", palette = "jco")
stat_compare_means(comparisons = my_comparisons) # Add pairwise comparisons p-value
stat_compare_means(label.y = 50)
coord_cartesian(clip = "off", ylim = c(0, 35))
theme(plot.margin = unit(c(8,0,0,0), "cm"),
legend.position = c(.5,1.5),
legend.direction = "horizontal")
CodePudding user response:
You can try guide_axis_truncated()
from the ggh4x package. Disclaimer: I'm the author of ggh4x.
library(ggpubr)
#> Warning: package 'ggpubr' was built under R version 4.1.1
#> Loading required package: ggplot2
data("ToothGrowth")
library(ggh4x)
# Visualize: Specify the comparisons you want
my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )
ggboxplot(ToothGrowth, x = "dose", y = "len",
color = "dose", palette = "jco")
stat_compare_means(comparisons = my_comparisons) # Add pairwise comparisons p-value
stat_compare_means(label.y = 50)
scale_y_continuous(
breaks = function(x) {
x <- scales::extended_breaks()(x)
x[x < 32]
},
guide = guide_axis_truncated(trunc_lower = -Inf, trunc_upper = 32)
)
#> Warning in wilcox.test.default(c(4.2, 11.5, 7.3, 5.8, 6.4, 10, 11.2, 11.2, :
#> cannot compute exact p-value with ties
#> Warning in wilcox.test.default(c(4.2, 11.5, 7.3, 5.8, 6.4, 10, 11.2, 11.2, :
#> cannot compute exact p-value with ties
#> Warning in wilcox.test.default(c(16.5, 16.5, 15.2, 17.3, 22.5, 17.3, 13.6, :
#> cannot compute exact p-value with ties
Created on 2021-09-24 by the reprex package (v2.0.1)