簡體   English   中英

如何在 R 中自定義 highcharts plot

[英]How to customise the highcharts plot in R

我想以交互方式使用R到 plot 中的highcharts js library -

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的總數在xy方向上始終為10
  2. 我還想為 plot 區域設置border

任何指針將不勝感激。

網格線放置在刻度線的位置,因此如果您想要 10 條網格線,則需要有 10 個刻度線。 您可以將axis.tickAmount設置為 10,但此選項僅適用於“線性”軸。 您的 yAxis 是“線性”,但您的 xAxis 是“日期時間”。 您需要將其更改為“線性”。 當您這樣做時,您將丟失日期標簽格式,因此您需要自己格式化它們。 為此,我使用了 xAxis.labels.formatterHighcharts.dateFormat()方法。 為了注入 JavaScript 代碼,我還使用了JS("...") function。

要獲得 plot 邊框,您可以使用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)

And here you can find API Reference for all used options: https://api.highcharts.com/highcharts/xAxis.tickAmount https://api.highcharts.com/highcharts/xAxis.labels.formatter

https://api.highcharts.com/class-reference/Highcharts#.dateFormat https://api.highcharts.com/highcharts/xAxis.type https://api.highcharts.com/highcharts/xAxis.tickPositions https: //api.highcharts.com/highcharts/chart.plotBorderWidth

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM