【问题标题】:`gggrid` additional arguments in function`gggrid` 函数中的附加参数
【发布时间】:2023-01-07 22:37:45
【问题描述】:

gggrid 允许直接使用网格对象(grobs)修改 ggplot2 图。函数grid_panel() 接受 grob 或函数作为第一个参数。我正在寻找将附加参数传递给此函数的方法(除了 datacoords)。

只要我坚持使用默认参数,它似乎就可以工作,但是一旦我设置了这个参数,我就会收到一个错误。

library(ggplot2)
library(gggrid)
data("mtcars")

lab1 <- function(data, coords, label = "ABC") {
  textGrob(label)
}

# This works fine
ggplot(mtcars, aes(x=disp, y=mpg)) +
  geom_point() +
  grid_panel(lab1)

# This does not work
ggplot(mtcars, aes(x=disp, y=mpg)) +
  geom_point() +
  grid_panel(lab1(label = "BCD"))
# Error in lab1(label = "BCD") : 
#  argument "data" is missing, with no default

【问题讨论】:

    标签: r ggplot2 grid gggrid


    【解决方案1】:

    使用来自 purrr 的 partial。仅添加/修改了以## 结尾的行。

    library(gggrid)
    library(ggplot2)
    library(purrr) ##
    
    lab1 <- function(data, coords, label = "ABC") {
      textGrob(label)
    }
    
    ggplot(mtcars, aes(x=disp, y=mpg)) +
      geom_point() +
      grid_panel(partial(lab1, label = "BCD")) ##
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-16
      • 2022-11-15
      相关资源
      最近更新 更多