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

Puzzled for annotation that contain duplicate contents #6

Closed powerinformatics closed 7 years ago

powerinformatics commented 7 years ago

Hi Constantin, The package you provide is exceedingly useful. But I'm troubled by the function aes(annotation=). The annotation would be merged and drawn on the midpoint coordinate, when the annotation contain duplicate contents. You can try this code which comes from the site https://cran.r-project.org/web/packages/ggsignif/vignettes/intro.html. After I modified the content of annotation, the strange result emerged. geom_signif(stat="identity", data=data.frame(x=c(0.875, 1.875), xend=c(1.125, 2.125), y=c(5.8, 8.5), annotation=c("**", "**")), aes(x=x,xend=xend, y=y, yend=y, annotation=annotation)) Waitting for your solution.

const-ae commented 7 years ago

Hi, the issue you raise is very puzzling indeed... To quickly fix the problem, just define the group in aes(x=x,xend=xend, y=y, yend=y, annotation=annotation, group=c(1,2)):


ggplot(dat, aes(Group, Value)) +
  geom_bar(aes(fill = Sub), stat="identity", position="dodge", width=.5) +
  geom_signif(comparisons=list(c("S1", "S2")), annotations="***",
              y_position = 9.3, tip_length = 0, vjust=0.4) +
  geom_signif(stat="identity",
              data=data.frame(x=c(0.875, 1.875), xend=c(1.125, 2.125),
                              y=c(5.8, 8.5), annotation=c("**", "**")),
              aes(x=x,xend=xend, y=y, yend=y, annotation=annotation, group=c(1,2))) +
  scale_fill_manual(values = c("grey80", "grey20"))

image

The problem is that currently the data is separated by annotation into different groups and if the annotation is identical for some rows it is assumed that it's just a more complex definition of the bracket.

I will see if I can find a better pattern to avoid confusion or I will update the documentation in the next release.

const-ae commented 7 years ago

In the new v.0.3.0 release I have extended the capability of the package to put brackets at custom locations, where only xmin, xmax and y_position are provided. The new code for the example thus looks like this:

dat <- data.frame(Group = c("S1", "S1", "S2", "S2"),
                  Sub   = c("A", "B", "A", "B"),
                  Value = c(3,5,7,8))  

ggplot(dat, aes(Group, Value)) +
  geom_bar(aes(fill = Sub), stat="identity", position="dodge", width=.5) +
  geom_signif(y_position=c(5.3, 8.3), xmin=c(0.8, 1.8), xmax=c(1.2, 2.2),
              annotation=c("**", "NS"), tip_length=0) +
  geom_signif(comparisons=list(c("S1", "S2")),
              y_position = 9.3, tip_length = 0, vjust=0.2) +
  scale_fill_manual(values = c("grey80", "grey20"))

result

This avoids the unnecessary complicated definition of the data.frame and aes in the geom_signif call.

I will close this issue, but you can re-open it if I missed anything.