rggplot2

Change position of stat_compare_means() in the plot using ggplot


I have this ggplot figure with ggpubr::stat_compare_means() function: enter image description here

As you can see, the Wilcoxon test overlaps some dots on the left bar.

How can I move the text to the right?


Solution

  • As suggested by @Mikolajm, you can use the label.x and label.y arguments in the stat_compare_means() function to position your text. The label.x argument changes the position of the text on the x-axis, and the label.y argument changes the position of the text on the y-axis.

    For example (which logically doesn't make sense), I used the mtcars dataset.

    require(ggplot2)
    require(ggpubr)
    
    
    ggboxplot(mtcars, x = "am", y = "carb",
              color = "am") +
      stat_compare_means(method = "wilcox.test",
                         label.x = 1.2, 
                         label.y = 10)
    

    enter image description here

    However, if we'll change the label.x = 1.2 and label.y = 10 to label.x = 0.6 and label.y = 6, the graph would look like this:

    ggboxplot(mtcars, x = "am", y = "carb",
              color = "am") +
      stat_compare_means(method = "wilcox.test",
                         label.x = 0.6, label.y = 6)
    

    enter image description here