0

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?

Chris T.
  • 1,699
  • 7
  • 23
  • 45
  • That is not an error but a warning. I still see the braces being drawn on the plot. – Ronak Shah Aug 02 '21 at 14:16
  • @RonakShah I think the error lies in the fact that `geom_brace()` was unable to deal with axis whose values is not numeric, hence, it still puts the brace "inside" (instead of "outside") the plotting grid (which also causes the flipped y-axis to shift upward in order to give the brace some space) even after `coord_cartesian()` was called. I am wondering if there's a way to trick ggplot to assume the flipped y-axis as numeric? – Chris T. Aug 02 '21 at 14:28
  • Maybe turning it into factor? `dfm$name <- factor(rownames(dfm))` but that doesn't change anything in the plot. – Ronak Shah Aug 02 '21 at 14:44

1 Answers1

1

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

iago
  • 2,990
  • 4
  • 21
  • 27