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

error when adding geom_signif to a plot #46

Open captcoma opened 6 years ago

captcoma commented 6 years ago

Dear Constantin

Thank you very much for your package ggsignif, I very appreciate it. I try to add results from chi-square test to a ggplot and it was mentioned, that I could use ggsignif:

https://stackoverflow.com/questions/51886623/compare-dependent-proportions-in-a-ggplot

Moreover I found your adivce to use geom_signif

https://github.com/const-ae/ggsignif/issues/23

However, If i add this to my plot:

geom_signif(data = annotation_df,
              aes(annotations = annotations, xmin = xmin, xmax = xmax, y_position = y_position),
              manual = TRUE)
df <- data.frame(timepoint=rep(0:2, each=10),response=c("A","B","A","A","A","A","A","A","B","B","A","A","A","A","A","A","A","B","B","B","A","B","B","B","B","B","A","B","B","B"),variable=rep(c("var1","var2"),each=5, 3), subject=rep(1:5,6))
df$timepoint <- factor(df$timepoint, level=c(1,0,2), labels=c("method_A","baseline","method_B"))

df %>% add_count(timepoint,variable,response) %>% add_count(timepoint,variable) %>% mutate(freq=n/nn*100) %>% mutate(total=1) -> df

stats <-data.frame(xmax=c(rep(c("baseline","method_B"),2)))
stats %>% mutate(xmin=as.factor(c(rep(c("method_A","baseline"),2)))) %>% 
  mutate(annotations=c("1","0.2","1","0.5")) %>% 
  mutate(y_position=5) %>% 
  mutate(variable=as.factor(c("var1","var1","var2","var2"))) -> annotation_df

ggplot(df,
       aes(x = timepoint, stratum = response, alluvium = subject,
           y = total, 
           fill = response, label = paste(freq,"%") )) +
  geom_flow() +
  geom_stratum(alpha = .5) +
  geom_text(stat = "stratum", size = 3) +
  theme(legend.position = "none") +
  geom_signif(data = annotation_df,
              aes(annotations = annotations, xmin = xmin, xmax = xmax, y_position = y_position),
              manual = TRUE) +
  facet_wrap(~variable) 

I get this error: _Warning: Ignoring unknown aesthetics: annotations, xmin, xmax, yposition Error in FUN(X[[i]], ...) : object 'response' not found

If i leave out geom_signif(...) everything works. Thank you for any advice, Jacob

muhark commented 3 years ago

Running into the same issue. Pretty sure it's related to #63, wasn't able to make that solution work for me though.

IndrajeetPatil commented 3 years ago

Can reproduce:

library(tidyverse)
library(ggsignif)
library(ggalluvial)

