How to add a table to a ggplot?
Asked Answered
V

3

7

I am trying to combine (in a single chart) a regular ggplot chart with a table obtained with flextable.

Consider the following example:

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 looks like

enter image description here

but unfortunately I cannot combine it with p1

> p1 + p2
Error: Don't know how to add p2 to a plot

What can we do? Thanks!

Vanadinite answered 22/2, 2020 at 4:24 Comment(2)
What is your expected output?Ruhr
Does this answer your question? Adding table within the plotting region of a ggplot in rRuhr
G
9

You can use fonction flextable::as_raster to get a raster from a flextable and then add with annotation_custom to an empty ggplot object.

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) )

enter image description here

Documentation is here: https://davidgohel.github.io/flextable/articles/offcran/images.html

Gley answered 22/2, 2020 at 8:8 Comment(6)
thanks!! but i am trying to add the table as if it was a plot (not as an annotation that is). how can i get that?Bomb
in other words the table should take as much space as the plot (side by side)Bomb
If I understand correctly, with patchwork, you would have to use p1 + p2 or with cowplot cowplot::plot_grid(p1, p2, nrow = 1, ncol = 2). Is that what you want?Gley
ha actually David the big issue is that as_raster needs install_phantomjs() which is deprecated and hard to install at work. Do you see any other working alternatives?Bomb
using webshot2 is an alternativeGley
it was missing from as_raster args but now its ok - use github versionGley
P
10

tableGrob from the gridExtra package works too

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))

Created on 2020-02-21 by the reprex package (v0.3.0)

Porras answered 22/2, 2020 at 5:3 Comment(0)
G
9

You can use fonction flextable::as_raster to get a raster from a flextable and then add with annotation_custom to an empty ggplot object.

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) )

enter image description here

Documentation is here: https://davidgohel.github.io/flextable/articles/offcran/images.html

Gley answered 22/2, 2020 at 8:8 Comment(6)
thanks!! but i am trying to add the table as if it was a plot (not as an annotation that is). how can i get that?Bomb
in other words the table should take as much space as the plot (side by side)Bomb
If I understand correctly, with patchwork, you would have to use p1 + p2 or with cowplot cowplot::plot_grid(p1, p2, nrow = 1, ncol = 2). Is that what you want?Gley
ha actually David the big issue is that as_raster needs install_phantomjs() which is deprecated and hard to install at work. Do you see any other working alternatives?Bomb
using webshot2 is an alternativeGley
it was missing from as_raster args but now its ok - use github versionGley
R
5

Following 2 ways are there

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)

enter image description here

p1 + geom_table_npc(data = mydf, label = list(mydf),npcx = 0.05, npcy = 0.90, hjust = 0, vjust = 1)

enter image description here

Ruhr answered 22/2, 2020 at 5:0 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.