ggsignif help with facets (sample data attached)

I am trying to use the ggsignif to add significance annotations on my facets but I failed. My data has the p-value already calculated and the significance annotation is included in a column. However, I did not find an easy way to add the significance annotation onto the graph following the Advanced Example on their GitHub page: https://github.com/const-ae/ggsignif

I have a sample data set from my experiment:

|DrugAConc|DrugBConc|CellLines|AverageAbsorb|StdDevAbsorb|CountAbsorb|CellViability|PooledStdDev|StdError|tValue|pValue|SignificanceOfChange|
|---|---|---|---|---|---|---|---|---|---|---|---|
|0|0|A2058|0.16|0.029297326|3|1|0.022524061|0.018390819|-9.153117849|0.00079083|***|
|0|0|A375|0.223666667|0.005567764|3|1|0.009678154|0.00790218|-13.24529058|0.00018775|***|
|0|0|FB|0.328333333|0.012503333|3|1|0.012503333|0.010208929|0|1|NS|
|0|1.5625|A2058|0.113666667|0.003605551|3|0.710416667|0.006055301|0.004944132|-26.42863461|1.22E-05|***|
|0|1.5625|A375|0.176333333|0.006658328|3|0.788375559|0.007234178|0.005906682|-11.51238602|0.000325053|***|
|0|1.5625|FB|0.244333333|0.007767453|3|0.744162437|0.007767453|0.006342099|0|1|NS|
|0|3.125|A2058|0.105666667|0.005|3|0.660416667|0.005887841|0.004807402|-23.15873319|2.06E-05|***|
|0|3.125|A375|0.146333333|0.010785793|3|0.654247392|0.008962886|0.007318166|-9.656335396|0.000643387|***|
|0|3.125|FB|0.217|0.006658328|3|0.660913706|0.006658328|0.005436502|0|1|NS|
|0|6.25|A2058|0.084666667|0.004582576|3|0.529166667|0.006377042|0.005206833|-22.53449087|2.30E-05|***|
|0|6.25|A375|0.12|0.010503968|3|0.536512668|0.009237604|0.007542472|-10.87176676|0.000406297|***|
|0|6.25|FB|0.202|0.007767453|3|0.615228426|0.007767453|0.006342099|0|1|NS|
|1.667|0|A2058|0.198|0.009451631|3|1.2375|0.009009255|0.007356025|-15.99595688|8.93E-05|***|
|1.667|0|A375|0.226|0.002081666|3|1.010432191|0.006218253|0.005077182|-17.66071522|6.04E-05|***|
|1.667|0|FB|0.315666667|0.008544004|3|0.96142132|0.008544004|0.00697615|0|1|NS|
|1.667|1.5625|A2058|0.140666667|0.01473092|3|0.879166667|0.010440307|0.008524475|-12.66940257|0.000223512|***|
|1.667|1.5625|A375|0.171|0.006806859|3|0.764530551|0.00486484|0.003972125|-19.55292565|4.03E-05|***|
|1.667|1.5625|FB|0.248666667|0.001|3|0.757360406|0.001|0.000816497|0|1|NS|
|1.667|3.125|A2058|0.129333333|0.007505553|3|0.808333333|0.008534245|0.006968182|-14.01609079|0.000150331|***|
|1.667|3.125|A375|0.150666667|0.005567764|3|0.673621461|0.007756718|0.006333333|-12.05263158|0.000271738|***|
|1.667|3.125|FB|0.227|0.009451631|3|0.691370558|0.009451631|0.007717225|0|1|NS|
|1.667|6.25|A2058|0.107|0.010016653|3|0.66875|0.010969655|0.008956686|-10.16000796|0.000528488|***|
|1.667|6.25|A375|0.127666667|0.008185353|3|0.570789866|0.010181683|0.008313309|-8.460329215|0.001069541|**|
|1.667|6.25|FB|0.198|0.011846237|3|0.603045685|0.011846237|0.009672412|0|1|NS|

