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

geom_signif fails when reassigning factor levels #47

Open SubstantiaNegri opened 6 years ago

SubstantiaNegri commented 6 years ago

Hi Constantin,

Thank you for your work in developing the geom_signif extension to ggplot. It is a great tool.

I want to bring to your attention an issue I have run into using geom_signif, specifically, that the geom_signif layer will not render in the plot if factor levels are reassigned within ggplot. I found this while using geom_signif to annotate some bar plots

Here is an example:

library(plyr)
library(ggplot2)
library(ggsignif)

#generate some data

mtcars.meanMPG <- 
  ddply(
    mtcars,
    .(carb, am),
    summarize,
    meanMPG = round(mean(mpg),3)
  )

This works:

ggplot(
  data=mtcars.meanMPG,
  aes(
    x=factor(carb),
    y=meanMPG,
    fill=am,
    group=am
  )
)+
  geom_bar(
    stat = "identity",
    position = position_dodge(preserve = "single")
  )+
  geom_signif(
    annotation="p = 0.01",
    y_position=29,
    xmin=1.7,
    xmax=2.3,
    tip_length = c(0.01, 0.01)
  )

This does not:

ggplot(
  data=mtcars.meanMPG,
  aes(
    x=factor(carb, levels = unique(rev(mtcars.meanMPG$carb))),
    y=meanMPG,
    fill=am,
    group=am
  )
)+
  geom_bar(
   stat = "identity",
    position = position_dodge(preserve = "single")
  )+
  geom_signif(
    annotation="p = 0.01",
    y_position=29,
    xmin=1.7,
    xmax=2.3,
    tip_length = c(0.01, 0.01)
  )

Nor does this:

mtcars.meanMPG$carb <- 
factor(mtcars.meanMPG$carb, levels = unique(rev(mtcars.meanMPG$carb)))

ggplot(
  data=mtcars.meanMPG,
  aes(
    x=carb,
    y=meanMPG,
    fill=am,
    group=am
  )
)+
  geom_bar(
    stat = "identity",
    position = position_dodge(preserve = "single")
  )+
  geom_signif(
    annotation="p = 0.01",
    y_position=29,
    xmin=1.7,
   xmax=2.3,
   tip_length = c(0.01, 0.01)
 )

I often find myself reassigning factor levels in order to get figures to rendering correctly. Just letting you know in case this is something you want to look into.

Thanks, -Joe

const-ae commented 5 years ago

Hi Joe, thanks for letting me know. This is indeed a puzzling bug. Unfortunately I don't have the capacity right now to search for the root cause. I will just leave this issue open for others who might encounter the same problem. Best, Constantin

IndrajeetPatil commented 3 years ago

Posting a reprex for future reference:

library(plyr)
library(ggplot2)
library(ggsignif)

# generate some data
mtcars.meanMPG <-
  ddply(
    mtcars,
    .(carb, am),
    summarize,
    meanMPG = round(mean(mpg), 3)
  )

# works
ggplot(
  data = mtcars.meanMPG,
  aes(
    x = factor(carb),
    y = meanMPG,
    fill = am,
    group = am
  )
) +
  geom_bar(
    stat = "identity",
    position = position_dodge(preserve = "single")
  ) +
  geom_signif(
    annotation = "p = 0.01",
    y_position = 29,
    xmin = 1.7,
    xmax = 2.3,
    tip_length = c(0.01, 0.01)
  )

# doesn't work
ggplot(
  data = mtcars.meanMPG,
  aes(
    x = factor(carb, levels = unique(rev(mtcars.meanMPG$carb))),
    y = meanMPG,
    fill = am,
    group = am
  )
) +
  geom_bar(
    stat = "identity",
    position = position_dodge(preserve = "single")
  ) +
  geom_signif(
    annotation = "p = 0.01",
    y_position = 29,
    xmin = 1.7,
    xmax = 2.3,
    tip_length = c(0.01, 0.01)
  )
#> Warning: Use of `mtcars.meanMPG$carb` is discouraged. Use `carb` instead.

#> Warning: Use of `mtcars.meanMPG$carb` is discouraged. Use `carb` instead.

# doesn't work
mtcars.meanMPG$carb <-
  factor(mtcars.meanMPG$carb, levels = unique(rev(mtcars.meanMPG$carb)))

ggplot(
  data = mtcars.meanMPG,
  aes(
    x = carb,
    y = meanMPG,
    fill = am,
    group = am
  )
) +
  geom_bar(
    stat = "identity",
    position = position_dodge(preserve = "single")
  ) +
  geom_signif(
    annotation = "p = 0.01",
    y_position = 29,
    xmin = 1.7,
    xmax = 2.3,
    tip_length = c(0.01, 0.01)
  )

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