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

Square Brackets do not appear when I am using `x` and `fill` arguments #71

Closed guilhermeparreira closed 3 years ago

guilhermeparreira commented 4 years ago

Hi There. Thanks for your package! Inspiring!

When I have a graphic using x and fill arguments tip_length does not work properly. See the code below:

library(ggsignif)
library(ggplot2)
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(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("**", "NS")),
              aes(x=x,xend=xend, y=y, yend=y, annotation=annotation),
              tip_length = .05) +
  geom_signif(comparisons=list(c("S1", "S2")), annotations="***",
              y_position = 9.3, tip_length = 0.05, vjust=0.4) +
  scale_fill_manual(values = c("grey80", "grey20"))

Produces: image

So, it does not produce square brackets for the first geom_signif. Maybe it is due to the fact that it uses data.frame. But I don't know how to solve.

It would be great if you could have a look on it.

Thanks in advance!

const-ae commented 4 years ago

Hey Guilherme,

sorry for the delay. I did try to figure out yesterday what is happening there, but got very confused by my own code and the way that ggplot calls the internal functions. It's been a while since I wrote the original code. I will give it another try next week.

Best, Constantin

johnhenrypezzuto commented 3 years ago

Has there been any progress on this issue?

IndrajeetPatil commented 3 years ago

Here is an example using both x and fill, where the square brackets do appear.

library(ggsignif)
library(ggplot2)

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.05
  ) +
  geom_signif(
    comparisons = list(c("S1", "S2")),
    y_position = 9.3, tip_length = 0.05, vjust = 0.2
  ) +
  scale_fill_manual(values = c("grey80", "grey20"))

Created on 2021-08-12 by the reprex package (v2.0.1)

I am not sure what this is supposed to do in your example. It's likely to be the source of the issue you are facing.

aes(x=x,xend=xend, y=y, yend=y, annotation=annotation),