I have 3 cell lines which include 1 control. The SignificanceOfChange were obtained from comparing the 2 experimental cell lines to the 1 control cell line (so the control celll line has NS in SignifAnnotation)

and part of my current bar chart looks like this (x-axis is CellLine, y-axis is CellViability , and each facet represents a different combination of DrugAConc and DrugBConc )

I think maybe with a for loop, I can iterate through each of the row in my dataframe and add the signif annotation one-by-one? However, I cannot get a annotation_df to work as it does in their Advanced Example. Could someone help me out on this? Thanks!

Hi, a for loop will not help here. ggplot2 always uses all the data you hand over and displays the variables you map to, no need for looping (at least for a facet - if you want to create the same plot for different datasets or variables, that's a different story).

So how does your annotation_df looks like? What is the result - an error or misplaced labels?

It would be great if you would provide a reproducible example so we can test this on our own and offer you a solution.

I have never used the package but it looks pretty straight forward. You build a df containing all significant comparisons you want to show. Important here is that the variables have the same structure and classes as in your original df. That being said, your two facet variables should have the same levels for example if those are factors.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

So the annotation in the datframe looks as in the example?

annotation_df
#>   color start       end   y   label
#> 1     E  Good Very Good 3.6 Comp. 1
#> 2     H  Fair      Good 4.7 Comp. 2

How are you feeding this into ggplot2? Can you share your ggplot code as well?

Also, I am not sure how to read your example data - why don't you sample some values from your data use dput() and provide the output here? (Sorry, I just realized now the link I shared was broken. I fixed it.)

Thanks for the suggestion. I updated the question with a sample dataframe. I cannot directly use their comparison function because my dataframe has been summarized before and each sample size is now 1 so R cannot give any p-value (I will have all NS). I have already calculated the p-value on my side (all comparing to FB) and I have the annotation in the dataframe, but I don't know how to put them on the graph.

Sure. This is what I got from the dput()

structure(list(DrugAConc = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 1.667, 1.667, 1.667, 1.667, 1.667, 1.667, 1.667, 1.667, 1.667, 
1.667, 1.667, 1.667), DrugBConc = c(0, 0, 0, 1.5625, 1.5625, 
1.5625, 3.125, 3.125, 3.125, 6.25, 6.25, 6.25, 0, 0, 0, 1.5625, 
1.5625, 1.5625, 3.125, 3.125, 3.125, 6.25, 6.25, 6.25), CellLines = c("A2058", 
"A375", "FB", "A2058", "A375", "FB", "A2058", "A375", "FB", "A2058", 
"A375", "FB", "A2058", "A375", "FB", "A2058", "A375", "FB", "A2058", 
"A375", "FB", "A2058", "A375", "FB"), AverageAbsorb = c(0.16, 
0.223666667, 0.328333333, 0.113666667, 0.176333333, 0.244333333, 
0.105666667, 0.146333333, 0.217, 0.084666667, 0.12, 0.202, 0.198, 
0.226, 0.315666667, 0.140666667, 0.171, 0.248666667, 0.129333333, 
0.150666667, 0.227, 0.107, 0.127666667, 0.198), StdDevAbsorb = c(0.029297326, 
0.005567764, 0.012503333, 0.003605551, 0.006658328, 0.007767453, 
0.005, 0.010785793, 0.006658328, 0.004582576, 0.010503968, 0.007767453, 
0.009451631, 0.002081666, 0.008544004, 0.01473092, 0.006806859, 
0.001, 0.007505553, 0.005567764, 0.009451631, 0.010016653, 0.008185353, 
0.011846237), CountAbsorb = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L
), CellViability = c(1, 1, 1, 0.710416667, 0.788375559, 0.744162437, 
0.660416667, 0.654247392, 0.660913706, 0.529166667, 0.536512668, 
0.615228426, 1.2375, 1.010432191, 0.96142132, 0.879166667, 0.764530551, 
0.757360406, 0.808333333, 0.673621461, 0.691370558, 0.66875, 
0.570789866, 0.603045685), PooledStdDev = c(0.022524061, 0.009678154, 
0.012503333, 0.006055301, 0.007234178, 0.007767453, 0.005887841, 
0.008962886, 0.006658328, 0.006377042, 0.009237604, 0.007767453, 
0.009009255, 0.006218253, 0.008544004, 0.010440307, 0.00486484, 
0.001, 0.008534245, 0.007756718, 0.009451631, 0.010969655, 0.010181683, 
0.011846237), StdError = c(0.018390819, 0.00790218, 0.010208929, 
0.004944132, 0.005906682, 0.006342099, 0.004807402, 0.007318166, 
0.005436502, 0.005206833, 0.007542472, 0.006342099, 0.007356025, 
0.005077182, 0.00697615, 0.008524475, 0.003972125, 0.000816497, 
0.006968182, 0.006333333, 0.007717225, 0.008956686, 0.008313309, 
0.009672412), tValue = c(-9.153117849, -13.24529058, 0, -26.42863461, 
-11.51238602, 0, -23.15873319, -9.656335396, 0, -22.53449087, 
-10.87176676, 0, -15.99595688, -17.66071522, 0, -12.66940257, 
-19.55292565, 0, -14.01609079, -12.05263158, 0, -10.16000796, 
-8.460329215, 0), pValue = c(0.00079083, 0.00018775, 1, 1.22e-05, 
0.000325053, 1, 2.06e-05, 0.000643387, 1, 2.3e-05, 0.000406297, 
1, 8.93e-05, 6.04e-05, 1, 0.000223512, 4.03e-05, 1, 0.000150331, 
0.000271738, 1, 0.000528488, 0.001069541, 1), SignificanceOfChange = c("***", 
"***", "NS", "***", "***", "NS", "***", "***", "NS", "***", "***", 
"NS", "***", "***", "NS", "***", "***", "NS", "***", "***", "NS", 
"***", "**", "NS")), class = "data.frame", row.names = c(NA, 
-24L))

