const-ae/ggsignif

Use with coord_cartisian when outliers present

Closed this issue · 1 comments

SFrav commented

I have not been able to control the position of the significance brackets when there are outliers present.
Here's an example of what I mean:

mydf <- data.frame(ID=paste(sample(LETTERS, 163, replace=TRUE), sample(1:1000, 163, replace=FALSE), sep=''), Group=c(rep('C',10),rep('FH',10),rep('I',19),rep('IF',42),rep('NA',14),rep('NF',42),rep('NI',15),rep('NS',10),rep('PGMC4',1)), Value=rnorm(n=163))   
CN <- combn(levels(mydf$Group), 2, simplify = FALSE)  

#This is what I want the plot to look like 
ggplot(mydf, aes(x=Group, y=Value, fill=Group)) + geom_boxplot(outlier.shape = NA) + stat_compare_means(comparisons = CN)  

#Add outliers 
mydf$Value[4] <- 300 
mydf$Value[5] <- 765 
mydf$Value[6] <- 12000   

# the plot with outliers 
ggplot(mydf, aes(x=Group, y=Value, fill=Group)) + geom_boxplot(outlier.shape = NA) + stat_compare_means(comparisons = CN)

How can I incorporate coord_cartisian with this plot and get the brackets in a position that I want?

Hey, sorry for the late response.

The trick if you have to deal with outliers is that you have to set the y_position of the bar manually.

ggplot(mydf, aes(x=Group, y=Value, fill=Group)) +
  geom_boxplot(outlier.shape = NA) +
  geom_signif(comparisons = list(c("C", "FH")), y_position=3.3,
              tip_length = 0) +
  coord_cartesian(ylim = c(-3, 5))

image