添加花括号到ggplot2,然后使用ggsave [英] Add Curly braces to ggplot2 and then use ggsave
问题描述
所以这与这个
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't present.
example:
library(ggplot2)
library(grid)
library(pBrackets)
x <- c(runif(10),runif(10)+2)
y <- c(runif(10),runif(10)+2)
the_plot <- qplot(x=x,y=y) +
scale_x_continuous("",breaks=c(.5,2.5),labels=c("Low types","High types") ) +
theme(axis.ticks = element_blank(),
axis.ticks.length = unit(.85, "cm"))
the_plot
grid.locator(unit="native")
bottom_y <- 284
grid.brackets(220, bottom_y, 80, bottom_y, lwd=2, col="red")
grid.brackets(600, bottom_y, 440, bottom_y, lwd=2, col="red")
ggsave("test.png",width = 4, height = 2.5)
I'm not open to using the RStudio export button as it doesn't properly export my theme font sizes etc. I also need higher resolution than 76 dpi. I need a solution to add curly braces to a ggplot2 graphic and be able to save it using ggsave.
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)
这篇关于添加花括号到ggplot2,然后使用ggsave的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!