在使用R绘制图形的时候,经常出现x轴刻度名称过长导致显示不完整的情况。
例如
count = c(10,20,23,32,16,29,15,18,21)
names(count) = c("redred", "orangeorange", "yellowyellow",
"greengreen", "blueblue", "purplepurple", "whitewhite", "greygrey", "pinkpink")
barplot(count, las=2, xlab="Colors")
显示结果如下

解决办法
调用par()函数设置外边框的大小,默认外边框的大小为mar=c(5.1,4.1,4.1,2.1), 分别对应下,左,上,右四个外边框。
我们可以将下边框调大点:
par(mar=c(8, 4.1, 4.1, 2.1))
barplot(count, las=2, xlab="Colors")

x刻度标签的问题解决了,但是x坐标标签又发生重叠了,怎么解决呢?
我们需要将横坐标标签往下移,首先,设置xlab为空,然后调用mtext函数在外边框中输出文字。
par(mar=c(8, 4.1, 4.1, 2.1))
barplot(count, las=2, xlab="")
mtext(text="Colors", side=1, line=6.5) # side表明是下外边框

网友评论