2014-04-17 109 views
1

当使用{graphics}绘制堆叠barplot时,出现x轴未正确缩放的问题,滴答图未正确对齐条形图,而使轴太短。为barplot正确缩放x轴

# dummy data 
mat <- structure(c(0L, 5L, 7L, 10L, 12L, 14L, 16L, 18L, 20L, 22L, 24L, 
       26L, 28L, 30L, 32L, 34L, 36L, 38L, 40L, 42L, 44L, 46L, 48L, 50L, 
       52L, 54L, 56L, 58L, 60L, 62L, 63L, 64L, 0L, 0L, 0L, 3L, 0L, 0L, 
       1L, 0L, 0L, 0L, 5L, 0L, 1L, 4L, 0L, 9L, 0L, 0L, 1L, 0L, 8L, 0L, 
       7L, 0L, 1L, 1L, 6L, 0L, 1L, 3L, 4L, 0L, 1L, 1L, 5L, 6L, 1L, 6L, 
       0L, 0L, 5L, 4L, 1L, 8L, 0L, 1L, 1L, 3L, 1L, 3L, 1L, 0L, 1L, 1L, 
       1L, 1L, 0L, 3L, 3L, 5L, 4L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 
       0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
       0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 6L, 0L, 
       11L, 0L, 7L, 0L, 6L, 0L, 0L, 5L, 4L, 0L, 1L, 0L, 1L, 0L, 0L, 
       1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 9L, 0L), .Dim = c(32L, 5L 
       ), .Dimnames = list(NULL, c("Time", "Var1", "Var2", "Var3", "Var4" 
       ))) 

# barplot 
barplot(t(mat[,2:4]), beside=F, legend=levels(mat), col=c("blue",'red','forestgreen','purple')) 

# manually assign x-axis 
axis(1,at=c(1:32),labels=mat[,1]) 

任何指针,将不胜感激。我对ggplot2解决方案不感兴趣。谢谢!

enter image description here

+0

你忽略你的一列在'barplot'调用 试试这个'barplot(T(垫[2:5]!),...' – infominer

回答

2

为了您的轴,得到barplots的坐标第一。

bp <-barplot(t(mat[,2:5]), beside=F, 
      legend = levels(mat), col = c("blue",'red','forestgreen','purple')) 

现在使用BP的X-TICK标签

axis(1,at=bp,labels=mat[,1]) 

所得的情节 enter image description here

另外,如果你与你的绘图窗口/设备的宽度玩,你可以得到所有标签。

+0

感谢评论和提供解决方案感叹,这样的我身边的一个错误! –