简体   繁体   中英

Removing “Series 1” in legend and ordering legend

I am creating a funny jigsaw puzzle curve and want to know the following from how I wrote the code below:

Dataset

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) How do I change the font size of the title?

2) How do I remove the value "Series 1" from the legend but keep the line appearing on the graph?

3) How do I order the legend so that it is:

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

Code below:

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) 

在此处输入图像描述

  1. You can change the font size of the title using https://api.highcharts.com/highcharts/title.style

  2. You can remove the first series from legend by setting series.showInLegend: false https://api.highcharts.com/highcharts/series.line.showInLegend

  3. You can position your series in legend using series.legendIndex : https://api.highcharts.com/highcharts/series.line.legendIndex

If you want me to show you how to do it in your code (I will edit answer), then please provide all the code with data. For now, when trying to run your code by copy/paste in RStudio, I am getting error " object 'jigsaw' not found ".

edit : this it the whole code. My way is not perfect (I used JavaScript because I don't know R), but it works as you expected:

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) 

For the title you can do this,

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

For removing 'series 1' you can insert showInLegend: false . API: https://api.highcharts.com/highcharts/plotOptions.series.showInLegend

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

For reorderring the legend you can use can be sorted by setting legendIndex for each series. 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
        },...`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM