【问题标题】:Adjust size/width of barplot plots when using mfrow使用 mfrow 时调整条形图的大小/宽度
【发布时间】:2019-09-24 00:54:00
【问题描述】:

我想使用基数 R 生成两个图形,都显示条形图。第一个图应该包含两个条形图,第二个图应该包含四个条形图。

我使用 par(mfrow = c(...)) 在一个图中排列多个条形图。 我自己制作数字没有问题,但是当我保存数字时,条形宽度和刻度标签的大小不同。

据我了解,当我生成带有四个条形图的第二个图形并在导出时选择第一个图形的两倍宽度时,条形图和标签应在文件中以相同大小显示。但是,标签要小得多,并且第二个图中的条具有不同的宽度。谁能告诉我为什么?

这里是一个简单的例子:

png(filename="plot1.png", width=200, height=300, bg="white")
par(mfrow = c(1, 2), mar = c(1, 2, 1, 1), oma = c(0, 0, 0, 0))

barplot(height = c(2,3), width = 1, xlim = c(0,2))
barplot(height = c(2,3), width = 1, xlim = c(0,2))
dev.off()

png(filename="plot2.png", width=400, height=300, bg="white")
par(mfrow = c(1, 4), mar = c(1, 2, 1, 1), oma = c(0, 0, 0, 0))

barplot(height = c(2,3), width = 1, xlim = c(0,2))
barplot(height = c(2,3), width = 1, xlim = c(0,2))
barplot(height = c(2,3), width = 1, xlim = c(0,2))
barplot(height = c(2,3), width = 1, xlim = c(0,2))
dev.off()

情节 1:

情节 2:

【问题讨论】:

  • 很遗憾没有。即使我按照此答案中的建议使用代码导出图,我的条形宽度和刻度标签不等问题仍然存在。我编辑了我的问题以使其更清楚。

标签: r plot graph bar-chart par


【解决方案1】:

parpdf(width, height) 可能应该相等。

png(filename="plot1.png", width=400, height=300, bg="white")
par(mfrow=c(1, 4), mar=c(1, 2, 1, 1), oma=c(0, 0, 0, 0))
replicate(2, barplot(height=c(2,3), width=1, xlim=c(0,2)))
dev.off()

png(filename="plot2.png", width=400, height=300, bg="white")
par(mfrow=c(1, 4), mar=c(1, 2, 1, 1), oma=c(0, 0, 0, 0))
replicate(4, barplot(height=c(2,3), width=1, xlim=c(0,2)))
dev.off()

另一种解决方案是使用layout

def.par <- par(no.readonly=TRUE) # save par default, for resetting...

# 1 x 2 plot
layout(matrix(c(1:2, 0, 0), nrow=1, ncol=4, byrow=TRUE))
layout.show(n=2)  # to inspect layout                        # MARK
replicate(2, barplot(height=c(2,3), width=1, xlim=c(0,2)))

# 1 x 4 plot
layout(matrix(c(1:4), nrow=1, ncol=4, byrow=TRUE))
layout.show(n=4)  # to inspect layout
replicate(4, barplot(height=c(2,3), width=1, xlim=c(0,2)))

# 2 x 4 plot
layout(matrix(c(1:2, 0, 0, 3:6), nrow=2, ncol=4, byrow=TRUE))
layout.show(n=6)  # to inspect layout
replicate(2, barplot(height=c(2,3), width=1, xlim=c(0,2)))
replicate(4, barplot(height=c(2,3), width=1, xlim=c(0,2)))

par(def.par)  # reset to default

但是,这两种解决方案都会带来半空的情节 1,原因可以在上面的代码# MARK 中看到。

我们可以使用magick 包将第一个情节“切割”为所需的内容。首先我们用第二种方法创建*.pngs。

clr <- "#ED7C22"  # color

png(filename="plot1.png", width=400, height=300, bg="white")
layout(matrix(c(1:2, 0, 0), nrow=1, ncol=4, byrow=TRUE))
replicate(2, barplot(height=c(2,3), width=1, xlim=c(0,2), col=clr, border=0))
dev.off()

png(filename="plot2.png", width=400, height=300, bg="white")
layout(matrix(c(1:4), nrow=1, ncol=4, byrow=TRUE))
replicate(4, barplot(height=c(2,3), width=1, xlim=c(0,2), col=clr, border=0))
dev.off()

现在,我们使用image_chopplot1.png 修剪到它的左半边。

library(magick)
(i <- image_read("plot1.png"))
i.chopped <- image_chop(i, "200x+200")  # says: trim by 200px at pos. 200

最后,我们导出切碎的图像。

image_write(i.chopped, path="plot1.ch.png", format="png")

情节 1(“切碎”)

情节 2

【讨论】:

  • 感谢您的回答。我试过这个,但后来我得到了第一个情节的右半部分作为空白。当我希望图表仅占文档的半页时,这是不利的。
  • 你说得对,这太丑了。有一个应用 magick::image_chop 的解决方案,请参阅更新。
  • 这是解决此问题的好方法。谢谢你。但是我仍然不明白为什么当我使用四个而不是两个具有两倍图像宽度的图时,R 会改变图中的比例。这种现象没有解释吗?
  • @VincentH。绘图会在内部自动调整,因此不会创建可用空间。因此,该解决方案被推广,可能是因为这种情况发生的频率比您需要具有相同纵横比的不同大小的图集更频繁。但是,我错过了barplot 中的aspect 选项。由于plot 具有此选项,因此可能值得检查plot 是否在设置此选项时产生相同的方面。
猜你喜欢
  • 2014-06-24
  • 2010-10-17
  • 1970-01-01
  • 1970-01-01
  • 2013-11-03
  • 1970-01-01
  • 2013-04-28
  • 2011-12-25
  • 2013-09-14
相关资源
最近更新 更多