【问题标题】:Limit ticks on X axis in highcharts with R使用 R 限制高图表中 X 轴上的刻度
【发布时间】:2016-01-13 15:20:00
【问题描述】:

我正在尝试在我在 R 中创建的 highcharts 图表上获取正确的日期 X 轴。当我将变量用作 Date highcharts 似乎不理解它并且当我将所有 X 标签转换为字符时显示出来了,看起来不太好。使用 rCharts 包中的 NVD3 图表,有一个选项 reduceXticks=TRUE 可以解决这个问题。也许有一个等价物?

例子:

# data
df <- data.frame(x = 1:100, y = rnorm(100), s = 100*rnorm(100), z = 10*rnorm(100),Date=
                   as.POSIXct(seq(Sys.Date(), by = 1, length.out = 100)))

# chart 1
h1 <- Highcharts$new()

h1$xAxis(categories=df$Date,type = "datetime")
h1$yAxis(list(list(title = list(text = 'x'), opposite = FALSE), 
              list(title = list(text = 'y'), opposite = TRUE),
              list(title = list(text = 's'), opposite = TRUE)))
h1$series(name = 'x', type = 'line', color = '#000099',
          data = df$x)
h1$series(name = 'y', type = 'line', color = '#FF0000',
          data = df$y, yAxis=1)
h1$series(name = 's', type = 'line', color = '#006600',
          data = df$s, yAxis=2)
h1
#-------------------------------------------------------------#

h1 <- Highcharts$new()

h1$xAxis(categories=as.character(df$Date),type = "datetime")
h1$yAxis(list(list(title = list(text = 'x'), opposite = FALSE), 
              list(title = list(text = 'y'), opposite = TRUE),
              list(title = list(text = 's'), opposite = TRUE)))
h1$series(name = 'x', type = 'line', color = '#000099',
          data = df$x)
h1$series(name = 'y', type = 'line', color = '#FF0000',
          data = df$y, yAxis=1)
h1$series(name = 's', type = 'line', color = '#006600',
          data = df$s, yAxis=2)
h1

【问题讨论】:

    标签: r highcharts shiny axis


    【解决方案1】:

    您可以尝试新的包highcharter,其中包括一些快捷功能来创建给定日期和值的图表(也使用 highstock)。检查链接http://jkunst.com/highcharter/oldindex.html#time-series

    【讨论】:

    • 这看起来很棒!感谢您创建此软件包。
    • 乍一看,这些例子看起来不是很好,但很棒!是否也可以加分:highcharts.com/demo/dynamic-click-to-add 这是我在 rCharts 中挣扎了好几个小时的事情......
    • @Tim_Utrecht 有可能。您需要将 js 对象转换为高级语法:hc_chart(type = "scatter", margins = c(70, 50, 60, 80))), ...) 在事件情况下,您需要一个函数,您需要将该函数作为文本传递,并添加 JS 函数:events = list(click = JS("function(e){ .... }"))。如果您有更多疑问,我们可以在 github 存储库中讨论。问候,
    【解决方案2】:

    您可以为xAxis 使用minTickInterval 选项

    minTickInterval: NumberSince
    

    轴值中允许的最小刻度间隔。例如在 放大带有每日数据的轴,这可用于防止 显示小时的轴。默认为两个之间的最近距离 轴上的点。

    来自http://api.highcharts.com/highcharts#xAxis.minTickInterval

    或者,使用tickInterval 设置自定义刻度数:

    tickInterval: Number
    

    刻度线的间隔,以轴为单位。为空时,打勾 间隔被计算为大致遵循上的 tickPixelInterval 线性和日期时间轴。在分类轴上,空 tickInterval 将默认为 1,一个类别。 请注意,日期时间轴是基于 以毫秒为单位,因此例如一天的间隔表示为 24 * 3600 * 1000.

    来自http://api.highcharts.com/highcharts#xAxis.tickInterval

    因此,就您而言,也许您应该使用每周刻度,即tickInterval = 7* 24 * 3600 * 1000

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-13
      相关资源
      最近更新 更多