Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

geom_signif fails when reassigning factor levels #47

Open
SubstantiaNegri opened this issue Aug 31, 2018 · 2 comments
Open

geom_signif fails when reassigning factor levels #47

SubstantiaNegri opened this issue Aug 31, 2018 · 2 comments
Labels

Comments

@SubstantiaNegri
Copy link

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
Copy link
Owner

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
Copy link
Collaborator

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants