在单个显示器中绘制几个jpeg图像

时间:2021-01-15 19:48:34

I need to plot and display several jpeg images in a single combined display (or canvas?). For example, suppose I have images {a,b,c,d}.jpg, each of different size, and I would like to plot them on one page in a 2x2 grid. It would be also nice to be able to set a title for each subplot.

我需要在单个组合显示(或画布?)中绘制和显示几个jpeg图像。例如,假设我有不同大小的图像{a,b,c,d} .jpg,我想在2x2网格中将它们绘制在一个页面上。能够为每个子图设置标题也是很好的。

I've been thoroughly looking for a solution, but couldn't find out how to do it, so any ideas would really help. I would preferably use a solution that is based on the EBImage package.

我一直在寻找一个解决方案,但无法找到如何做到这一点,所以任何想法都会有所帮助。我最好使用基于EBImage包的解决方案。

3 个解决方案

#1


4  

There are two ways how to arrange several plots with base graph functions, namely par(mfrow=c(rows,columns)) (substitute rows and columns with integers) and layout(mat) where mat is a matrix like matrix(c(1,2,3,4)).
For further info see ?par, ?layout, and especially Quick-R: Combining Plots.

有两种方法可以如何使用基本图形函数排列多个图形,即par(mfrow = c(行,列))(用整数替换行和列)和布局(mat),其中mat是矩阵矩阵(c(1 ,2,3,4))。有关详细信息,请参阅?par,?layout,尤其是Quick-R:Combining Plots。

However, as your question is about images I don't know if it helps you at all. If not, I am sorry for misinterpreting your question.

但是,由于您的问题是关于图像,我不知道它是否对您有所帮助。如果没有,我很抱歉误解了你的问题。

#2


3  

To add to Henriks solution, a rather convenient way of using the par() function is:

要添加到Henriks解决方案,使用par()函数的一种相当方便的方法是:

jpeg(filename="somefile.jpg")
op <- par(mfrow=c(2,2)
#plot the plots you want
par(op)
dev.off()

This way, you put the parameters back to the old state after you ran the code. Be aware of the fact this is NOT true if one of the plots gave an error.

这样,您在运行代码后将参数重新置于旧状态。请注意,如果其中一个图表出错,则情况并非如此。

Be aware of the fact that R always put the plots in the same order. Using mfrow fills the grid row by row. If you use mfcol instead of mfrow in the code, you fill up column by column.

请注意R总是以相同的顺序放置这些图。使用mfrow逐行填充网格。如果在代码中使用mfcol而不是mfrow,则逐列填充。

Layout is a whole different story. Here you can define in which order the plots have to be placed. So layout(matrix(1:4,nrow=2) does the same as par(mfcol=c(2,2)). But layout(matrix(c(1,4,3,2),ncol=2)) places the first plot lefttop, the next one rightbottom, the third one righttop, and the last one leftbottom.

布局是一个完全不同的故事。在这里,您可以定义图表必须放置的顺序。因此布局(矩阵(1:4,nrow = 2)与par(mfcol = c(2,2))相同。但布局(矩阵(c(1,4,3,2),ncol = 2))放置第一个图左侧,下一个右侧底部,第三个右侧顶部,最后一个左侧底部。

Every plot is completely independent, so the titles you specify using the option main are printed as well. If you want to have more flexibility, you should take a look at lattice plots.

每个绘图都是完全独立的,因此使用选项main指定的标题也会打印出来。如果你想拥有更大的灵活性,你应该看一下格子图。

#3


1  

If you do not want the images in a regular grid (the different sizes could imply this), then you might consider using the subplot function from the TeachingDemos package. The last example in the help page shows using an image as a plotting character, just modify to use your different images and sizes/locations.

如果您不希望图像位于常规网格中(不同的大小可能意味着这一点),那么您可以考虑使用TeachingDemos包中的子图功能。帮助页面中的最后一个示例显示使用图像作为绘图字符,只需修改即可使用不同的图像和大小/位置。

The ms.image function (same package) used with my.symbols is another possibility.

与my.symbols一起使用的ms.image函数(相同的包)是另一种可能性。

#1


4  

There are two ways how to arrange several plots with base graph functions, namely par(mfrow=c(rows,columns)) (substitute rows and columns with integers) and layout(mat) where mat is a matrix like matrix(c(1,2,3,4)).
For further info see ?par, ?layout, and especially Quick-R: Combining Plots.

有两种方法可以如何使用基本图形函数排列多个图形,即par(mfrow = c(行,列))(用整数替换行和列)和布局(mat),其中mat是矩阵矩阵(c(1 ,2,3,4))。有关详细信息,请参阅?par,?layout,尤其是Quick-R:Combining Plots。

However, as your question is about images I don't know if it helps you at all. If not, I am sorry for misinterpreting your question.

但是,由于您的问题是关于图像,我不知道它是否对您有所帮助。如果没有,我很抱歉误解了你的问题。

#2


3  

To add to Henriks solution, a rather convenient way of using the par() function is:

要添加到Henriks解决方案,使用par()函数的一种相当方便的方法是:

jpeg(filename="somefile.jpg")
op <- par(mfrow=c(2,2)
#plot the plots you want
par(op)
dev.off()

This way, you put the parameters back to the old state after you ran the code. Be aware of the fact this is NOT true if one of the plots gave an error.

这样,您在运行代码后将参数重新置于旧状态。请注意,如果其中一个图表出错,则情况并非如此。

Be aware of the fact that R always put the plots in the same order. Using mfrow fills the grid row by row. If you use mfcol instead of mfrow in the code, you fill up column by column.

请注意R总是以相同的顺序放置这些图。使用mfrow逐行填充网格。如果在代码中使用mfcol而不是mfrow,则逐列填充。

Layout is a whole different story. Here you can define in which order the plots have to be placed. So layout(matrix(1:4,nrow=2) does the same as par(mfcol=c(2,2)). But layout(matrix(c(1,4,3,2),ncol=2)) places the first plot lefttop, the next one rightbottom, the third one righttop, and the last one leftbottom.

布局是一个完全不同的故事。在这里,您可以定义图表必须放置的顺序。因此布局(矩阵(1:4,nrow = 2)与par(mfcol = c(2,2))相同。但布局(矩阵(c(1,4,3,2),ncol = 2))放置第一个图左侧,下一个右侧底部,第三个右侧顶部,最后一个左侧底部。

Every plot is completely independent, so the titles you specify using the option main are printed as well. If you want to have more flexibility, you should take a look at lattice plots.

每个绘图都是完全独立的,因此使用选项main指定的标题也会打印出来。如果你想拥有更大的灵活性,你应该看一下格子图。

#3


1  

If you do not want the images in a regular grid (the different sizes could imply this), then you might consider using the subplot function from the TeachingDemos package. The last example in the help page shows using an image as a plotting character, just modify to use your different images and sizes/locations.

如果您不希望图像位于常规网格中(不同的大小可能意味着这一点),那么您可以考虑使用TeachingDemos包中的子图功能。帮助页面中的最后一个示例显示使用图像作为绘图字符,只需修改即可使用不同的图像和大小/位置。

The ms.image function (same package) used with my.symbols is another possibility.

与my.symbols一起使用的ms.image函数(相同的包)是另一种可能性。