Home > Mobile >  Use ggsignif to put significance bars over facets
Use ggsignif to put significance bars over facets

Time:09-16

I'm trying to do almost enter image description here

From this data frame:

Data<-structure(list(Op = c("No", "No", "No", "No", "Yes", "Yes", "Yes", 
"Yes"), Drug = c("No", "No", "Yes", "Yes", "No", "No", "Yes", 
"Yes"), Follow = c("No", "Yes", "No", "Yes", "No", "Yes", "No", 
"Yes"), n = c(46, 101, 25, 27, 2, 65, 2, 22), Percent = c(31.29251701, 
68.70748299, 48.07692308, 51.92307692, 2.985074627, 97.01492537, 
8.333333333, 91.66666667)), spec = structure(list(cols = list(
    Op = structure(list(), class = c("collector_character", "collector"
    )), Drug = structure(list(), class = c("collector_character", 
    "collector")), Follow = structure(list(), class = c("collector_character", 
    "collector")), n = structure(list(), class = c("collector_double", 
    "collector")), Percent = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), delim = ","), class = "col_spec"), problems = <pointer: 0x000001fa6e358930>, row.names = c(NA, 
-8L), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"))

And the graph was made with this code:

Data %>%
  ggplot(aes(x = Drug,
             y = Percent, 
             fill = Follow))  
  geom_col(position = "dodge2")  
  labs(x = "", 
       fill = "Follow", 
       title = "Drug vs Follow")  
  geom_text(aes(label = paste0("n=",n)), 
            vjust = -0.2, 
            position = position_dodge(.9))  
  facet_grid(.~Op)  
  geom_text(aes(label=paste0("n=",n)), 
            vjust = -0.2, 
            position = position_dodge(.9))

I'd love to manually draw in significance bars that look like this:

enter image description here

But I am having trouble following the code in that linked answer. When I add:

geom_signif(data = data.frame(Op = c("No","Yes")),
            aes(y_position=c(5.3, 8.3), 
                xmin=c(0.8, 0.8), 
                xmax=c(1.2, 1.2),
                annotations=c("**", "NS")), 
            tip_length=0, 
            manual = T) 

to the end of my code, I get the error ' "Follow" not found '. (P.s. I'm sure the numbers were wrong and in wrong position, I was just hoping to get some bars and then I'd move them around)

Any help would be appreciated! Thank you!

CodePudding user response:

The issue is that geom_signif inherits the global aesthetics you set inside ggplot(). In particular, as you set fill=Follow ggplot is expecting a variable with this name in the dataframe you passed via the data argument of geom_signif.

One option to solve this issue would be to make fill=Follow a local aesthetic to geom_col which also requires to map Follow on the group aes inside geom_text.

library(ggplot2)
library(ggsignif)

ggplot(Data, aes(x = Drug, y = Percent))  
  geom_col(aes(fill = Follow), position = "dodge2")  
  labs(x = "", fill = "Follow", title = "Drug vs Follow")  
  geom_text(aes(label = paste0("n=", n), group = Follow), vjust = -0.2, position = position_dodge(.9))  
  facet_grid(. ~ Op)  
  geom_signif(data = data.frame(Op = c("No", "Yes")), aes(y_position = c(5.3, 8.3), xmin = c(0.8, 0.8), xmax = c(1.2, 1.2), annotations = c("**", "NS")), tip_length = 0, manual = T)
#> Warning: Ignoring unknown aesthetics: y_position, xmin, xmax, annotations

A second option would be to set inherit.aes=FALSE in geom_signif.

ggplot(Data, aes(x = Drug, y = Percent, fill = Follow))  
  geom_col(position = "dodge2")  
  labs(x = "", fill = "Follow", title = "Drug vs Follow")  
  geom_text(aes(label = paste0("n=", n)), vjust = -0.2, position = position_dodge(.9))  
  facet_grid(. ~ Op)  
  geom_signif(data = data.frame(Op = c("No", "Yes")), 
    aes(y_position = c(5.3, 8.3), xmin = c(0.8, 0.8), xmax = c(1.2, 1.2),
        annotations = c("**", "NS")), tip_length = 0, manual = T,
        inherit.aes = FALSE)
#> Warning: Ignoring unknown aesthetics: y_position, xmin, xmax, annotations

  • Related