Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将表格添加到ggplot2,并在r中使用镶嵌面_包裹_R_Ggplot2 - Fatal编程技术网

将表格添加到ggplot2,并在r中使用镶嵌面_包裹

将表格添加到ggplot2,并在r中使用镶嵌面_包裹,r,ggplot2,R,Ggplot2,我想添加一个表,其中包含一些信息,这些信息在facet中的每个面板中都会有所不同。 我使用的是ggplot2和facet_网格 假设我想给每个面板添加一些描述性统计数据,它们不一定相同。 这些统计数据放在我为此目的制作的df中 我找到了一些方法将这些表添加到图表中,但是: 就我而言,Annotate将为刻面中的所有面板提供相同的表 我真的很想使用面_扭曲的简单性,而不是网格_额外 桌子的位置对我来说并不重要,但我不想让它与图表重叠 我的目标是将这两个答案混合在一起: 第一个答案(带注释-)对我

我想添加一个表,其中包含一些信息,这些信息在facet中的每个面板中都会有所不同。 我使用的是ggplot2和facet_网格

假设我想给每个面板添加一些描述性统计数据,它们不一定相同。 这些统计数据放在我为此目的制作的df中

我找到了一些方法将这些表添加到图表中,但是:

  • 就我而言,Annotate将为刻面中的所有面板提供相同的表
  • 我真的很想使用面_扭曲的简单性,而不是网格_额外 桌子的位置对我来说并不重要,但我不想让它与图表重叠

    我的目标是将这两个答案混合在一起:

    第一个答案(带注释-)对我不起作用,因为我希望每个绘图中的表都是唯一的。) 第二个答案更好,但我不希望它重叠或隐藏图形中的一些细节,在每个面板中,线/散点位于不同的位置,因此我不能这样使用它。我想它被附加就像在注释

    试试这个

    library(ggplot2)
    library(tibble)
    library(gridExtra)
    library(grid)
    
    GeomCustom <- ggproto(
      "GeomCustom",
      Geom,
      setup_data = function(self, data, params) {
        data <- ggproto_parent(Geom, self)$setup_data(data, params)
        data
      },
    
      draw_group = function(data, panel_scales, coord) {
        vp <- grid::viewport(x=data$x, y=data$y)
        g <- grid::editGrob(data$grob[[1]], vp=vp)
        ggplot2:::ggname("geom_custom", g)
      },
    
      required_aes = c("grob","x","y")
    
    )
    
    geom_custom <-  function(mapping = NULL,
               data = NULL,
               stat = "identity",
               position = "identity",
               na.rm = FALSE,
               show.legend = NA,
               inherit.aes = FALSE,
               ...) {
        layer(
          geom = GeomCustom,
          mapping = mapping,
          data = data,
          stat = stat,
          position = position,
          show.legend = show.legend,
          inherit.aes = inherit.aes,
          params = list(na.rm = na.rm, ...)
        )
    }
    
    
    gl <- list(tableGrob(iris[1:2,1:3]), 
               tableGrob(iris[1:4,1:3]),
               tableGrob(iris[1:3,1:3]),
               tableGrob(iris[1:2,1:2]))
    
    dummy <- tibble(f=letters[1:4], grob = gl )
    
    d <- tibble(x=rep(1:3, 4), f=rep(letters[1:4], each=3))
    
    ggplot(d, aes(x,x)) +
      facet_wrap(~f) +
      theme_bw() +
      geom_custom(data=dummy, aes(grob=grob), x = 0.5, y = 0.5)
    
    库(ggplot2)
    图书馆(tibble)
    图书馆(gridExtra)
    图书馆(网格)
    可能的副本
    
    library(ggplot2)
    library(tibble)
    library(gridExtra)
    library(grid)
    
    GeomCustom <- ggproto(
      "GeomCustom",
      Geom,
      setup_data = function(self, data, params) {
        data <- ggproto_parent(Geom, self)$setup_data(data, params)
        data
      },
    
      draw_group = function(data, panel_scales, coord) {
        vp <- grid::viewport(x=data$x, y=data$y)
        g <- grid::editGrob(data$grob[[1]], vp=vp)
        ggplot2:::ggname("geom_custom", g)
      },
    
      required_aes = c("grob","x","y")
    
    )
    
    geom_custom <-  function(mapping = NULL,
               data = NULL,
               stat = "identity",
               position = "identity",
               na.rm = FALSE,
               show.legend = NA,
               inherit.aes = FALSE,
               ...) {
        layer(
          geom = GeomCustom,
          mapping = mapping,
          data = data,
          stat = stat,
          position = position,
          show.legend = show.legend,
          inherit.aes = inherit.aes,
          params = list(na.rm = na.rm, ...)
        )
    }
    
    
    gl <- list(tableGrob(iris[1:2,1:3]), 
               tableGrob(iris[1:4,1:3]),
               tableGrob(iris[1:3,1:3]),
               tableGrob(iris[1:2,1:2]))
    
    dummy <- tibble(f=letters[1:4], grob = gl )
    
    d <- tibble(x=rep(1:3, 4), f=rep(letters[1:4], each=3))
    
    ggplot(d, aes(x,x)) +
      facet_wrap(~f) +
      theme_bw() +
      geom_custom(data=dummy, aes(grob=grob), x = 0.5, y = 0.5)