2012-01-09 103 views
0

你好我想用下面的代码来创建一个堆叠barplot:x轴标签不匹配吧

test <- as.matrix(read.csv(file="test4.csv",sep=",",head=TRUE)) 
test <- test[,2:ncol(test)] 
pdf(file="test.pdf", height=4, width=6) 
par(lwd = 0.3) 
barplot(test, space=0.4, xaxt='n', ann=FALSE) 
axis(1, cex.axis=0.25, las=2, at=1:ncol(test), space=0.4, labels=colnames(test)) 
dev.off() 

我也得到: enter image description here

正如你可以看到在标签x轴与图中的条不匹配。此外,蜱是巨大的。 你们能帮我美化x轴吗?非常感谢

+0

老实说,我无法读取X轴上的标签,所以我不知道他们应该匹配或不匹配。 – 2012-01-09 23:20:31

+0

对不起,这是更大的版本。生病继续努力获得更大更清晰的版本 – 2012-01-09 23:23:59

回答

3

尝试调用的返回值存储到barplot()在名为对象,然后通过在该at=说法axis()

xLabLocs <- barplot(test, space=0.4, xaxt='n', ann=FALSE) 
axis(1, cex.axis=0.25, las=2, at=xLabLocs, 
    space=0.4, labels=colnames(test)) 

这可能看起来很奇怪,但它解释在?barplot帮助文件的Value部分:

Value: 
    A numeric vector (or matrix, when ‘beside = TRUE’), say ‘mp’, 
    giving the coordinates of _all_ the bar midpoints drawn, useful 
    for adding to the graph. 

您刚刚提出了(容易造成的)错误,假设条中心的x轴坐标位于1:n,其中n是条的数量。这并不一定是真实的,所以一次致电barplot()将同时满足以下两点很好:(a)将棒图绘制为其副作用;和(b)返回必要的x轴坐标作为其返回值。

+0

的确,非常感谢。现在是我还是y轴离情节还很远? – 2012-01-09 23:33:54

+1

如果你这么认为,你可以首先通过'barplot(...,axes = FALSE)'来抑制* both *轴的绘图,然后使用'pos ='参数来更好地定位y轴()'。 ('axis(2,pos = 0.3)'将会把y轴移动到默认位置的右边。) – 2012-01-09 23:41:15