如何将 table 添加到 ggplot?
How to add a table to a ggplot?
我正在尝试(在单个图表中)将常规 ggplot
图表与 flextable
获得的 table 图表结合起来。
考虑以下示例:
library(tidyverse)
library(patchwork)
mydf <- tibble(a = c(1,2,3,4,5,4),
b = c(4,4,4,3,3,3))
p1 <- mydf %>% ggplot(aes(x = a, y = b, color = as.factor(b))) + geom_point()
p2 <- mydf %>% flextable::flextable()
p2
长得像
但不幸的是我无法将其与 p1
结合使用
> p1 + p2
Error: Don't know how to add p2 to a plot
我们能做什么?
谢谢!
有以下2种方式
library(tidyverse)
library(ggpmisc)
mydf <- tibble(a = c(1,2,3,4,5,4),
b = c(4,4,4,3,3,3))
p1 <- mydf %>% ggplot(aes(x = a, y = b, color = as.factor(b))) + geom_point()
p1 + annotate(geom = "table", x = 4.5, y = 3.5, label = list(mydf),
vjust = 1, hjust = 0)
p1 + geom_table_npc(data = mydf, label = list(mydf),npcx = 0.05, npcy = 0.90, hjust = 0, vjust = 1)
gridExtra
包中的 tableGrob
也有效
library(tidyverse)
library(grid)
library(gridExtra)
mydf <- tibble(a = c(1,2,3,4,5,4),
b = c(4,4,4,3,3,3))
p1 <- mydf %>% ggplot(aes(x = a, y = b, color = as.factor(b))) + geom_point()
mytheme <- gridExtra::ttheme_default(
core = list(padding = unit(c(2.5, 2.5), "mm")))
tbl <- tableGrob(mydf, theme = mytheme, rows = NULL)
grid.arrange(p1,
tbl,
nrow = 2,
as.table = TRUE,
heights = c(4, 1))
由 reprex package (v0.3.0)
于 2020-02-21 创建
您可以使用函数 flextable::as_raster
从 flextable 获取栅格,然后使用 annotation_custom
添加到一个空的 ggplot 对象。
library(ggplot2)
library(flextable)
library(grid)
library(cowplot)
library(tidyverse)
mydf <- tibble(a = c(1,2,3,4,5,4),
b = c(4,4,4,3,3,3))
p1 <- mydf %>% ggplot(aes(x = a, y = b, color = as.factor(b))) + geom_point()
ft_raster <- mydf %>% flextable::flextable() %>%
as_raster()
p2 <- ggplot() +
theme_void() +
annotation_custom(rasterGrob(ft_raster), xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf)
cowplot::plot_grid(p1, p2, nrow = 2, ncol = 1, rel_heights = c(4, 1) )
文档在这里:https://davidgohel.github.io/flextable/articles/offcran/images.html
我正在尝试(在单个图表中)将常规 ggplot
图表与 flextable
获得的 table 图表结合起来。
考虑以下示例:
library(tidyverse)
library(patchwork)
mydf <- tibble(a = c(1,2,3,4,5,4),
b = c(4,4,4,3,3,3))
p1 <- mydf %>% ggplot(aes(x = a, y = b, color = as.factor(b))) + geom_point()
p2 <- mydf %>% flextable::flextable()
p2
长得像
但不幸的是我无法将其与 p1
> p1 + p2
Error: Don't know how to add p2 to a plot
我们能做什么? 谢谢!
有以下2种方式
library(tidyverse)
library(ggpmisc)
mydf <- tibble(a = c(1,2,3,4,5,4),
b = c(4,4,4,3,3,3))
p1 <- mydf %>% ggplot(aes(x = a, y = b, color = as.factor(b))) + geom_point()
p1 + annotate(geom = "table", x = 4.5, y = 3.5, label = list(mydf),
vjust = 1, hjust = 0)
p1 + geom_table_npc(data = mydf, label = list(mydf),npcx = 0.05, npcy = 0.90, hjust = 0, vjust = 1)
gridExtra
包中的 tableGrob
也有效
library(tidyverse)
library(grid)
library(gridExtra)
mydf <- tibble(a = c(1,2,3,4,5,4),
b = c(4,4,4,3,3,3))
p1 <- mydf %>% ggplot(aes(x = a, y = b, color = as.factor(b))) + geom_point()
mytheme <- gridExtra::ttheme_default(
core = list(padding = unit(c(2.5, 2.5), "mm")))
tbl <- tableGrob(mydf, theme = mytheme, rows = NULL)
grid.arrange(p1,
tbl,
nrow = 2,
as.table = TRUE,
heights = c(4, 1))
由 reprex package (v0.3.0)
于 2020-02-21 创建您可以使用函数 flextable::as_raster
从 flextable 获取栅格,然后使用 annotation_custom
添加到一个空的 ggplot 对象。
library(ggplot2)
library(flextable)
library(grid)
library(cowplot)
library(tidyverse)
mydf <- tibble(a = c(1,2,3,4,5,4),
b = c(4,4,4,3,3,3))
p1 <- mydf %>% ggplot(aes(x = a, y = b, color = as.factor(b))) + geom_point()
ft_raster <- mydf %>% flextable::flextable() %>%
as_raster()
p2 <- ggplot() +
theme_void() +
annotation_custom(rasterGrob(ft_raster), xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf)
cowplot::plot_grid(p1, p2, nrow = 2, ncol = 1, rel_heights = c(4, 1) )
文档在这里:https://davidgohel.github.io/flextable/articles/offcran/images.html