Search code examples
rggplot2curly-bracesggpubr

Adding underbrace after using coord_flip() on ggplot output


I'm trying to add an underbrace horizontally at the bottom of a ggplot output whose x-y has been flipped (using coordin_flip()). I'll using the mtcars example data for illustration.

library(ggpubr)

data("mtcars")
dfm <- mtcars
# Convert the cyl variable to a factor
dfm$cyl <- as.factor(dfm$cyl)
# Add the name colums
dfm$name <- rownames(dfm)
# Subset the data
head(dfm[, c("name", "wt", "mpg", "cyl")])

# Sort by group and by ascending order

g <- ggbarplot(dfm, x = "name", y = "mpg",
          fill = "cyl",       # change fill color by cyl
         color = "white", # Set bar border colors to white
          palette = "jco",  # jco journal color palett. 
          sort.val = "asc",  # Sort the value in dscending order 
          sort.by.groups = TRUE, # Sort inside each group
          x.text.angle = 90 # Rotate vertically x axis texts
          ) + labs(title="Brand & MPG", y = " ", x = " Brand") + coord_flip()
          

          

Then I want to add a horizontal curly bracket at the bottom of the ggplot object to have the original plot looks like this.

plot w/ underbrace

so I referenced this post and called the ggbrace package, modifies the code by adding this following line:

g + geom_brace(aes(x=c(0,-3), y=c(0,35), label = "something"), inherit.data=F, rotate=90, labelsize=5) + coord_cartesian(y=range(dfm$mpg), clip = "off")

# coord_cartesian() is used in order to put the underbrace outside of the plotting area (at the bottom).

However, R returns an error message:

Coordinate system already present. Adding new coordinate system, which will replace the existing one.

Any suggestions on how to adjust the arguments inside the geom_brace() function to achieve the desired result with minimal modification?


Solution

  • The error seem to indicate that you cannot use coord_flip and coord_cartesian simultaneously, so first, you have to enter all the coord_cartesian parameters into coord_flip.

    Then, as x is categorical, some modifications have to be done so geom_brace and coord_flip understand the inputs in the same way.

    Finally, you have to rotate 270 degrees instead of 90.

    It would result this:

    ggbarplot(dfm, x = "name", y = "mpg",
                   fill = "cyl",       # change fill color by cyl
                   color = "white", # Set bar border colors to white
                   palette = "jco",  # jco journal color palett. 
                   sort.val = "asc",  # Sort the value in dscending order 
                   sort.by.groups = TRUE, # Sort inside each group
                   x.text.angle = 90 # Rotate vertically x axis texts
    ) + labs(title="Brand & MPG", y = " ", x = " Brand") + 
      geom_brace(aes(x=c(-1,0), y = c(0,35), label = "something"), inherit.data=F, rotate=270, labelsize=5) +
      coord_flip(clip="off", expand = FALSE,x=range(seq_along(dfm$name))) +
      theme(plot.margin = unit(c(0., 0., 0.5, 0.), units="lines"))
    

    enter image description here