将curly大括号添加到ggplot2然后使用ggsave

时间:2021-07-08 00:09:12

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.

所以这与这个问题非常相关,这个答案是一个很好的解决方案。问题是,当我尝试使用ggsave导出绘图时,花括号不存在。

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.

我没有打开使用RStudio导出按钮,因为它没有正确导出我的主题字体大小等。我还需要比76 dpi更高的分辨率。我需要一个解决方案来添加花括号到ggplot2图形,并能够使用ggsave保存它。

2 个解决方案

#1


10  

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?

我不理解grid.brackets中使用的逻辑,但如果有一个只能返回一个grob而不绘制它的bracketGrob函数会有所帮助。也许可以通过功能请求联系维护者?

Anyway, assuming such a function was available, it can be fed to annotation_custom making it compatible with ggsave.

无论如何,假设这样的函数可用,它可以被提供给annotation_custom,使其与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)

将curly大括号添加到ggplot2然后使用ggsave

#2


1  

Well I figured you could do something with devices, as an alternative to ggsave, and I finally got this to work. It was more effort than it should have been because R-Studio somehow gets confused about which devices are actually open or closed (off). So you have to reset your R session sometimes. Checking dev.list() a lot helps. Sort of...

好吧,我认为你可以用设备做一些事情,作为ggsave的替代品,我终于让它工作了。由于R-Studio在某种程度上对哪些设备实际打开或关闭(关闭)感到困惑,因此它应该付出更多的努力。所以你有时必须重置你的R会话。检查dev.list()有很大帮助。有点...

But after a bit of testing this sequence works fairly reliably.

但经过一些测试后,这个序列运行得相当可靠。

I tested it with jpeg too because I can look at the resolution with the file property command in windows to see that the resolution I specified (200 ppi) is getting through:

我也用jpeg测试了它,因为我可以在windows中使用file属性命令查看分辨率,看看我指定的分辨率(200 ppi)是通过的:

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

# User has to click here to specify where the brackets go
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")

#dev.copy(png,"mypng.png",height=1000,width=1000,res=200)
dev.copy(jpeg,"myjpg.jpg",height=1000,width=1000,res=200) 
dev.off()

The image: 将curly大括号添加到ggplot2然后使用ggsave

The properties:

将curly大括号添加到ggplot2然后使用ggsave

#1


10  

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?

我不理解grid.brackets中使用的逻辑,但如果有一个只能返回一个grob而不绘制它的bracketGrob函数会有所帮助。也许可以通过功能请求联系维护者?

Anyway, assuming such a function was available, it can be fed to annotation_custom making it compatible with ggsave.

无论如何,假设这样的函数可用,它可以被提供给annotation_custom,使其与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)

将curly大括号添加到ggplot2然后使用ggsave

#2


1  

Well I figured you could do something with devices, as an alternative to ggsave, and I finally got this to work. It was more effort than it should have been because R-Studio somehow gets confused about which devices are actually open or closed (off). So you have to reset your R session sometimes. Checking dev.list() a lot helps. Sort of...

好吧,我认为你可以用设备做一些事情,作为ggsave的替代品,我终于让它工作了。由于R-Studio在某种程度上对哪些设备实际打开或关闭(关闭)感到困惑,因此它应该付出更多的努力。所以你有时必须重置你的R会话。检查dev.list()有很大帮助。有点...

But after a bit of testing this sequence works fairly reliably.

但经过一些测试后,这个序列运行得相当可靠。

I tested it with jpeg too because I can look at the resolution with the file property command in windows to see that the resolution I specified (200 ppi) is getting through:

我也用jpeg测试了它,因为我可以在windows中使用file属性命令查看分辨率,看看我指定的分辨率(200 ppi)是通过的:

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

# User has to click here to specify where the brackets go
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")

#dev.copy(png,"mypng.png",height=1000,width=1000,res=200)
dev.copy(jpeg,"myjpg.jpg",height=1000,width=1000,res=200) 
dev.off()

The image: 将curly大括号添加到ggplot2然后使用ggsave

The properties:

将curly大括号添加到ggplot2然后使用ggsave