更改使用 barplot() 创建的图形的宽度

Change the width of a figure created with barplot()

当我使用barplot时,如果名称太大,我看不到栏的名称。例如:

barplot(as.numeric(c(2, 4, 1, 6)), col = c("lightblue"), main="Barplot",
        names.arg=c("This is bar 1...1", "This is bar 1...2",
                    "This is bar 1...3", "This is bar 1...4"), 
        xpd=TRUE, las=1, lwd=1, cex.names=0.5)

当我使用horiz=TRUE时,我看不到左边的名字:

barplot(as.numeric(c(2, 4, 1, 6)), col = c("lightblue"), main="Barplot", 
        names.arg=c("This is \nbar 2...1", "This is bar 2...2",
                    "This is bar 2...3", "This is bar 2...4"), 
        xpd=TRUE, las=1, lwd=1, horiz=TRUE, space=1)

在名称中使用换行符(例如 "This is \nbar 2...1")或减小文本大小,例如 cex.names=0.5 "solves" 问题,但我更愿意添加 space这样名字就合适了。

有没有办法改变整个图形的宽度,包括情节?

horiz=FALSE(默认值)时,一种选择是使用 las=2 绘制垂直于 x 轴的柱名称,并向底部边距添加高度以适应柱的长度名字。要将 space 添加到边距,请使用 par(mar=c(b, l, t, r)),其中 bltr 是给出行数的数字width/height 您希望分别为底部、左侧、顶部和右侧的边距。

例如:

par(mar=c(15, 3, 3, 1)) # 15 line height for bottom margin
barplot(c(2, 4, 1, 6), main="Barplot", las=2,
        names.arg=c("This is my first very long name",
                    "This is my second very long name",
                    "This is my third very long name",
                    "This is my fourth very long name"))

horiz=TRUE时,您可以使用las=1并在左边距添加space:

par(mar=c(3, 15, 3, 1))
barplot(c(2, 4, 1, 6), main="Barplot", las=1, horiz=TRUE,
        names.arg=c("This is my first very long name",
                    "This is my second very long name",
                    "This is my third very long name",
                    "This is my fourth very long name"))

当你Export你可以调整宽度和高度