【问题标题】:scale x-axis correctly for barplot为条形图正确缩放 x 轴
【发布时间】:2014-04-17 16:41:28
【问题描述】:

当使用{graphics} 绘制堆叠条形图时,我遇到了一个问题,即 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 解决方案不感兴趣。谢谢!

【问题讨论】:

  • 您在barplot 通话中省略了您的一列试试这个barplot(t(mat[,2:5]),...

标签: r graphics scale boxplot axes


【解决方案1】:

对于您的轴,首先获取条形图的坐标。

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

结果图

此外,如果您使用绘图窗口/设备的宽度,您可以获得所有标签。

【讨论】:

  • 感谢您的评论和提供解决方案!唉,我这方面的错误!
猜你喜欢
  • 1970-01-01
  • 2018-11-08
  • 2014-12-25
  • 2021-12-14
  • 1970-01-01
  • 1970-01-01
  • 2017-03-06
  • 1970-01-01
  • 2023-03-29
相关资源
最近更新 更多