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

"non-finite values (stat_signif)" error in facet boxplot #57

Closed tsievert closed 5 years ago

tsievert commented 5 years ago

Hey,

first of all thanks for the great package! I already used it several times for non-faceted plots without any issue.

However, I'm constantly getting #Warning: Removed 2 rows containing non-finite values (stat_signif). but I can't pinpoint where I went wrong. I tried to follow your example code as closely as possible .

Thanks in advance!

Your example:

library(ggplot2)
library(ggsignif)
#> Warning: package 'ggsignif' was built under R version 3.5.3

annotation_df <- data.frame(color=c("E", "H"), 
                            start=c("Good", "Fair"), 
                            end=c("Very Good", "Good"),
                            y=c(3.6, 4.7),
                            label=c("Comp. 1", "Comp. 2"))

annotation_df
#>   color start       end   y   label
#> 1     E  Good Very Good 3.6 Comp. 1
#> 2     H  Fair      Good 4.7 Comp. 2

ggplot(diamonds, aes(x=cut, y=carat)) +
  geom_boxplot() +
  geom_signif(data=annotation_df,
              aes(xmin=start, xmax=end, annotations=label, y_position=y),
              textsize = 3, vjust = -0.2,
              manual=TRUE) +
  facet_wrap(~ color) +
  ylim(NA, 5.3)
#> Warning: Ignoring unknown aesthetics: xmin, xmax, annotations, y_position

My code:

pairwise.p <- data.frame(s=factor('m',levels = c("m","f")), 
                         start=c("C", "C"), 
                         end=c("AP", "PO"),
                         y=c(3.8, 4.1),
                         label=c("***", "*"))

pairwise.p
#>   s start end   y label
#> 1 m     C  AP 3.8   ***
#> 2 m     C  PO 4.1     *

.df3  <- structure(list(x = structure(c(1L, 2L, 2L, 3L, 1L, 1L, 2L, 1L,1L, 3L, 2L, 3L, 3L, 3L, 1L, 2L, 1L, 3L, 2L, 2L), .Label = c("C","AP", "PO"), class = "factor"), y = c(3.6531, 3.7663, 3.477,3.4731, 3.6278, 3.4597, 3.8088, 3.7771, 3.724, 3.4595, 3.3822,3.7056, 3.4812, 3.7882, 3.73, 3.6536, 3.7506, 3.7997, 3.7486,3.4663), s = structure(c(2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L,2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L), .Label = c("m","f"), class = "factor")), row.names = c(86L, 23L, 46L, 118L,96L, 183L, 111L, 87L, 9L, 117L, 165L, 136L, 139L, 126L, 4L, 51L,142L, 75L, 109L, 188L), class = "data.frame")

library(ggsignif)
#> Warning: package 'ggsignif' was built under R version 3.5.3
library(ggplot2)

.plot2 <- ggplot(data = .df3, aes(x = factor(x), y = y)) + 
  stat_boxplot(geom = "errorbar", width = 0.5) + 
  geom_boxplot() + 
  facet_wrap( ~ s) + 
  scale_y_continuous(limits = c(3.2, 4.29), expand = expand_scale(mult = c(0.05, 0.025))) +
  xlab("") + 
  ylab("") + 
  ggtitle("Postnatal subset") +
  theme_bw(base_size = 14, base_family = "sans") + 
  theme(panel.spacing = unit(0.3, "lines"), plot.title = element_text(size=14)) +
  geom_signif(data=pairwise.p,
              aes(xmin=start, xmax=end, annotations=label, y_position=y),
              textsize = 3,
              manual=TRUE) 
#> Warning: Ignoring unknown aesthetics: xmin, xmax, annotations, y_position
print(.plot2)
#> Warning: Removed 2 rows containing non-finite values (stat_signif).

const-ae commented 5 years ago

Hi Trolske, thanks for the opening the issue and providing a reproducible examples.

As far as I can tell, all you have to do is remove the scale_y_continuous(limits = ...) from the call

ggplot(data = .df3, aes(x = factor(x), y = y)) + 
  stat_boxplot(geom = "errorbar", width = 0.5) + 
  geom_boxplot() + 
  facet_wrap( ~ s) + 
  # scale_y_continuous(limits = c(3.2, 4.29), expand = expand_scale(mult = c(0.05, 0.025))) +
  xlab("") +
  ylab("") +
  ggtitle("Postnatal subset") +
  theme_bw(base_size = 14, base_family = "sans") +
  theme(panel.spacing = unit(0.3, "lines"), plot.title = element_text(size=14)) +
  geom_signif(data=pairwise.p,
              aes(xmin=start, xmax=end, annotations=label, y_position=y),
              textsize = 3,
              manual=TRUE)
#> Warning: Ignoring unknown aesthetics: xmin, xmax, annotations, y_position

Created on 2019-04-01 by the reprex package (v0.2.1)

Now, of course the question is why? And to be honest, right now I have no idea...

This seems to be a genuine bug

const-ae commented 5 years ago

I have found out where the bug was (https://github.com/const-ae/ggsignif/commit/f0f9738ebf0b271d55d2bfb7a1deef4be335decf) and fixed it.

.plot2 <- ggplot(data = .df3, aes(x = factor(x), y = y)) + 
  stat_boxplot(geom = "errorbar", width = 0.5) + 
  geom_boxplot() + 
  facet_wrap( ~ s) + 
  scale_y_continuous(limits = c(3.2, 4.29), expand = expand_scale(mult = c(0.05, 0.025))) +
  xlab("") + 
  ylab("") + 
  ggtitle("Postnatal subset") +
  theme_bw(base_size = 14, base_family = "sans") + 
  theme(panel.spacing = unit(0.3, "lines"), plot.title = element_text(size=14)) +
  geom_signif(data=pairwise.p,
              aes(xmin=start, xmax=end, annotations=label, y_position=y),
              textsize = 3,
              manual=TRUE) 
#> Warning: Ignoring unknown aesthetics: xmin, xmax, annotations, y_position

print(.plot2)

Created on 2019-04-01 by the reprex package (v0.2.1)

To get the latest version with the bug fix, you have to install it from GitHub

devtools::install_github("const-ae/ggsignif")

In addition, I will try to push a new version to CRAN in the next weeks.

I will close this issue for now. If I missed anything feel free to comment and thanks again for discovering the bug.