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

add orientation and better compatible with coord_flip #104

Closed xiangpin closed 3 years ago

xiangpin commented 3 years ago

original

library(ggplot2)
library(ggsignif)
dat <- data.frame(
            Group = c("S1", "S1", "S2", "S2"),
            Sub = c("A", "B", "A", "B"),
             Value = c(3, 5, 7, 8)
            )
p1 <- ggplot(dat, mapping=aes(x=Group, y=Value)) +
             geom_col(aes(fill=Sub), position="dodge", width=0.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"))
p1

p1

Introducing orientation

p2 <- ggplot(dat, mapping=aes(y=Group, x=Value)) +
          geom_col(aes(fill=Sub), position="dodge", width=0.5, orientation="y") + 
          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, 
                              orientation="y") +
          geom_signif(comparisons = list(c("S1", "S2")),
                               y_position = 9.3, 
                               tip_length = 0, 
                               vjust = 0.2, 
                                orientation="y") + 
          scale_fill_manual(values = c("grey80", "grey20"))
p2

p2

Orignal + coord_flip

p3 <- p1 + coord_flip()
p3

p3

orientation + coord_flip

p4 <- p2 + coord_flip()
p4

p4

IndrajeetPatil commented 3 years ago

Thank you so much for working on this! This looks fantastic.

Just a couple of minor things:

Thanks

const-ae commented 3 years ago

@xiangpin, Wow this is really great, thank you for addressing this longstanding shortcoming.

@IndrajeetPatil, do you know what is going on with all the CI failures. Is that expected?

Can you please make an entry in NEWS.md about this change?

That's a good idea. In addition, I would suggest adding an example in the README.Rmd and also if possible some unit tests

IndrajeetPatil commented 3 years ago

Some of the tests need to be updated because of this change. I can update them once this is merged.

xiangpin commented 3 years ago

Hello @IndrajeetPatil @const-ae

I have made some updates according to your suggestions.

best wishes xushuangbin

IndrajeetPatil commented 3 years ago

Excellent. Thanks once again for your contribution! :)