ggpubr icon indicating copy to clipboard operation
ggpubr copied to clipboard

ylim & stat_compare_means

Open xiangrong7 opened this issue 3 years ago • 1 comments

Hello dear,I have a problem. I checked this #305 ,but it did not work .

my_comparisons <- list(c("1","2"), c("1", "3"),c("1", "4"),
                       c("2","3"),c("2", "4"),
                       c("3", "4")
                       )

ggboxplot(df, x = "Patient",y="Value",outlier.shape=NA,fill = "Patient")+
  #ylim(0,20)+
  stat_compare_means(method = "wilcox.test", comparisons = my_comparisons, 
                    # label.y=c( 20)
                     )

This will output following: image add ylim

ggboxplot(df, x = "Patient",y="Value",outlier.shape=NA,fill = "Patient")+
  ylim(0,20)+
  stat_compare_means(method = "wilcox.test", comparisons = my_comparisons, 
                     label.y=c( 20)
                     )

This will output following: image

There is not have p value in graph. Could you help me ?

xiangrong7 avatar Feb 16 '22 09:02 xiangrong7

hi, I have a similar problem, the label.y is not solving it.

plot_parameters <- function(parameter, y.label, y.low=0, y.high=100, stat.print.y = NA, df = dataset) { 
      df %>%
          select(kat_cas, nick, parameter) %>%
          pivot_longer(cols=parameter, names_to = "parameter", values_to = "values") %>%
          ggplot(aes(x=kat_cas, y=values))  + geom_boxplot(width=0.3) + 
          stat_compare_means(comparisons = compare_to_zero, 
                             method = "wilcox.test",
                             paired = TRUE,
                             label="p.signif", hide.ns = FALSE,
                             label.y = stat.print.y) +
      coord_cartesian(ylim=c(y.low-(y.high-y.low)/10, y.high), clip = "off")
}

With a call plot_parameters("IL.6", 'IL-6 (ng/L)', 0, 15000, stat.print.y = 10000, df = df.septic) I get the following result:

slika

However, playing aroung with plot_parameters("IL.6", 'IL-6 (ng/L)', 0, 50000, stat.print.y = 10000, df = df.septic) I get this result, which I cant explain slika

Thank you!

alejer avatar Mar 10 '22 13:03 alejer

The length of label.y should be the same as the number of brackets. If you have 5 brackets then label.y should contain 5 values, one position for each bracket. See examples below:

suppressPackageStartupMessages(library(ggpubr))

# Load data
data("ToothGrowth")
head(ToothGrowth)
#>    len supp dose
#> 1  4.2   VC  0.5
#> 2 11.5   VC  0.5
#> 3  7.3   VC  0.5
#> 4  5.8   VC  0.5
#> 5  6.4   VC  0.5
#> 6 10.0   VC  0.5

# Pairwise comparisons: Specify the comparisons you want
my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )
p <- ggboxplot(ToothGrowth, x = "dose", y = "len",
          color = "dose", palette = "npg")

  # Add pairwise comparisons p-value
p + stat_compare_means(comparisons = my_comparisons)


# Changing bracket y position
# you should specify position for each bracket
# We have 3 brackets so label.y should contain 3 values
p + stat_compare_means(comparisons = my_comparisons, label.y = c(29, 35, 40))

See also https://rpkgs.datanovia.com/ggpubr/reference/geom_pwc.html

kassambara avatar Nov 23 '22 23:11 kassambara