如何绘制空图?

发布于 2024-10-13 17:00:39 字数 176 浏览 6 评论 0原文

我需要做一个空的情节。这是我能想到的最好的办法了。

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

有更简单的解决方案吗?

PS:完全空的,没有轴等。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

夏日浅笑〃 2024-10-20 17:00:39

怎么样:

plot.new()

How about something like:

plot.new()
泛泛之交 2024-10-20 17:00:39

我建议有人需要制作空图,以便稍后在其上添加一些图形。因此,

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

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

I suggest that someone needs to make empty plot in order to add some graphics on it later. So, using

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

you can specify the axes limits of your graphic.

忘年祭陌 2024-10-20 17:00:39

以下不会在图中绘制任何内容,并且它将保持为空。

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

这将产生:

“在此处输入图像描述”

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

附注:
这也可用于箱线图、小提琴图和群体图。对于那些记得将 add = TRUE 添加到其绘图函数中,并指定 at = 来指定要在哪个数字上绘制它们(默认为 x 轴,除非您设置了这些函数中的 horz = TRUE

如果您想触发新的绘图,例如当您使用 layout() 时,最好使用 plot.例如,在下面,我们将在第一行的两侧添加空图以添加一些填充:

layout(mat = matrix(c(1,2,2,3,
                      4,4,5,5),
                    nrow = 2,
                    byrow = TRUE))
plot.new()
plot(iris[, 1:2])
plot.new()
plot(iris[, 1:2])
plot(iris[, 1:2])

在此图中,我手动注释了区域以阐明我的意思:

在此处输入图像描述

以下是您实际得到的内容:

在此处输入图像描述

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

Which will produce:

enter image description here

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.

If you want to trigger a new plot, for example when you are using layout(), you are better off with plot.new(). For example in the following we are adding empty plots to either side of the first row to add some padding:

layout(mat = matrix(c(1,2,2,3,
                      4,4,5,5),
                    nrow = 2,
                    byrow = TRUE))
plot.new()
plot(iris[, 1:2])
plot.new()
plot(iris[, 1:2])
plot(iris[, 1:2])

In this plot I have manually annotated the regions to clarify what I mean:

enter image description here

And the following is what you actually get:

enter image description here

妳是的陽光 2024-10-20 17:00:39

这比您原来的解决方案稍微简单一些:

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

This is marginally simpler than your original solution:

plot(0,type='n',axes=FALSE,ann=FALSE)
残龙傲雪 2024-10-20 17:00:39

Adam,按照您上面的评论(“我希望空图作为多图(mfrow)图中的填充物。”),您真正想要的是 mfg 选项

    par(mfg=c(row,column))

- 它控制您想要放置下一个图的位置。例如,要将一个图放在 3x3 多重图的中间,请执行以下操作

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

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))
忘东忘西忘不掉你 2024-10-20 17:00:39

你需要一个新的绘图窗口,还需要一个坐标系,所以你需要 plot.new()plot.window(),然后你就可以开始添加图形元素:

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

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

示例图

You need a new plot window, and also a coordinate system, so you need plot.new() and plot.window(), then you can start to add graph elements:

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

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

example plot

诗酒趁年少 2024-10-20 17:00:39

您的解决方案有一个 plot.new() 没有的兴趣:在您“绘制”的空图中,您可以使用 text(x = .. ., y = ..., your_text)

There is an interest in your solution that plot.new() hasn't though: in the empty plot you "draw" you can write text at specified coordinates with text(x = ..., y = ..., your_text).

无声静候 2024-10-20 17:00:39
grid.newpage() ## If you're using ggplot

grid() ## If you just want to activate the device.
grid.newpage() ## If you're using ggplot

grid() ## If you just want to activate the device.
我纯我任性 2024-10-20 17:00:39

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

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

An empty plot with some texts which are set position.

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")
白色秋天 2024-10-20 17:00:39

如果有人正在寻找 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 包于 2019 年 3 月 17 日创建(v0.2.1.9000 )

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)

落叶缤纷 2024-10-20 17:00:39

另一个简单的 ggplot2 选项是使用 geom_blank ,如下所示:

library(ggplot2)
ggplot() +
  geom_blank()

< img src="https://i.sstatic.net/UxDqf.png" alt="">

创建于 2022 年 8 月 21 日,使用 reprex v2.0.2

如您所见,该图是空白的。

Another simple ggplot2 option is using geom_blank like this:

library(ggplot2)
ggplot() +
  geom_blank()

Created on 2022-08-21 with reprex v2.0.2

As you can see the plot is blank.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文