Search code examples
rggplot2significance

How do you change font and bracket size with geom_signif (ggsignif, R)?


I'm attempting to plot data with ggplot2 and show pairwise statistical comparisons using ggsignif. The font-size of the ggsignif annotations is too small and the line-weight too fine. The help states that the textsize and size parameters should respectively change font-size and bracket thickness, however, these do not work for me. I've tried adjusting text sizes in theme() and setting the base_size. All other text is responsive. I am using what I believe to be the latest version of everything (R v3.6.1, ggsignif v0.6.0).

I am uncertain whether it is related, but I also found that geom_signif() is misplacing annotations (e.g. "*") but correctly placing the comparison brackets. This occurs whenever I add multiple comparisons within one geom_signif(). If I add multiple geom_signif() each with a single-pair comparison (see code below) then I don't get this issue. Even if I add only one geom_signif() it doesn't affect the textsize/size issue.

p = ggplot(population, aes(x=Cluster, y=Cells)) +
  geom_jitter(aes(colour=Sample), position=position_jitterdodge(jitter.width=0.3, dodge.width=0.7), size=1, alpha=0.1) +
  geom_point(data=population.sum, aes(y=Cells, fill=Sample), size=10, position=position_dodge(0.7)) +
  geom_errorbar(data=population.sum, aes(fill=Sample, ymin=CI.Lower, ymax=CI.Upper), width=0.2, size=2, position=position_dodge(0.7)) +
  scale_y_continuous(limits=c(0,1200), expand=c(0, 0)) +
  scale_color_brewer(palette="Paired") +
  theme_classic(base_size = 60) +
  theme(
    legend.justification=c(0, 1),
    legend.position=c(0.01, 1),
    legend.title=element_blank(),
    axis.text.x=element_text(angle=45, hjust=1),
    axis.title.x=element_blank(),
    plot.margin=unit(c(25.5,5.5,5.5,5.5),"pt")
  )

for (i in seq_along(cluster.levels)) # Shouldn't have to add individually?
  p = p + geom_signif(
    stat="identity",
    data=population.sig[i,],
    size=10, # This doesn't work!
    textsize=200, # This doesn't work!
    aes(x=x.start, xend=x.end, y=y, yend=y, annotation=Annotation)
  )

png("Population Scatter Plot.png", 4500, 3000)
p
dev.off()

The datasets are:

population individual data points. 30k records (1k per Cluster * Sample). Columns:

  • Cluster (factor): The groups of graph columns
  • Sample (factor): The individual of the pair in the groups of graph columns
  • Cells (int): The faint points plotted

population.sum summary data. 30 records (1 per Cluster * Sample). Columns:

  • Cluster (factor): as above
  • Sample (factor): as above
  • Cells (int): The large black dot plotted
  • CI.Lower (num): Lower extent of the error bar plotted
  • CI.Upper (num): Upper extent of the error bar plotted

population.sig format data for significance brackets. 15 records (1 per Cluster). Columns:

  • x.start (num): left extent of the comparison bracket
  • x.end (num): right extent of the comparison bracket
  • y (num): vertical position of the comparison bracket
  • Annotation (chr): text to annotate ("*" or "NS")

Example graph. The comparison bracket is too faint and the annotation text is too small


Solution

  • I encountered the same issue. Weirdly, when I moved the textsize function in the aes() of geom_signif, the issue was resolved.

    For your case, try following code:

        geom_signif(
        stat="identity",
        data=population.sig[i,],
        #size=10, 
        #textsize=200, 
        aes(x=x.start, xend=x.end, y=y, yend=y, annotation=Annotation,size=10,textsize=200)