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

时间:2015-12-07 09:27:37

标签: r bar-chart sweave

当我使用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“解决”问题但我更愿意添加空格以便名字合适。

是否有能力改变整个人物的宽度,包括情节?

2 个答案:

答案 0 :(得分:2)

horiz=FALSE(默认值)时,一个选项是使用las=2绘制垂直于x轴的条形名称,并将高度添加到底部边距以适应名称的长度。要向边距添加空格,请使用par(mar=c(b, l, t, r)),其中bltr是数字,给出宽度/高度的行数你想要分别是底部,左边,上边和右边距。

例如:

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

enter image description here

horiz=TRUE时,您可以使用las=1并向左边距添加空格:

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

enter image description here

答案 1 :(得分:0)

Export时,您可以调整宽度和高度 enter image description here