在ramcharts中使用带有年份的文字月份名称

时间:2017-11-23 09:53:15

标签: r bar-chart amcharts

以下是使用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")

感谢。

2 个答案:

答案 0 :(得分:0)

看来rAmCharts并没有公开AmCharts' categoryAxis中的dateFormats设置,因此您必须通过init event访问它,并使用dateFormats期间的修改后的格式字符串创建您自己的MM数组。我对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();',
           "}")
) 

答案 1 :(得分:0)

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

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)

enter image description here