So this is very relevant to this question and this answer is an excellent solution.
The problem is that when I try to export the plot using ggsave the curly braces aren\
I don't understand the logic used in grid.brackets
but it would help if there was a bracketsGrob
function that would simply return a grob without drawing it. Perhaps contact the maintainer with a feature request?
Anyway, assuming such a function was available, it can be fed to annotation_custom
making it compatible with ggsave
.
bracketsGrob <- function(...){
l <- list(...)
e <- new.env()
e$l <- l
grid:::recordGrob( {
do.call(grid.brackets, l)
}, e)
}
# note that units here are "npc", the only unit (besides physical units) that makes sense
# when annotating the plot panel in ggplot2 (since we have no access to
# native units)
b1 <- bracketsGrob(0.33, 0.05, 0, 0.05, h=0.05, lwd=2, col="red")
b2 <- bracketsGrob(1, 0.05, 0.66, 0.05, h=0.05, lwd=2, col="red")
p <- the_plot +
annotation_custom(b1)+
annotation_custom(b2) +
scale_y_continuous(expand=c(0.11,0))
p
ggsave("test.png", p, width = 4, height = 2.5)