使用ggboxplot定位选定的p值

时间:2019-06-05 01:28:27

标签: r ggplot2 boxplot p-value ggpubr

我以mtcars数据集为例来运行以下代码。

library(ggplot2)
library(ggpubr)
ggboxplot(mtcars, x = "cyl", y = "drat", fill = "cyl", 
          facet.by = "am", width = 0.5, outlier.shape = NA, 
          bxp.errorbar = TRUE, bxp.errorbar.width = 0.2) + 
  stat_compare_means(aes(label = ifelse(p < 1.e-4, 
                                        sprintf("p = %2.1e", as.numeric(..p.format..)), 
                                        sprintf("p = %5.4f", as.numeric(..p.format..)))), 
                     method = "wilcox.test", paired = FALSE)

我无法修改此设置以实现以下目的。在每个方面,我只想添加两个wilcox.test p值,一个将cyl=4cyl=6进行比较,另一个将cyl=4cyl=8进行比较。然后,我希望这些p值分别位于cyl=6cyl=8框的上方。

我喜欢使用ggsignif包,但是我需要将此示例推断为9个比较,这使ggsignif不太适合(比较条占用太多空间)。

谢谢您的任何建议。

1 个答案:

答案 0 :(得分:4)

您想将其他组与“参考”组cyl=4进行比较。如果您将ref.group的参数stat_compare_means使用到ggboxplot(mtcars, x = "cyl", y = "drat", fill = "cyl", facet.by = "am", width = 0.5, outlier.shape = NA, bxp.errorbar = TRUE, bxp.errorbar.width = 0.2) + stat_compare_means( aes(label = ifelse(p < 1.e-4, sprintf("p = %2.1e", as.numeric(..p.format..)), sprintf("p = %5.4f", as.numeric(..p.format..)))), ref.group = "4", method = "wilcox.test", paired = FALSE ) ,它将为您提供所需的内容:

var flagclock = 0;
var flagstop = 1;
var stoptime = 0;
var currenttime;
var splitdate = '';
var output;
var clock;
var thetable = document.getElementById("timerTable");
// Adjust limits and colors here
// Specify limits in seconds
var limit1 = 0;
color1 = "lightgreen";
var limit2 = 360;
color2 = "orange";
var limit3 = 600;
color3 = "red";

function startstop() {
  var startdate = new Date();
  var starttime = startdate.getTime();
  if (flagclock == 0) {
    startstop.value = 'Stop';
    flagclock = 1;
    counter(starttime);
  } else {
    startstop.value = 'Start';
    flagclock = 0;
    flagstop = 1;
    splitdate = '';
  }
}

function counter(starttime) {
  output = document.getElementById('output');
  clock = document.getElementById('clock');
  currenttime = new Date();
  var timediff = currenttime.getTime() - starttime;
  if (flagstop == 1) {
    timediff = timediff + stoptime
  }
  if (flagclock == 1) {
    clock.value = formattime(timediff, '');
    refresh = setTimeout('counter(' + starttime + ');', 100);
    var secs = timediff / 1000;
    var thecolor = "white";
    if (secs > limit3) thecolor = color3;
    else if (secs > limit2) thecolor = color2;
    else if (secs > limit1) thecolor = color1;
    thetable.style.backgroundColor = thecolor;
    console.log(timediff / 1000)
  } else {
    window.clearTimeout(refresh);
    stoptime = timediff;
  }
}

function formattime(rawtime, roundtype) {
  if (roundtype == 'round') {
    var ds = Math.round(rawtime / 100) + '';
  } else {
    var ds = Math.floor(rawtime / 100) + '';
  }
  var sec = Math.floor(rawtime / 1000);
  var min = Math.floor(rawtime / 60000);
  ds = ds.charAt(ds.length - 1);
  if (min >= 15) {
    startstop();
  }
  sec = sec - 60 * min + '';
  if (sec.charAt(sec.length - 2) != '') {
    sec = sec.charAt(sec.length - 2) + sec.charAt(sec.length - 1);
  } else {
    sec = 0 + sec.charAt(sec.length - 1);
  }
  min = min + '';
  if (min.charAt(min.length - 2) != '') {
    min = min.charAt(min.length - 2) + min.charAt(min.length - 1);
  } else {
    min = 0 + min.charAt(min.length - 1);
  }
  return min + ':' + sec;
}