I need to make an empty plot. This is the best could I come up with.
plot(0, xaxt = 'n', yaxt = 'n', bty = 'n', pch = '', ylab = '', xlab = '')
Any simpler solutions?
P.S.: completely empty, no axis etc.
The following does not plot anything in the plot and it will remain empty.
plot(NULL, xlim=c(0,1), ylim=c(0,1), ylab="y label", xlab="x lablel")
This is useful when you want to add lines or dots afterwards within a for loop or something similar. Just remember to change the xlim and ylim values based on the data you want to plot.
As a side note:
This can also be used for Boxplot, Violin plots and swarm plots. for those remember to add add = TRUE to their plotting function and also specify at = to specify on which number you want to plot them (default is x axis unless you have set horz = TRUE in these functions.
Adam, following your comment above ("I wanted the empty plot to serve as filler in a multiplot (mfrow) plot."), what you actually want is the mfg option
par(mfg=c(row,column))
- which controls where you want to put the next plot. For instance, to put a plot in the middle of a 3x3 multiplot, do
par(mfrow=c(3,3))
par(mfg=c(2,2))
plot(rnorm(10))
If anyone is looking for a ggplot2 solution, you can use either cowplot or patchwork packages
library(ggplot2)
### examples from cowplot vignettes
plot.mpg <- ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) +
geom_point(size = 2.5)
plot.diamonds <- ggplot(diamonds, aes(clarity, fill = cut)) +
geom_bar() +
theme(axis.text.x = element_text(angle = 0, vjust = 0.5))
library(cowplot)
### use NULL
plot_grid(plot.mpg, NULL, NULL, plot.diamonds,
labels = c("A", "B", "C", "D"),
ncol = 2
)
# Note: if you want to initialize an empty drawing canvas, use ggdraw()

library(patchwork)
### use plot_spacer()
plot.mpg + plot_spacer() + plot_spacer() + plot.diamonds +
plot_layout(ncol = 2) +
plot_annotation(
title = "Plot title",
subtitle = "Plot subtitle",
tag_levels = "A",
tag_suffix = ")"
)

Created on 2019-03-17 by the reprex package (v0.2.1.9000)