【问题标题】:How to specify number of axis labels如何指定轴标签的数量
【发布时间】:2018-04-06 15:35:49
【问题描述】:

我有一个看起来像这样的 df,但有 14000 多行和 30 个唯一的“Vessel.Pln”标识符;

我正在使用 df 为每艘船按月按 size.code 创建一个堆叠的上岸条形图,为期 10 年。我想减少每 3 个月说一次的 x 轴标签的数量,以便它们清晰易读。我找到了一些方法示例,但我不确定在从 df 矩阵调用条形图时如何设置轴序列。

    Size.Code   Date    Vessel.Pln  Weight
            2   2011-01-01  BF206   0.174330
            3   2011-01-01  BF206   0.095940
            4   2011-01-01  BF206   0.143910
            5   2011-01-01  BF206   0.407745
            2   2011-02-01  BF206   0.061425
            3   2011-02-01  BF206   0.234000
            5   2011-02-01  BF206   0.327600
            2   2011-05-01  BF206   0.081900
            3   2011-05-01  BF206   0.152100
            4   2011-05-01  BF206   0.444600
            5   2011-05-01  BF206   1.070550
            2   2011-06-01  BF206   0.273780
            3   2011-06-01  BF206   1.965600
            4   2011-06-01  BF206   0.795600
            1   2011-08-01  BF206   0.421200
            2   2011-08-01  BF206   1.329120
            3   2011-08-01  BF206   2.398500
            4   2011-08-01  BF206   2.000700
            5   2011-08-01  BF206   0.649350
            3   2011-10-01  BF206   0.056160

for (a in unique(grade$Vessel.Pln)) {
  df <- grade[grade$Vessel.Pln == a,]
  library(reshape2)
  df2 <- dcast(df,Size.Code~Date,sum)
  library(RColorBrewer)

  barplot(as.matrix(df2),main=a,
    xlim=c(0, ncol(df2) + 20),
    col=brewer.pal(nrow(df2), "Spectral"),
    ylab="Landings (tonnes)",xlab="Month",las=2,cex.names=0.4,
    args.legend=list(
    x=ncol(df2) + 3,
    y=max(colSums(df2)),
    bty = "n"
 )
)
legend(55, 
       legend = c("1", "2","3","4","5"), 
       fill =brewer.pal(nrow(df2), "Spectral"))
}

【问题讨论】:

    标签: r loops bar-chart axis-labels


    【解决方案1】:

    要保持使用基本 R 的 barplot,请考虑传递 axisnames = FALSE 并使用 axis() 重新编写。下面重写了第一个和最后一个日期列之间的每个季度。

    另外,考虑使用by,该函数将数据帧拆分为一个或多个因子,为每个不同的Vessel.Pln 运行绘图,从而避免子集调用和@987654326 @ 环形。另外,请务必删除 df2 矩阵的第一列以不绘制 Size.Code

    by(grade, grade$Vessel.Pln, function(df) {
    
      df2 <- dcast(df, Size.Code ~ Date,sum, value.var="Weight")
    
      bp <- barplot(as.matrix(df2[-1]), axisnames = FALSE,
                    main = a,
                    xlim = c(0, ncol(df2) + 20),
                    col = brewer.pal(nrow(df2), "Spectral"),
                    ylab = "Landings (tonnes)",xlab="Month",las=2,cex.names=0.4,
                    args.legend = list(
                      x = ncol(df2) + 3,
                      y = max(colSums(df2)),
                      bty = "n"
                    )
      )
    
      legend(55, 
             legend = c("1", "2","3","4","5"), 
             fill =brewer.pal(nrow(df2), "Spectral"))
    
      # REWRITES AXIS FOR YEAR QUARTERS
      axis(1, at = bp[seq(1,ncol(df2[-1]), 3)], 
           labels = names(df2[-1])[seq(1, ncol(df2[-1]), 3)]
      ) 
    
    })
    

    【讨论】:

    • 感谢 by 函数代码的改进。这部分的重写轴部分没有按预期工作,我没有提到每个 Vessel.Pln 的每个月实际上没有数据,有些有几个月的差距。因此,我希望为每 3 个条形图贴上任何标签,而不是每季度给它们一个标签。
    • 查看更新,现在每三分之一而不是按季度计算条数:1、4、7 等。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-01
    • 2020-04-19
    • 2014-06-17
    • 2020-06-01
    • 2013-06-10
    • 2021-05-27
    • 2018-12-21
    相关资源
    最近更新 更多