const-ae / ggsignif

Easily add significance brackets to your ggplots
https://const-ae.github.io/ggsignif/
GNU General Public License v3.0
593 stars 43 forks source link

Computation failed in `stat_signif()` #56

Closed xiaorenwu1111 closed 3 years ago

xiaorenwu1111 commented 5 years ago

Hello, I met some questions when used this packages.Could you help me? This is my code:

my_comparisons <- list(c("A", "B"), c("B", "C"), c("C", "D"))
ggplot(distance, aes(x=Group,y=Distance, color=Method,shape=Method)) +
  geom_boxplot(fill="cornflowerblue",
               color="black", notch=TRUE) +
  geom_point(position = "jitter", color="blue", alpha=.5) +
  geom_rug(side="1", color="black")+theme_bw()+
  geom_signif(comparisons = my_comparisons,test = "t.test")+
  facet_grid(.~Method)

This is my data structures: Group Distance Method A 0 Hamming distance A 0 Hamming distance A 0 Hamming distance A 0 Hamming distance A 0 Hamming distance A 0 Hamming distance A 0 Hamming distance A 0 Hamming distance A 0 Hamming distance A 0 Hamming distance A 0 Hamming distance A 0 Hamming distance A 0 Hamming distance A 0 Hamming distance A 0 Hamming distance A 0 Hamming distance A 0 Hamming distance A 0 Hamming distance A 0 Hamming distance B 162 Hamming distance B 151 Hamming distance B 90 Hamming distance B 150 Hamming distance B 131 Hamming distance B 107 Hamming distance B 145 Hamming distance B 87 Hamming distance B 103 Hamming distance B 96 Hamming distance B 114 Hamming distance B 102 Hamming distance B 103 Hamming distance B 91 Hamming distance B 71 Hamming distance B 77 Hamming distance B 77 Hamming distance B 67 Hamming distance B 40 Hamming distance C 179 Hamming distance C 167 Hamming distance C 109 Hamming distance C 152 Hamming distance C 152 Hamming distance C 152 Hamming distance C 152 Hamming distance C 152 Hamming distance C 152 Hamming distance C 152 Hamming distance C 129 Hamming distance C 89 Hamming distance C 86 Hamming distance C 109 Hamming distance C 86 Hamming distance C 93 Hamming distance C 89 Hamming distance C 80 Hamming distance C 55 Hamming distance D 275 Hamming distance D 250 Hamming distance D 193 Hamming distance D 241 Hamming distance D 235 Hamming distance D 186 Hamming distance D 240 Hamming distance D 174 Hamming distance D 183 Hamming distance D 193 Hamming distance D 171 Hamming distance D 182 Hamming distance D 169 Hamming distance D 159 Hamming distance D 141 Hamming distance D 131 Hamming distance D 122 Hamming distance D 111 Hamming distance D 94 Hamming distance I want to draw a picture like this: 42407858-33b43f68-81c3-11e8-8668-67c916b007d3

But I used above data and code can't get similar picture. Rplot03

And I got some warning messages: Warning messages: 1: Computation failed in stat_signif(): missing value where TRUE/FALSE needed 2: Computation failed in stat_signif(): missing value where TRUE/FALSE needed 3: Computation failed in stat_signif(): missing value where TRUE/FALSE needed Could you help me ? Thank you !

const-ae commented 5 years ago

Hi, to really help you it would be helpful if you could send me a reproducible example, preferably using reprex. Reducing the code to the core also helps me to quickly understand what is happening (for example, stripping the example of unnecessary lines for styling the code).

Anyway, if I generate a similar dataset, I don't encounter the specific problem that you describe, but I hope the code below is helpful to you.

library(tidyverse)
library(ggsignif)

data <- tibble(
  Group = rep(LETTERS[1:4], each=10),
  Distance = rnorm(40, 200, sd=20),
  Method = rep(c("M1", "M2"), times=20)
)

data
#> # A tibble: 40 x 3
#>    Group Distance Method
#>    <chr>    <dbl> <chr> 
#>  1 A         198. M1    
#>  2 A         157. M2    
#>  3 A         173. M1    
#>  4 A         175. M2    
#>  5 A         211. M1    
#>  6 A         175. M2    
#>  7 A         204. M1    
#>  8 A         181. M2    
#>  9 A         196. M1    
#> 10 A         210. M2    
#> # ... with 30 more rows

data %>%
  ggplot(aes(x=Group,y=Distance, color=Method,shape=Method)) +
  geom_boxplot(fill="cornflowerblue",
               color="black") +
  geom_point(position = "jitter", color="blue", alpha=.5) +
  geom_rug(side="1", color="black")+theme_bw()+
  geom_signif(comparisons = list(c("A", "B"), c("C", "D")),test = "t.test", color="black")+
  facet_grid(~ Method)
#> Warning: Ignoring unknown parameters: side


annotation_df <- data %>%
  mutate(Group = as.factor(Group)) %>%
  group_by(Method, Group) %>%
  mutate(id = 1:n()) %>%
  nest() %>%
  crossing(., .) %>%
  rename(Group1 = Group, Group2 = Group1) %>%
  filter(Method == Method1) %>%
  select(- Method1) %>%
  filter(as.numeric(Group1) < as.numeric(Group2)) %>%
  unnest() %>%
  rename(Distance1 = Distance, Distance2 = Distance1) %>%
  group_by(Method, Group1, Group2) %>%
  summarize(pval = sprintf("%.2g", t.test(Distance1, Distance2)$p.value)) %>%
  group_by(Method) %>%
  mutate(ypos = 250 + 10 * 1:n())

data %>%
  ggplot(aes(x=Group,y=Distance, color=Method,shape=Method)) +
  geom_boxplot(fill="cornflowerblue",
               color="black") +
  geom_point(position = "jitter", color="blue", alpha=.5) +
  geom_rug(side="1", color="black")+theme_bw()+
  geom_signif(data=annotation_df,
              aes(xmin=Group1, xmax=Group2, annotations=pval, y_position=ypos),
              tip_length = 0.01, textsize = 3, vjust = -0.2, color="black",
              manual=TRUE) +
  facet_grid(~ Method)
#> Warning: Ignoring unknown parameters: side
#> Warning: Ignoring unknown aesthetics: xmin, xmax, annotations, y_position

Created on 2019-03-30 by the reprex package (v0.2.1)