如何在R中自定义高图图

问题描述 投票:0回答:1

我想使用highcharts中的js library R进行交互式绘制-

library(highcharter)

Data = 
structure(list(date = structure(c(18361, 18362, 18363, 18364, 
18365, 18366, 18367, 18368, 18369, 18370, 18371, 18372, 18373, 
18374, 18375, 18376, 18377, 18378, 18379, 18380), class = "Date"), 
    variable = c("aaa", "aaa", "aaa", "aaa", "aaa", "aaa", "aaa", 
    "aaa", "aaa", "aaa", "aaa", "aaa", "aaa", "aaa", "aaa", "aaa", 
    "aaa", "aaa", "aaa", "aaa"), value = c(-12.7015856459843, 
    4932.54512619195, 4337.84840857512, -1571.78718535286, -1475.28222178705, 
    4232.12003534889, 6536.12700338211, 2413.36143649343, 7711.43571028106, 
    5461.46337941179, 665.728647378994, 6007.98203770009, -900.505732433108, 
    12397.2655306638, 15182.2497643643, 4654.17811998194, 1673.36119858179, 
    10611.5994058113, 7510.88576801876, 20669.8626227112)), row.names = c(NA, 
20L), class = "data.frame")


hchart(Data, "line", plotBorderWidth = 0.2, plotBorderColor = '#070707',
          hcaes(x = as.Date(as.Date(date)), y = value)) %>%
        hc_xAxis(title = NULL, reversed = FALSE, gridLineWidth = 1, type = 'datetime') %>%
        hc_yAxis(title = "", gridLineWidth = 0.2, minorGridLineWidth = 0, gridLineColor = '#070707', opposite = TRUE) 

有了这个,我想实现以下目标-

  1. 我希望grid-lines的总数在10x方向上始终为y
  2. 我也想在绘图区域使用border

任何指针都将受到高度赞赏。

javascript r highcharts r-highcharter
1个回答
0
投票

gridLines放置在ticks的位置,因此,如果要有10条gridLines,则需要有10条刻度。您可以将axis.tickAmount设置为10,但是此选项仅对'linear'轴有效。您的yAxis是'linear',但您的xAxis是'datetime'。您需要将其更改为“线性”。这样做时,您将丢失日期标签的格式,因此您需要自行格式化它们。为此,我使用了[[xAxis.labels.formatter和Highcharts.dateFormat()方法。要注入JavaScript代码,我还使用了JS(“ ...”)函数。

要获得绘图边框,可以使用

chart.plotBorderWidth

属性。如果要自行定义刻度位置,则可以使用

axis.tickPositions

这是整个代码:

library(highcharter) Data = structure(list(date = structure(c(18361, 18362, 18363, 18364, 18365, 18366, 18367, 18368, 18369, 18370, 18371, 18372, 18373, 18374, 18375, 18376, 18377, 18378, 18379, 18380), class = "Date"), variable = c("aaa", "aaa", "aaa", "aaa", "aaa", "aaa", "aaa", "aaa", "aaa", "aaa", "aaa", "aaa", "aaa", "aaa", "aaa", "aaa", "aaa", "aaa", "aaa", "aaa"), value = c(-12.7015856459843, 4932.54512619195, 4337.84840857512, -1571.78718535286, -1475.28222178705, 4232.12003534889, 6536.12700338211, 2413.36143649343, 7711.43571028106, 5461.46337941179, 665.728647378994, 6007.98203770009, -900.505732433108, 12397.2655306638, 15182.2497643643, 4654.17811998194, 1673.36119858179, 10611.5994058113, 7510.88576801876, 20669.8626227112)), row.names = c(NA, 20L), class = "data.frame") hchart(Data, "line", plotBorderWidth = 0.2, plotBorderColor = '#070707', hcaes(x = as.Date(as.Date(date)), y = value)) %>% hc_chart(plotBorderWidth = 2) %>% hc_xAxis(title = NULL, reversed = FALSE, gridLineWidth = 1, type = 'linear', tickAmount = 10, labels = list(formatter = JS("function () { return Highcharts.dateFormat('%d. %b', this.value); }"))) %>% hc_yAxis(title = "", gridLineWidth = 0.2, minorGridLineWidth = 0, gridLineColor = '#070707', opposite = TRUE, tickAmount = 10)

并且您可以在此处找到所有使用的选项的API参考:https://api.highcharts.com/highcharts/xAxis.tickAmounthttps://api.highcharts.com/highcharts/xAxis.labels.formatter

https://api.highcharts.com/class-reference/Highcharts#.dateFormathttps://api.highcharts.com/highcharts/xAxis.typehttps://api.highcharts.com/highcharts/xAxis.tickPositionshttps://api.highcharts.com/highcharts/chart.plotBorderWidth

© www.soinside.com 2019 - 2024. All rights reserved.