Open SubstantiaNegri opened 6 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
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)
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:
This works:
This does not:
Nor does this:
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