const-ae/ggsignif

Rescaling the y-axis with manual annotations?

JCSzamosi opened this issue · 3 comments

I am using the manual annotations option described here, and it's working great except when I add scale_y_sqrt() to the plot, the data points get rescaled but it appears that the annotations do not. The y_position is defined such that the lines are above all the data and this works fine when the y-axis is not rescale, but once I rescale the data the bars are sometimes below the data. Wondering if anyone has any idea how to fix this.

Can you please provide a reproducible example here?

For sure! Here are some data files and an R script that should generate the problem: https://gist.github.com/JCSzamosi/277edf5129d5cc0e16f91798adf66999

library(ggplot2)
library(ggsignif)

# data
mre_df <- structure(list(Sample = c(
  "KS30", "KS44", "KS29", "KS76", "KS28",
  "KS67", "KS69", "KS68", "KS27", "KS88", "KS50", "KS85", "KS82",
  "KS70", "KS72", "KS74", "KS43", "KS84", "KS48", "KS83", "KS75",
  "KS90", "KS49", "KS64", "KS101", "KS62", "KS71", "KS51", "KS66",
  "KS100", "KS99", "KS87", "KS65", "KS97", "KS86", "KS98", "KS73",
  "KS47", "KS52", "KS58", "KS56", "KS60", "KS54", "KS92", "KS81",
  "KS59", "KS80", "KS33", "KS53", "KS94", "KS93", "KS61", "KS77",
  "KS55", "KS89", "KS79", "KS46", "KS45", "KS63", "KS32", "KS78",
  "KS31", "KS96", "KS95"
), Abundance = c(
  0.000766902119071645,
  0.000709849157054126, 0.000683469324040401, 0.000677752199773304,
  0.000658978583196046, 0.000570559147965006, 0.000487420794120955,
  0.000471289689716021, 0.00038038723420442, 0.000322798368400974,
  0.000296925471706602, 0.000295097903069018, 0.000288453004348984,
  0.000268111489694465, 0.000266260935717003, 0.000264279722851004,
  0.00025071487620106, 0.000246285198259585, 0.000238574659553961,
  0.000238120436024976, 0.000236781663627969, 0.000234660993085323,
  0.000228277687124293, 0.000227795374669154, 0.000194537810585018,
  0.000194223562745636, 0.000193265761896581, 0.000182116190129302,
  0.000177714590367869, 0.000169033130493577, 0.000167089746473442,
  0.000155175590879555, 0.000151700563314758, 0.000116891949332653,
  0.000109045308325609, 9.55740525135976e-05, 9.5331913947059e-05,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0
), Group = c(
  "C1", "A1", "C1", "B2", "C1", "B1",
  "B1", "B1", "C1", "B1", "A1", "B1", "B1", "B1", "B2", "B2", "A1",
  "B1", "A1", "B1", "B2", "B1", "A1", "B1", "B2", "B1", "B1", "A1",
  "B1", "B2", "B2", "B1", "B1", "B2", "B1", "B2", "B2", "A1", "A1",
  "A2", "A2", "A2", "A2", "B2", "B2", "A2", "B2", "C2", "A2", "B2",
  "B2", "A2", "B2", "A2", "B1", "B2", "A1", "A1", "B1", "C2", "B2",
  "C2", "B2", "B2"
)), class = "data.frame", row.names = c(NA, -64L))


mre_star <- structure(list(xmin = c("B1", "C1", "A1"), xmax = c(
  "B2", "C2",
  "A2"
), annotations = c("*", "*", "*"), y_position = c(
  0.0304622974195429,
  0.0332315971849559, 0.0332315971849559
), Test = c(
  "Test1", "Test2",
  "Test2"
)), row.names = c(NA, -3L), class = "data.frame")

# If I'm not rescaling the y axis, this works fine
unscaled <- ggplot(mre_df, aes(Group, Abundance)) +
  geom_boxplot() +
  geom_signif(
    data = mre_star, aes(
      annotations = annotations,
      xmin = xmin, xmax = xmax,
      y_position = y_position,
      colour = Test
    ),
    manual = TRUE
  )
#> Warning: Ignoring unknown aesthetics: annotations, xmin, xmax, y_position

unscaled

# If I rescale the y axis, only the boxplots get rescaled
broken <- ggplot(mre_df, aes(Group, Abundance)) +
  geom_boxplot() +
  geom_signif(
    data = mre_star, aes(
      annotations = annotations,
      xmin = xmin, xmax = xmax,
      y_position = y_position,
      colour = Test
    ),
    manual = TRUE
  ) +
  scale_y_sqrt()
#> Warning: Ignoring unknown aesthetics: annotations, xmin, xmax, y_position

broken

# I need to manually rescale the significance data frame
mre_star$y_position <- sqrt(mre_star$y_position)
fixed <- ggplot(mre_df, aes(Group, Abundance)) +
  geom_boxplot() +
  geom_signif(
    data = mre_star, aes(
      annotations = annotations,
      xmin = xmin, xmax = xmax,
      y_position = y_position,
      colour = Test
    ),
    manual = TRUE
  ) +
  scale_y_sqrt()
#> Warning: Ignoring unknown aesthetics: annotations, xmin, xmax, y_position

fixed

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

Session info
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-21                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date       lib source        
#>  assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.0.2)
#>  callr         3.5.1   2020-10-13 [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.1   2021-01-15 [1] CRAN (R 4.0.3)
#>  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.3   2021-01-15 [1] CRAN (R 4.0.3)
#>  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.2   2021-01-15 [1] CRAN (R 4.0.3)
#>  farver        2.0.3   2020-01-16 [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)
#>  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)
#>  highr         0.8     2019-03-20 [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)
#>  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)
#>  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)
#>  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)
#>  remotes       2.2.0   2020-07-21 [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)
#>  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.5   2021-01-15 [1] CRAN (R 4.0.3)
#>  tidyselect    1.1.0   2020-05-11 [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.4.0   2021-01-16 [1] CRAN (R 4.0.3)
#>  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