【问题标题】:Using literal month names with year in ramcharts在 ramcharts 中使用带有年份的文字月份名称
【发布时间】:2017-11-23 09:53:15
【问题描述】:

这是我使用 rAmChart 生成条形图的代码,

library(rAmCharts)
amBarplot(x = "month", y = "value", data = dataset, 
                  dataDateFormat = "MM/YYYY", minPeriod = "MM",
                  show_values = FALSE, labelRotation = -90, depth = 0.1)

但是,有没有办法在我的 x 轴上使用月份名称和年份?我正在尝试使用 MMM-YY 格式。

样本数据集,

 structure(list(value = c(11544, 9588, 9411, 10365, 11154, 12688
), month = c("05/2012", "06/2012", "07/2012", "08/2012", "09/2012", 
"10/2012")), .Names = c("value", "month"), row.names = c(NA, 
6L), class = "data.frame")

谢谢。

【问题讨论】:

  • 请提供dataset 的示例以使代码完全可重现
  • 在此处添加。谢谢。
  • 存储的意思是可重现的代码 - 你能把代码的dput() 贴出来,这样我们就可以在 R 中重现了吗?
  • 确实,看看dput() 结果会很有帮助
  • 添加了它们。希望那已经足够好了。

标签: r bar-chart amcharts


【解决方案1】:

rAmCharts 似乎没有在 categoryAxis 中公开 AmCharts 的 dateFormats 设置,因此您必须通过 init event 访问它并使用修改后的 MM 格式字符串创建自己的 dateFormats 数组时期。我对 R 的经验不是很丰富,但这里是我如何使用 R 3.4.2 和 rAmCharts 2.1.5 使其工作

chart <- amBarplot( ... settings omitted ... )
addListener(.Object = chart,
            name = 'init',
            expression = paste(
            "function(e) {",
            "e.chart.categoryAxis.dateFormats = ", 
           '[{"period":"fff","format":"JJ:NN:SS"},{"period":"ss","format":"JJ:NN:SS"},',
           '{"period":"mm","format":"JJ:NN"},{"period":"hh","format":"JJ:NN"},{"period":"DD","format":"MMM DD"},',
           '{"period":"WW","format":"MMM DD"},',
           '{"period":"MM","format":"MMM-YY"},', # "add YY to default MM format
           '{"period":"YYYY","format":"YYYY"}]; ',
           'e.chart.validateData();',
           "}")
) 

【讨论】:

    【解决方案2】:

    这是一个不同的解决方案:

    library(rAmCharts)
    dataset <-  structure(list(value = c(11544, 9588, 9411, 10365, 11154, 12688
    ), month = c("05/2012", "06/2012", "07/2012", "08/2012", "09/2012", 
    "10/2012")), .Names = c("value", "month"), row.names = c(NA, 
    6L), class = "data.frame")
    
    dataset$month <- as.character(
                            format(
                               as.Date(paste0("01/",dataset$month), "%d/%m/%Y"),
                            "%B %Y"))
    
    amBarplot(x = "month", y = "value", data = dataset, 
                      show_values = FALSE, labelRotation = -90, depth = 0.1)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-03
      • 2017-09-09
      • 1970-01-01
      • 2019-07-19
      • 2021-12-31
      • 2022-01-12
      • 1970-01-01
      相关资源
      最近更新 更多