Why geom_bracket is not allowing me to plot a bracket?

I would like to add a bracket using geom_bracket for my first two groups of countries the United Kingdom (UK) and France (FR). I use the following code and it plots the three estimates:

library(ggpubr) library(ggplot2) df %>% ggplot(aes(estimate, cntry)) + geom_point() 

However, whenever i add the geom_bracket as below, i get an error. I tried to get around it in different ways but it is still not working. Could someone let me know what i am doing wrong?

df %>% ggplot(aes(estimate, cntry)) + geom_point() + geom_bracket(ymin = "UK", ymax = "FR", x.position = -.75, label.size = 7, label = "group 1") 

Here is a reproducible example:

structure(list(cntry = structure(1:3, .Label = c("BE", "FR", "UK"), class = "factor"), estimate = c(-0.748, 0.436, -0.640)), row.names = c(NA, -3L), groups = structure(list( cntry = structure(1:3, .Label = c("BE", "FR", "UK"), class = "factor"), .rows = structure(list(1L, 2L, 3L), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr", "list"))), row.names = c(NA, 3L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", "tbl_df", "tbl", "data.frame")) 
4

1 Answer

Well, it's pretty damn late at that, but I figured out a workaround for this. I though that I might as well post it here in case anyone finds it useful.

Firstly, as Basti mentioned, ymin, ymax, and x.position aren't arguments that can be used - you have to use xmin, xmax, and y.position. Now, won't this only work for a flipped graph (i.e. x = cntry, y = estimate)? Yes, it will. However you can easily get around this by using coord_flip().

Secondly, it turns out that geom_bracket doesn't inherit the data description (df) and won't run without it being defined inside it. Why? No idea. But this is what was causing the error. Additionally, for some reason, merely defining the data isn't enough, a label must also be added. Not a problem here, just thought I might mention it for dumb people like me who decided to use geom_bracket to add brackets to stat_compare_means.

Here's an example of the OP that should work, along with data generation:

library(ggplot2) library(ggpubr) library(tibble) #I like tibbles df <- tibble(cntry = factor(c("BE", "FR", "UK")), estimate = c(-0.748,0.436,-0.64)) #dataframe generation df %>% ggplot(aes(cntry, estimate)) + geom_point() + coord_flip() + #necessary if you want to keep this weird x/y orientation geom_bracket(data = df, xmin = "UK", xmax = "FR", y.position = -.75, label.size = 7, label = "group 1", coord.flip = T) #coord.flip = T reflects the added coord_flip() 

You can then play around with y coordinates, size, etc. You can also expand the graph using expand_limits().

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like