如何绘制空图?

时间:2011-01-24 18:31:29

标签: r ggplot2 plot

我需要制作一个空图。这是我能想到的最好的。

plot(0, xaxt = 'n', yaxt = 'n', bty = 'n', pch = '', ylab = '', xlab = '')

任何更简单的解决方案?

P.S。:完全空,没有轴等。

10 个答案:

答案 0 :(得分:97)

如下:

plot.new()

答案 1 :(得分:52)

我建议有人需要制作空图,以便稍后添加一些图形。所以,使用

plot(1, type="n", xlab="", ylab="", xlim=c(0, 10), ylim=c(0, 10))

您可以指定图形的轴限制。

答案 2 :(得分:21)

这比原始解决方案简单得多:

plot(0,type='n',axes=FALSE,ann=FALSE)

答案 3 :(得分:17)

以下内容未在情节中绘制任何内容,它将保持为空。

plot(NULL, xlim=c(0,1), ylim=c(0,1), ylab="y label", xlab="x lablel")

当您想在for循环或类似内容中添加线条或点时,这非常有用。只需记住根据您要绘制的数据更改xlimylim值。

答案 4 :(得分:15)

亚当,按照上面的评论(“我希望空图在多色(mfrow)图中充当填充物。”),你真正想要的是mfg选项

    par(mfg=c(row,column))

- 控制下一个绘图的位置。例如,要将绘图放在3x3多重绘图的中间,请执行

    par(mfrow=c(3,3))
    par(mfg=c(2,2))
    plot(rnorm(10))

答案 5 :(得分:13)

您需要一个新的绘图窗口以及一个坐标系,因此您需要plot.new()plot.window(),然后您可以开始添加图元素:

plot.new( )
plot.window( xlim=c(-5,5), ylim=c(-5,5) )

points( rnorm(100), rnorm(100) )
axis( side=1 )

example plot

答案 6 :(得分:4)

您的解决方案对plot.new()没有感兴趣:在您“绘制”的空图中,您可以使用text(x = ..., y = ..., your_text)在指定坐标处书写文字。

答案 7 :(得分:3)

grid.newpage() ## If you're using ggplot

grid() ## If you just want to activate the device.

答案 8 :(得分:1)

带有一些已设置位置的文本的空图。

plot(1:10, 1:10,xaxt="n",yaxt="n",bty="n",pch="",ylab="",xlab="", main="", sub="")
mtext("eee", side = 3, line = -0.3, adj = 0.5)
text(5, 10.4, "ddd")
text(5, 7, "ccc")

答案 9 :(得分:0)

如果任何人正在寻找ggplot2解决方案,则可以使用cowplotpatchwork软件包

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

reprex package(v0.2.1.9000)于2019-03-17创建