Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
CorentinWicht opened this issue Sep 24, 2021 · 2 comments
Open

Comments

@CorentinWicht
Copy link

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
Copy link
Author

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
Copy link
Author

Anyone ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant