删除图例和订购图例中的“系列 1”
Posted
技术标签:
【中文标题】删除图例和订购图例中的“系列 1”【英文标题】:Removing "Series 1" in legend and ordering legend 【发布时间】:2020-08-02 01:57:28 【问题描述】:我正在创建一个有趣的拼图曲线,并想从我如何编写下面的代码中了解以下内容:
数据集
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) 如何从图例中删除值“系列 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)
【问题讨论】:
【参考方案1】:您可以使用https://api.highcharts.com/highcharts/title.style更改标题的字体大小
您可以通过设置 series.showInLegend: false 从图例中删除第一个系列 https://api.highcharts.com/highcharts/series.line.showInLegend
您可以使用 series.legendIndex 在图例中定位您的系列:https://api.highcharts.com/highcharts/series.line.legendIndex
如果您希望我在您的代码中向您展示如何执行此操作(我将编辑答案),请提供所有代码和数据。目前,当尝试通过在 RStudio 中复制/粘贴来运行您的代码时,我收到错误“object 'jigsaw' not found”。
编辑:这是整个代码。我的方法并不完美(我使用 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)
【讨论】:
是的,我今天会提供一个数据集。谢谢! @nak5120 我编辑了我的答案。正确请采纳【参考方案2】:对于标题你可以这样做,
`style:
fontSize: "14px"
`
要删除“系列 1”,您可以插入 showInLegend: false
。接口:https://api.highcharts.com/highcharts/plotOptions.series.showInLegend
`series: [
type: "line",
data: data,
showInLegend: false, //this will hide the legend
,...`
为了重新排序您可以使用的图例,可以通过为每个系列设置legendIndex
进行排序。接口: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
,...`
【讨论】:
谢谢,但这似乎不适用于 R 环境,您能告诉我在哪里可以将其添加到现有代码中吗?以上是关于删除图例和订购图例中的“系列 1”的主要内容,如果未能解决你的问题,请参考以下文章