Home > Net >  how to put a plot and a table together using grid.arrange
how to put a plot and a table together using grid.arrange

Time:12-04

I have a plot and a table, and I would like to combine them into a plot. how should I do that.

Here is my codes:

df<-structure(list(AEDECOD = c("Hypoxia", "Malignant pleural effusion", 
"Decubitus ulcer", "Nausea"), ADY = c(13, 13, 13, 14)), row.names = c(NA, 
-4L), class = "data.frame")

tbl <-structure(list(`Analysis Relative Day` = 13, `AE Type` = "SER", 
    `Adverse Event` = "Hypoxia/Malignant pleural effusion"), row.names = c(NA, 
-1L), class = c("tbl_df", "tbl", "data.frame"))

p1<- ggplot(data =df, aes(x = ADY, y = AEDECOD))  geom_point()
p2 <-grid.arrange(p1, tbl,
nrow = 2,as.table = TRUE)

print(p2)

I got the error codes:

Error: Input must be a vector, not a <viewport> object.

If you know any other way to do the same thing, I would like to learn that as well.

CodePudding user response:

We may use ggarrange after converting the tibble to ggtexttable

library(ggpubr)
ggarrange(p1, ggtexttable(tbl), nrow = 2)

Or using the OP's code

library(gridExtra)
grid.arrange(p1, ggtexttable(tbl),
      nrow = 2,as.table = TRUE)

-output

enter image description here

  •  Tags:  
  • r
  • Related