Home > front end >  In R, help converting a regular table into a ggplot table with borders?
In R, help converting a regular table into a ggplot table with borders?

Time:09-28

Here is my current table code:

reftable <- matrix(c("181198", "234321", "306789", "16000234"), ncol = 1, byrow = TRUE)
colnames(reftable) <- c("Score")
rownames(reftable) <- c("How I Met Your Mother", "There Are No Ifs, Ands, Or Buts in the Land of Fantasies and Dreams", "Leo and Carrie Simply Cannot Get Along", "Parry Needs A Dentist")
reftable <- as.table(reftable)
reftable

However, when I knit this code to a pdf, all the numbers turn into hashtags and it's hard to read because there are no border lines. How do I convert this into a ggplot table so that there are border lines on the tables and the inputted numbers are preserved?

CodePudding user response:

library gridExtra may help you

library(gridExtra)
pdf("reftable.pdf", height = 11, width = 8.5)
grid.table(reftable)
dev.off()

enter image description here

There was a simple way using ggtexttable

ggtexttable(reftable)

CodePudding user response:

I usually use flextable to output tables/dataframes to pdf output from rmarkdown:

library(flextable)
library(dplyr)
reftable <- matrix(c("181198", "234321", "306789", "16000234"), ncol = 1, byrow = TRUE)
colnames(reftable) <- c("Score")
rownames(reftable) <- c("How I Met Your Mother", "There Are No Ifs, Ands, Or Buts in the Land of Fantasies and Dreams", "Leo and Carrie Simply Cannot Get Along", "Parry Needs A Dentist")
reftable <- as.table(reftable)
reftable %>% 
  as.data.frame() %>% 
  flextable() %>% 
  border_inner()

border_inner() gives all inside border lines. You can control borders with different options: https://davidgohel.github.io/flextable/reference/border_inner_h.html and control size

  •  Tags:  
  • r
  • Related