I think my major issue is how to construct the right annotation_df. Since I only want to show A375 vs FB and A2058 vs. FB, I don't know how to skip the FB vs.FB pairs.

Also my code so far:

annotation_df <- data.frame(DrugAConc=viability_csv$DrugAConc,
                            DrugBConc=viability_csv$DrugBConc,
                            start=viability_csv$CellLines,
                            end="FB",
                            y=3.6,
                            label=viability_csv$SignificanceOfChange )



bar_plot2 <- ggplot(viability_csv, aes(x=CellLines, y=CellViability,ymin=CellViability-StdDevAbsorb, ymax=CellViability+StdDevAbsorb, fill=factor(ifelse(CellLines=="FB", "FB control", "Melanoma")))) + # Label the 2 FB references FB with distinct colors so you can tell them apart from the experiment cell lines easily
  geom_bar(stat = "identity", color = 'black', position=position_dodge()) +
  geom_errorbar(aes(y=stat(ymax)), width=.2, position=position_dodge(.9)) + # Move the ymax and ymin statements into the ggplot() such that other layers can access it as well--this allows the data label's height to be automatically adjusted by the respective error bar
  geom_text(aes(label=sprintf("%0.2f", round(CellViability, digits = 2))), position=position_dodge(width=0.9), vjust=-2) +
  facet_grid(DrugAConc~DrugBConc) +
  geom_signif(data=annotation_df,
              aes(xmin=start, xmax=end, annotations=label, y_position=y),
              textsize = 3, vjust = -0.2,
              manual=TRUE) +
  labs(title="6 cell lines' viability with different 2-DG or DLAD dosages for 24, 48, and 72 hrs", x = "Cell lines", y = "Cell viability", fill="Legends") +
  theme_bw()+
  theme(plot.title = element_text(size = 20,face = "bold"),axis.title=element_text(size=20,face="bold"),axis.text = element_text(size = 20, face="bold"), strip.text.x = element_text(size = 20, colour = "blue"), strip.text.y = element_text(size = 20, colour = "blue", angle=0),  legend.title=element_text(size=20),legend.text=element_text(size=15))
  
bar_plot2