const-ae / ggsignif

Easily add significance brackets to your ggplots
https://const-ae.github.io/ggsignif/
GNU General Public License v3.0
593 stars 43 forks source link

Error using facet_wrap & geom_signif : "Replacement has X rows..." #108

Open CorentinWicht opened 3 years ago

CorentinWicht commented 3 years ago

Dear developer,

I am having a hard time trying to provide geom_signif with a data.frame to define significant bars only for a subselection of plots, as suggested in your last example in the README file.

You can download the Rdata here.

And here is the code (which is part of a function in my code, but I tried to simplify it as much as possible):

# Create plot
p = ggplot(Data, aes(x = Data[[X]], y = Data[[Y]], fill=Data[[X]])) +
  geom_violin() +
  geom_boxplot(width = 0.15, fill = "grey", na.rm=T, outlier.shape = NA)  +
  geom_jitter(size = 3, width = 0.15, height = 0, alpha = 0.5) +
  labs(x = X, y = Y, title=Title, 
       caption = "[+]p<.1; [*]p<.05; [**]p<.01; [***]p<.001") +
  theme_bw() +
  ylim(min(Data[[Y]], na.rm=T),max(Data[[Y]], na.rm=T)+YLimSupp) 

# Facetting the plot if required  
if (dim(Stats)[1]>1){p = p + facet_wrap(.~Data[[Z]], scales="free")}

# Add the significance bars
# https://cran.r-project.org/web/packages/ggsignif/vignettes/intro.html
Steps = 0.04
NAIdx = !is.na(Annot)
Annot = Annot[NAIdx]
Groups = unique(Data[[Z]])[NAIdx]
AnnotationDF = data.frame(color = Groups,
                          start = rep(Levels[1], sum(NAIdx)),
                          end = rep(Levels[2], sum(NAIdx)),
                          y = rep(max(Data[[Y]], na.rm=T)+Steps*max(Data[[Y]], na.rm=T),sum(NAIdx)),
                          label = Annot)

p = p + geom_signif(data = AnnotationDF,
                    aes(xmin = AnnotationDF$start, xmax = AnnotationDF$end, annotations = AnnotationDF$label, y_position = AnnotationDF$y),
                    vjust = 0.3, tip_length = 0, size = 1.5, textsize = 10, manual=T)

plot(p)

Whatever I do, I always get the same error: image

Many thanks for your support, best regards,

Corentin

CorentinWicht commented 3 years ago

Changing argument in facet_wrap solved the issue raised above but generated a new one:

# Create plot
p = ggplot(Data, aes(x = Data[[X]], y = Data[[Y]], fill=Data[[X]])) +
  geom_violin() +
  geom_boxplot(width = 0.15, fill = "grey", na.rm=T, outlier.shape = NA)  +
  geom_jitter(size = 3, width = 0.15, height = 0, alpha = 0.5) +
  labs(x = X, y = Y, title=Title, 
       caption = "[+]p<.1; [*]p<.05; [**]p<.01; [***]p<.001") +
  theme_bw() +
  ylim(min(Data[[Y]], na.rm=T),max(Data[[Y]], na.rm=T)+YLimSupp) 

# Facetting the plot if required  
if (dim(Stats)[1]>1){p = p + facet_wrap(.~Measurement, scales="free_y")} # FIXED HERE

# Add the significance bars
# https://cran.r-project.org/web/packages/ggsignif/vignettes/intro.html
Steps = 0.04
NAIdx = !is.na(Annot)
Annot = Annot[NAIdx]
Groups = unique(Data[[Z]])[NAIdx]
AnnotationDF = data.frame(color = Groups,
                          start = rep(Levels[1], sum(NAIdx)),
                          end = rep(Levels[2], sum(NAIdx)),
                          y = rep(max(Data[[Y]], na.rm=T)+Steps*max(Data[[Y]], na.rm=T),sum(NAIdx)),
                          label = Annot)

p = p + geom_signif(data = AnnotationDF,
                    aes(xmin = AnnotationDF$start, xmax = AnnotationDF$end, annotations = AnnotationDF$label, y_position = AnnotationDF$y),
                    vjust = 0.3, tip_length = 0, size = 1.5, textsize = 10, manual=T)

plot(p)

New error: image

Does this mean that AnnotationDF needs to include 6 rows to match the number of violin plots ?

Best,

Corentin

CorentinWicht commented 3 years ago

Anyone ?