df <- data.frame(
  timepoint = rep(0:2, each = 10),
  response = c("A", "B", "A", "A", "A", "A", "A", "A", "B", "B", "A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "A", "B", "B", "B", "B", "B", "A", "B", "B", "B"),
  variable = rep(c("var1", "var2"), each = 5, 3),
  subject = rep(1:5, 6)
)

df$timepoint <- factor(df$timepoint,
  level = c(1, 0, 2),
  labels = c("method_A", "baseline", "method_B")
)

df %>%
  add_count(timepoint, variable, response) %>%
  add_count(timepoint, variable) %>%
  mutate(freq = n / nn * 100) %>%
  mutate(total = 1) -> df
#> Storing counts in `nn`, as `n` already present in input
#> ℹ Use `name = "new_name"` to pick a new name.

stats <- data.frame(xmax = c(rep(c("baseline", "method_B"), 2)))
stats %>%
  mutate(xmin = as.factor(c(rep(c("method_A", "baseline"), 2)))) %>%
  mutate(annotations = c("1", "0.2", "1", "0.5")) %>%
  mutate(y_position = 5) %>%
  mutate(variable = as.factor(c("var1", "var1", "var2", "var2"))) -> annotation_df

# without
ggplot(
  df,
  aes(
    x = timepoint, stratum = response, alluvium = subject,
    y = total,
    fill = response, label = paste(freq, "%")
  )
) +
  geom_flow() +
  geom_stratum(alpha = .5) +
  geom_text(stat = "stratum", size = 3) +
  theme(legend.position = "none") +
  facet_wrap(~variable)


# with
ggplot(
  df,
  aes(
    x = timepoint, stratum = response, alluvium = subject,
    y = total,
    fill = response, label = paste(freq, "%")
  )
) +
  geom_flow() +
  geom_stratum(alpha = .5) +
  geom_text(stat = "stratum", size = 3) +
  theme(legend.position = "none") +
  geom_signif(
    data = annotation_df,
    aes(annotations = annotations, xmin = xmin, xmax = xmax, y_position = y_position),
    manual = TRUE
  ) +
  facet_wrap(~variable)
#> Warning: Ignoring unknown aesthetics: annotations, xmin, xmax, y_position
#> Error in FUN(X[[i]], ...): object 'response' not found

Created on 2021-01-13 by the reprex package (v0.3.0)

Session info ``` r devtools::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.0.3 (2020-10-10) #> os macOS Mojave 10.14.6 #> system x86_64, darwin17.0 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz Europe/Berlin #> date 2021-01-13 #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date lib source #> assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.0.2) #> backports 1.2.1 2020-12-09 [1] CRAN (R 4.0.3) #> broom 0.7.3 2020-12-16 [1] CRAN (R 4.0.3) #> callr 3.5.1 2020-10-13 [1] CRAN (R 4.0.2) #> cellranger 1.1.0 2016-07-27 [1] CRAN (R 4.0.2) #> cli 2.2.0 2020-11-20 [1] CRAN (R 4.0.3) #> colorspace 2.0-0 2020-11-11 [1] CRAN (R 4.0.2) #> crayon 1.3.4 2017-09-16 [1] CRAN (R 4.0.2) #> curl 4.3 2019-12-02 [1] CRAN (R 4.0.1) #> DBI 1.1.0 2019-12-15 [1] CRAN (R 4.0.2) #> dbplyr 2.0.0 2020-11-03 [1] CRAN (R 4.0.2) #> desc 1.2.0 2018-05-01 [1] CRAN (R 4.0.2) #> devtools 2.3.2 2020-09-18 [1] CRAN (R 4.0.2) #> digest 0.6.27 2020-10-24 [1] CRAN (R 4.0.2) #> dplyr * 1.0.2 2020-08-18 [1] CRAN (R 4.0.2) #> ellipsis 0.3.1 2020-05-15 [1] CRAN (R 4.0.2) #> evaluate 0.14 2019-05-28 [1] CRAN (R 4.0.1) #> fansi 0.4.1 2020-01-08 [1] CRAN (R 4.0.2) #> farver 2.0.3 2020-01-16 [1] CRAN (R 4.0.2) #> forcats * 0.5.0 2020-03-01 [1] CRAN (R 4.0.2) #> fs 1.5.0 2020-07-31 [1] CRAN (R 4.0.2) #> generics 0.1.0 2020-10-31 [1] CRAN (R 4.0.2) #> ggalluvial * 0.12.3 2020-12-05 [1] CRAN (R 4.0.2) #> ggplot2 * 3.3.3 2020-12-30 [1] CRAN (R 4.0.3) #> ggsignif * 0.6.0 2019-08-08 [1] CRAN (R 4.0.2) #> glue 1.4.2 2020-08-27 [1] CRAN (R 4.0.2) #> gtable 0.3.0 2019-03-25 [1] CRAN (R 4.0.2) #> haven 2.3.1 2020-06-01 [1] CRAN (R 4.0.2) #> highr 0.8 2019-03-20 [1] CRAN (R 4.0.2) #> hms 0.5.3 2020-01-08 [1] CRAN (R 4.0.2) #> htmltools 0.5.1 2021-01-12 [1] CRAN (R 4.0.3) #> httr 1.4.2 2020-07-20 [1] CRAN (R 4.0.2) #> jsonlite 1.7.2 2020-12-09 [1] CRAN (R 4.0.3) #> knitr 1.30 2020-09-22 [1] CRAN (R 4.0.2) #> labeling 0.4.2 2020-10-20 [1] CRAN (R 4.0.2) #> lifecycle 0.2.0 2020-03-06 [1] CRAN (R 4.0.2) #> lubridate 1.7.9.2 2020-11-13 [1] CRAN (R 4.0.3) #> magrittr 2.0.1 2020-11-17 [1] CRAN (R 4.0.3) #> memoise 1.1.0 2017-04-21 [1] CRAN (R 4.0.2) #> mime 0.9 2020-02-04 [1] CRAN (R 4.0.2) #> modelr 0.1.8 2020-05-19 [1] CRAN (R 4.0.2) #> munsell 0.5.0 2018-06-12 [1] CRAN (R 4.0.2) #> pillar 1.4.7 2020-11-20 [1] CRAN (R 4.0.3) #> pkgbuild 1.2.0 2020-12-15 [1] CRAN (R 4.0.3) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.0.2) #> pkgload 1.1.0 2020-05-29 [1] CRAN (R 4.0.2) #> prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.0.2) #> processx 3.4.5 2020-11-30 [1] CRAN (R 4.0.3) #> ps 1.5.0 2020-12-05 [1] CRAN (R 4.0.3) #> purrr * 0.3.4 2020-04-17 [1] CRAN (R 4.0.2) #> R6 2.5.0 2020-10-28 [1] CRAN (R 4.0.2) #> Rcpp 1.0.5 2020-07-06 [1] CRAN (R 4.0.2) #> readr * 1.4.0 2020-10-05 [1] CRAN (R 4.0.2) #> readxl 1.3.1 2019-03-13 [1] CRAN (R 4.0.2) #> remotes 2.2.0 2020-07-21 [1] CRAN (R 4.0.2) #> reprex 0.3.0 2019-05-16 [1] CRAN (R 4.0.2) #> rlang 0.4.10 2020-12-30 [1] CRAN (R 4.0.3) #> rmarkdown 2.6 2020-12-14 [1] CRAN (R 4.0.3) #> rprojroot 2.0.2 2020-11-15 [1] CRAN (R 4.0.3) #> rvest 0.3.6 2020-07-25 [1] CRAN (R 4.0.2) #> scales 1.1.1 2020-05-11 [1] CRAN (R 4.0.2) #> sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 4.0.2) #> stringi 1.5.3 2020-09-09 [1] CRAN (R 4.0.2) #> stringr * 1.4.0 2019-02-10 [1] CRAN (R 4.0.2) #> testthat 3.0.1 2020-12-17 [1] CRAN (R 4.0.3) #> tibble * 3.0.4 2020-10-12 [1] CRAN (R 4.0.2) #> tidyr * 1.1.2 2020-08-27 [1] CRAN (R 4.0.2) #> tidyselect 1.1.0 2020-05-11 [1] CRAN (R 4.0.2) #> tidyverse * 1.3.0 2019-11-21 [1] CRAN (R 4.0.2) #> usethis 2.0.0 2020-12-10 [1] CRAN (R 4.0.3) #> vctrs 0.3.6 2020-12-17 [1] CRAN (R 4.0.3) #> withr 2.3.0 2020-09-22 [1] CRAN (R 4.0.2) #> xfun 0.20 2021-01-06 [1] CRAN (R 4.0.3) #> xml2 1.3.2 2020-04-23 [1] CRAN (R 4.0.2) #> yaml 2.2.1 2020-02-01 [1] CRAN (R 4.0.2) #> #> [1] /Users/patil/Library/R/4.0/library #> [2] /Library/Frameworks/R.framework/Versions/4.0/Resources/library ```
aubreyghoward commented 3 years ago

I would like to add the the error in (FUN(X[[i]], ...) is caused by a lack of the specification on dataframe supplying the significance values for all required terms in the facted plot. In the above example, there is not a term in the significance dataframe for "response". This can be solved by supplying the discrete arguments for the comparisons you intend to plot. Please see this example code using diamonds below, which I hope clarifies the issue:


library(ggplot2)
library(ggsignif)
rm(list = ls())

diamonds.abbv <- diamonds[diamonds$clarity == c("I1","SI2"),]

df.sig <- data.frame( start = c(0.8, 1.2), 
                        end = c(1.2,1.8),
                        y = c(3,3),
                        cut = c("Fair","Good"),
                        star = c("D", "E"),
                        label = c("#1", "#2"),
                       clarity = c("I1","I1"))
ggplot(data = diamonds.abbv, mapping = aes( x = color,
                                       y = carat,
                                       fill = clarity))+
         geom_boxplot()+
  facet_wrap(~cut)

ggplot(data = diamonds.abbv, mapping = aes( x = color,
                                            y = carat,
                                            fill = clarity))+
  geom_boxplot()+
  facet_wrap(~cut)+
  geom_signif(data = df.sig, manual = TRUE, 
              mapping = aes(xmin = star, xmax = end, y_position = y, annotations = label))
##end run

However, I still get the following error:

[Warning message:
Ignoring unknown aesthetics: xmin, xmax, y_position, annotations ]
bersbersbers commented 2 years ago

The original error is also explained here: https://stackoverflow.com/a/40391432/880783