将表格添加到ggplot2,并在r中使用镶嵌面_包裹
我想添加一个表,其中包含一些信息,这些信息在facet中的每个面板中都会有所不同。 我使用的是ggplot2和facet_网格 假设我想给每个面板添加一些描述性统计数据,它们不一定相同。 这些统计数据放在我为此目的制作的df中 我找到了一些方法将这些表添加到图表中,但是:将表格添加到ggplot2,并在r中使用镶嵌面_包裹,r,ggplot2,R,Ggplot2,我想添加一个表,其中包含一些信息,这些信息在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)