kassambara / ggpubr

'ggplot2' Based Publication Ready Plots

Home Page:https://rpkgs.datanovia.com/ggpubr/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

A modification suggestion for stat_compare_means

AnkingLin opened this issue · comments

In utilities_label.R:

# Check label coordinates for each group
#:::::::::::::::::::::::::::::::::::::::::
# coord.values: label coordinate for each group. If too short, they are recycled.
# group.id the id of groups as returned by ggplot_build()
.group_coord <- function(coord.values, group.id){
  if(!.is_empty(coord.values)){
    coord.values <- ifelse(length(coord.values) >= group.id,
                           coord.values[group.id], coord.values[1])
  }
  coord.values
}

this line

    coord.values <- ifelse(length(coord.values) >= group.id,
                           coord.values[group.id], coord.values[1])

should be changed to:

    coord.values <- ifelse(length(coord.values) >= group.id & group.id>0,
                           coord.values[group.id], coord.values[1])

in order to avoid passing in a group.id of 0 because the group data x contains 0, then causing an incredible error.(which confused me all night :-)


The problem can be reproduced like this:
First,generate group data with 0 in x (any x in (-2,1) can lead to this problem):

> data
   x   y group
1  0 1.1     A
2  0 1.2     A
3  0 1.3     A
4  0 1.4     A
5  3 1.5     A
6  3 1.6     A
7  3 1.7     A
8  3 1.8     A
9  0 1.1     B
10 0 1.2     B
11 0 1.3     B
12 0 1.4     B
13 3 1.5     B
14 3 1.6     B
15 3 1.7     B
16 3 1.8     B

Then have a simple plot:

> 
ggplot(data,aes(x=x,y=y,color=group))+
geom_point()+
stat_compare_means(label = 'p.signif')

And you get it:

Warning message:
Computation failed in `stat_compare_means()`
Caused by error in `purrr::map()`:
ℹ In index: 1.
Caused by error in `.check_npc_coord()`:
! '*.npc coord for x axis should be either a numeric value in [0-1] or a character strings including one of right, left, center, centre, middle