使用ggplotGrob时如何避免grob名称中的随机后缀?

问题描述 投票:2回答:1

[我刚刚注意到,每当我在ggplot对象上调用ggplotGrob时,我都会得到不同的grob名称-特别是不同的后缀。当我按全名选择和编辑grob时,这可能是重现代码的问题,因为该名称可以随着新的运行而更改。

例如,panel.background..rect.6在第二轮中变为panel.background..rect.76

library(ggplot2)
library(grid)

p <- ggplot(data = iris, 
            aes(x = Sepal.Length, 
                y = Sepal.Width,
                colour = Species)) +
  geom_point()

# First run
g <- grid.force(ggplotGrob(p))
grid.ls(g)
#> layout
#>   background.1-11-12-1
#>   panel.7-5-7-5
#>     grill.gTree.15
#>       panel.background..rect.6

# Second run
g <- grid.force(ggplotGrob(p))
grid.ls(g)
#> layout
#>   background.1-11-12-1
#>   panel.7-5-7-5
#>     grill.gTree.85
#>       panel.background..rect.76

我以为set.seed()可以解决问题,但类似这样:

set.seed(123)
g <- grid.force(ggplotGrob(p))
grid.ls(g)

每次运行时都给出不同的后缀。

[我知道ggplotGrob(x)实际上是ggplot_gtable(ggplot_build(x)),但是我无法在ggplot_gtableggplot_build中找到/理解任何参数来停止随机后缀。

我可以使用正则表达式来选择grobs的名称,避免使用后缀陷阱,但是想知道是否可以以某种方式取消随机后缀的命名。

r ggplot2 r-grid
1个回答
-2
投票

عزوزعبداللهعبدالرحمنبذوذهعبدالمحسنفايز

© www.soinside.com 2019 - 2024. All rights reserved.