删除图例中的 "Series 1" 并排序图例

Removing "Series 1" in legend and ordering legend

我正在创建一个有趣的拼图曲线,想从我编写下面代码的方式中了解以下内容:

数据集

Zone <- c('Overwhelm State', 'Overwhelm State', 'Puzzle Pairing State', 
  'Puzzle Pairing State', 'Puzzle Pairing State', 'Puzzle Pairing State', 
  'Puzzle Pairing State', 'Puzzle Pairing State', 'Puzzle Pairing State',
  '"I can do this" State', '"I can do this" State', '"I can do this" State',
  'Lost State','Lost State','Lost State','Lost State', 'Close Out State',
  'Close Out State', 'Close Out State','Close Out State', 'Close Out State', 
  'Close Out State')

`Jigsaw Puzzle Pieces`<- c(0.20, 0.10, 0.20, 0.40, 0.80, 1.60, 3.20, 
6.40, 12.80, 14.00,15.00, 18.00, 31.85, 33.50, 33.50, 31.85, 15.93, 
7.96, 3.98, 1.99, 1.00, 0.50)

Date <- as.Date(c('2020-03-28','2020-03-29','2020-03-30','2020-03-31',
  '2020-04-01','2020-04-02','2020-04-03','2020-04-04','2020-04-05','2020-04-06',    '2020-04-07','2020-04-08','2020-04-09','2020-04-10','2020-04-11','2020-04-12',
  '2020-04-13','2020-04-14','2020-04-15','2020-04-16','2020-04-17','2020-04-18'))

jigsaw_data <- data.frame(Date, `Jigsaw Puzzle Pieces`, Zone)

1) 如何更改标题的字体大小?

2) 如何从图例中删除值 "Series 1" 但保持该线出现在图表上?

3) 如何对图例进行排序,使其成为:

"Overwhelm State"
"Puzzle Pairing State"
"I can do this" State
"Lost State"
"Close Out State"

代码如下:

library(highcharter)
highchart() %>%
  hc_add_series(jigsaw_data, type = "line", lineWidth = 10,
               hcaes(x = Date, y = `Jigsaw Puzzle Pieces`)) %>%
  hc_add_series(jigsaw, type = "column",
               hcaes(x = Date, y = `Jigsaw Puzzle Pieces`, group = Zone)) %>%
  hc_xAxis(type = 'datetime', labels = list(format = '{value:%b %d}')) %>%
  hc_title(text = "<b>Jigsaw Puzzle Modelling Curve</b>") %>%
  hc_subtitle(text = "<b>COVID-19 Bias<b>") %>%
  hc_xAxis(title = list(text = '<b>Quarantine Period<b>',lineWidth = 3)) %>%
  hc_yAxis(title = list(text = '<b>Time Required to Insert One Puzzle Piece<b>')) %>%
  hc_legend(align = "right", verticalAlign = "top",
            layout = "vertical", x = 0, y = 100) 

标题可以这样做,

       `style: {
          fontSize: "14px"
        }`

要删除 'series 1',您可以插入 showInLegend: false。 API: https://api.highcharts.com/highcharts/plotOptions.series.showInLegend

       `series: [
        {
          type: "line",
          data: data,
          showInLegend: false, //this will hide the legend
        },...`

为了重新排序您可以使用的图例,可以通过为每个系列设置 legendIndex 来排序。 API: https://api.highcharts.com/highcharts/series.line.legendIndex

       `series: [
        {
          type: "line",
          data: data_1,
          name: "Overwhelm State",
          legendIndex: 1
        },
         {
          type: "line",
          data: data_2,
          name: "Puzzle Pairing State",
          legendIndex: 2
        },...`
  1. 您可以使用https://api.highcharts.com/highcharts/title.style

  2. 更改标题的字体大小
  3. 您可以通过设置 series.showInLegend: false 从图例中删除第一个系列 https://api.highcharts.com/highcharts/series.line.showInLegend

  4. 您可以使用 series.legendIndex 在图例中定位您的系列:https://api.highcharts.com/highcharts/series.line.legendIndex

如果您希望我向您展示如何在您的代码中执行此操作(我将编辑答案),请提供所有代码和数据。现在,在 RStudio 中尝试通过 copy/paste 运行 代码时,出现错误“object 'jigsaw' not found” .

edit:这是整个代码。我的方法并不完美(我使用 JavaScript 因为我不知道 R),但它按您预期的那样工作:

library(highcharter)
Zone <- c('Overwhelm State', 'Overwhelm State', 'Puzzle Pairing State', 
          'Puzzle Pairing State', 'Puzzle Pairing State', 'Puzzle Pairing State', 
          'Puzzle Pairing State', 'Puzzle Pairing State', 'Puzzle Pairing State',
          '"I can do this" State', '"I can do this" State', '"I can do this" State',
          'Lost State','Lost State','Lost State','Lost State', 'Close Out State',
          'Close Out State', 'Close Out State','Close Out State', 'Close Out State', 
          'Close Out State')

`Jigsaw Puzzle Pieces`<- c(0.20, 0.10, 0.20, 0.40, 0.80, 1.60, 3.20, 
                           6.40, 12.80, 14.00,15.00, 18.00, 31.85, 33.50, 33.50, 31.85, 15.93, 
                           7.96, 3.98, 1.99, 1.00, 0.50)

Date <- as.Date(c('2020-03-28','2020-03-29','2020-03-30','2020-03-31',
                  '2020-04-01','2020-04-02','2020-04-03','2020-04-04','2020-04-05','2020-04-06',    '2020-04-07','2020-04-08','2020-04-09','2020-04-10','2020-04-11','2020-04-12',
                  '2020-04-13','2020-04-14','2020-04-15','2020-04-16','2020-04-17','2020-04-18'))

jigsaw_data <- data.frame(Date, `Jigsaw Puzzle Pieces`, Zone)

highchart() %>%
  hc_add_series(jigsaw_data, type = "line", lineWidth = 10,
                hcaes(x = Date, y = `Jigsaw Puzzle Pieces`)) %>%
  hc_add_series(jigsaw_data, type = "column",
                hcaes(x = Date, y = `Jigsaw Puzzle Pieces`, group = Zone)) %>%
  hc_chart(events = list(load = JS("function() {

    this.series.forEach(function(series) {
      if (series.name === 'Series 1') {
        series.update({
          showInLegend: false
        });
      }
      if (series.name === '\"I can do this\" State') {
        series.update({
          legendIndex: 2
        });
      }
      if (series.name === 'Close Out State') {
        series.update({
          legendIndex: 4
        });
      }
      if (series.name === 'Lost State') {
        series.update({
          legendIndex: 3
        });
      }
      if (series.name === 'Overwhelm State') {
        series.update({
          legendIndex: 0
        });
      }
      if (series.name === 'Puzzle Pairing State') {
        series.update({
          legendIndex: 1
        });
      }
    });

  }"))) %>%
  hc_xAxis(type = 'datetime', labels = list(format = '{value:%b %d}')) %>%
  hc_title(text = "<b>Jigsaw Puzzle Modelling Curve</b>", style = list(fontSize = '30px')) %>%
  hc_subtitle(text = "<b>COVID-19 Bias<b>") %>%
  hc_xAxis(title = list(text = '<b>Quarantine Period<b>',lineWidth = 3)) %>%
  hc_yAxis(title = list(text = '<b>Time Required to Insert One Puzzle Piece<b>')) %>%
  hc_legend(align = "right", verticalAlign = "top",
            layout = "vertical", x = 0, y = 100)