0

I am hoping to add manual comparisons and annotations to a ggplot with facets. While the previous stack overflow questions have been helpful, transitioning to multiple pairwise comparison per facet has been tricky.

Fortunately, the authors give a great section on this at the bottom of https://github.com/const-ae/ggsignif titled "Advanced Example". It has been modified to show it's possible to add multiple pairwise comparison in one facet box:

    library(ggplot2)
    library(ggsignif)
    annotation_df <- data.frame(
       color = c("E", "E"),
       start = c("Good", "Fair"),
       end = c("Fair", "Very Good"),
       y = c(3.6, 4.7),
       label = c("Comp. 1", "Comp. 2")
  )
  

    ggplot(diamonds, aes(x = cut, y = carat)) +
        geom_boxplot() +
        geom_signif(
          data = annotation_df,
          aes(xmin = start, xmax = end, annotations = label, y_position = y),
          textsize = 3, vjust = -0.2,
          manual = TRUE
        ) +
        facet_wrap(~color) +
        ylim(NA, 5.3)

DiamondsExample

Great, but when I do something similar, I seem only able to get one number produced. Additionally, I can't get it to graph without fill being set. What might be causing this issue?

library(ggplot2)
library(ggsignif)
library(dplyr)


EnrichmentDF <- data.frame(
  cancer = c(rep("BRCA", 3), rep("PRAD", 3), rep("PAAD", 3)),
  occurance = c(166, 152, 90, 288, 512, 291, 58, 145, 101),
  cluster = rep(1:3)
)

Cancer <- apply(EnrichmentDF, 1, function(x)rep(x[1], x[2])) %>% unlist()
Cluster <- apply(EnrichmentDF, 1, function(x)rep(x[3], x[2])) %>% unlist()
niceTable <- table(Cancer, Cluster)

EnrichmentDF <- as.data.frame(niceTable)
colnames(EnrichmentDF) <- c("Cancer", "Cluster", "NumberOfPatients")
EnrichmentDF$CancerTotalN <- rowSums(niceTable) 
EnrichmentDF$Percentage <- round(100 * EnrichmentDF$NumberOfPatients / EnrichmentDF$CancerTotalN, 2)

SignificanceStorage <- runif(9, 0.001, 0.2)

SignificanceDF <- data.frame(Cancer = rep(unique(EnrichmentDF$Cancer), each = ncol(niceTable)), 
                             start = c(1,1,2), end = c(2, 3, 3),
                             y = c(75, 95, 125),
                             label = signif(SignificanceStorage, digits=2)
)

EnrichmentDF %>% ggplot(aes(x = Cluster, y= Percentage, fill = Cancer)) +  # example did not have fill but breaks without it
  geom_bar(aes(fill = Cluster), stat = "identity", 
           position = "dodge", width = .5) +
  geom_signif(
    data = SignificanceDF,
    aes(xmin = start, xmax = end, annotations = label, y_position = y),
    textsize = 3, vjust = -0.2,
    manual = TRUE) +
  facet_wrap(~Cancer) + ylim(0, 130) +
  theme_bw()

This question is similar to this question, but without depreciation fixes leading to consistent overlaps

Incomplete numbers added

OatMeal
  • 45
  • 6

1 Answers1

0

Thanks to TarJae and the contributors to the post here: https://github.com/const-ae/ggsignif/issues/63, an issue has been found requiring a group = in the parameters of geom_signif when faceting. While the comment on github mentions it's due to duplicated data, I do not see such in the example provided here.

These are the changes:

SignificanceDF$group <- 1:nrow(SignificanceDF)
EnrichmentDF %>% ggplot(aes(x = Cluster, y= Percentage)) +

and have been marked by a double ##

library(ggplot2)
library(ggsignif)
library(dplyr)


EnrichmentDF <- data.frame(
  cancer = c(rep("BRCA", 3), rep("PRAD", 3), rep("PAAD", 3)),
  occurance = c(166, 152, 90, 288, 512, 291, 58, 145, 101),
  cluster = rep(1:3)
)

Cancer <- apply(EnrichmentDF, 1, function(x)rep(x[1], x[2])) %>% unlist()
Cluster <- apply(EnrichmentDF, 1, function(x)rep(x[3], x[2])) %>% unlist()
niceTable <- table(Cancer, Cluster)

EnrichmentDF <- as.data.frame(niceTable)
colnames(EnrichmentDF) <- c("Cancer", "Cluster", "NumberOfPatients")
EnrichmentDF$CancerTotalN <- rowSums(niceTable) 
EnrichmentDF$Percentage <- round(100 * EnrichmentDF$NumberOfPatients / EnrichmentDF$CancerTotalN, 2)

SignificanceStorage <- runif(9, 0.001, 0.2)

SignificanceDF <- data.frame(Cancer = rep(unique(EnrichmentDF$Cancer), each = ncol(niceTable)), 
                             start = c(1,1,2), end = c(2, 3, 3),
                             y = c(75, 95, 125),
                             label = signif(SignificanceStorage, digits=2)
)
SignificanceDF$group <- 1:nrow(SignificanceDF) ## NEW CODE ADDED

EnrichmentDF %>% ggplot(aes(x = Cluster, y= Percentage)) +  ## REMOVED FILL
  geom_bar(aes(fill = Cluster), stat = "identity", 
           position = "dodge", width = .5) +
  geom_signif(
    data = SignificanceDF,
    aes(xmin = start, xmax = end, annotations = label, y_position = y),
    textsize = 3, vjust = -0.2,
    manual = TRUE) +
  facet_wrap(~Cancer) + ylim(0, 130) +
  theme_bw()

Fixed

OatMeal
  • 45
  • 6