Arrange Base Plots and Grid.Tables on the Same Page

Arrange base plots and grid.tables on the same page

To combine base plots and grid objects the package gridBase is useful.

A rough worked example base on your layout above

library(grid)
library(gridBase)
library(gridExtra)


layout(matrix(c(1,3, 2,3, 4,3), nrow = 3, ncol = 2, byrow = TRUE))

# First base plot
plot(1:10)

# second base plot
frame()
# Grid regions of current base plot (ie from frame)
vps <- baseViewports()
pushViewport(vps$inner, vps$figure, vps$plot)
# Table grob
grob <- tableGrob(iris[1:2,1:2])
grid.draw(grob)

popViewport(3)

# third base plot
plot(1:10)

# fourth
frame()
vps <- baseViewports()
pushViewport(vps$inner, vps$figure, vps$plot)
grid.draw(grob)
popViewport(3)

Which gives

Sample Image

Arrange multiple plots into one grid

Since you are not using grid based graphics system, you can use par() to arrange your figures.

See if this works:

a <- c(0.1,0.5,1,2,5,10,100)
b <- c(0.1,0.5,1,2,5,10,100)

par(mfrow=c(7,7), mar=c(2,2,2,0))

for(m in 1:7)
{
...

Adding a small table to a base graphics multiple plot chart

With plotrix you can modify your code as follows:

library(plotrix)
Table1<- data.frame( a=1:6,b=2:7,c=3:8)
layout(matrix(c(1:4), 2, 2, byrow = FALSE), widths=c(1,1), heights=c(1,1))
plot(1:6)
plot(2:7)
plot(3:8)
plot.new()
addtable2plot(0,0,Table1,
xpad=6, ypad=2,
bty='o',
display.rownames = TRUE,
hlines = TRUE,
vlines = TRUE,
title = "The table")

Sample Image

Output Plot and Table to Same Page for Each Group

since you want the tableGrob to be outside the plot panel and not inside, you shouldn't use annotation_custom, but arrangeGrob to arrange a plot and a table on a page. The list of grobs can then be printed page by page in the pdf device.

library(plyr)
plots <- dlply(iris, "Species", function(d) {
arrangeGrob(qplot(1,1), tableGrob(head(d)))
})

pdf("multipage.pdf")
plots
dev.off()

R grid.table - table keeps plotting on top of other plots

You can use grid.newpage() to get a new page in your panel.

library(grid)
d <- head(iris[,1:3])
plot(d)
library(gridExtra)

grid.newpage()

grid.table(d)

multiple different sized plots in same window in R?

EDIT -- fixed to shrink third chart per OP.

library(patchwork)
design <- c( # we specify the top, left, bottom, and right
area(1, 1, 4, 4), # coordinates for each of the three plots
area(1, 5, 2, 6),
area(3, 5)
)
allPlots[[1]] + allPlots[[2]] + allPlots[[3]] +
plot_layout(design = design)

Sample Image



Related Topics



Leave a reply



Submit