gpt4 book ai didi

r - 如何根据r中的分类列更改highchart中折线图的颜色?

转载 作者:行者123 更新时间:2023-12-04 08:22:35 24 4
gpt4 key购买 nike

我正在尝试使用 highcharts 创建交互式折线图并根据分类列更改线条的颜色,即 CFR_level .
下面是ggplot(非交互版本的所需形式)

p1_cfr <- grouped_cases_df %>% 
filter(Country.Region %in% top_10) %>%

ggplot(aes(x = Date, y = Confirmed, col = CFR_level, group = Country.Region, label = continent)) +
geom_path()

p1_cfr
enter image description here
问题 : 当我尝试使用 将其转换为交互式绘图时 Highcharts 它没有根据分类变量为线条着色,即 CFR_level而是基于每个 Country.Region 进行着色
library(tidyverse)
library(highcharter)
grouped_cases_df %>% 
filter(Country.Region %in% top_10) %>%

hchart(type = "line", hcaes(x = Date, y = Confirmed, group = Country.Region, color = CFR_level))
enter image description here
如何为 highchart 中的每一行着色基于分类变量 CFR_level ?
      ---------------------------------------------------------------
更新(数据详情):
top_10

######### output ##########

[1] "US"
[2] "India"
[3] "Brazil"
[4] "Russia"
[5] "France"
[6] "United Kingdom"
[7] "Italy"
[8] "Turkey"
[9] "Spain"
[10] "Argentina"
grouped_cases_df %>% 
filter(Country.Region %in% top_10) %>%
head(n = 30)

####### output #########

Country.Region continent Date Confirmed Deaths Case_Fatality_Ratio CFR_level
<chr> <fct> <date> <int> <int> <dbl> <fct>
1 Argentina Americas 2020-01-22 0 0 0 Low
2 Argentina Americas 2020-01-23 0 0 0 Low
3 Argentina Americas 2020-01-24 0 0 0 Low
4 Argentina Americas 2020-01-25 0 0 0 Low
5 Argentina Americas 2020-01-26 0 0 0 Low
6 Argentina Americas 2020-01-27 0 0 0 Low
7 Argentina Americas 2020-01-28 0 0 0 Low
8 Argentina Americas 2020-01-29 0 0 0 Low
9 Argentina Americas 2020-01-30 0 0 0 Low
10 Argentina Americas 2020-01-31 0 0 0 Low
# ... with 20 more rows
grouped_cases_df %>% str()

####### output #########

tibble [64,176 x 7] (S3: tbl_df/tbl/data.frame)
$ Country.Region : chr [1:64176] "Afghanistan" "Afghanistan" "Afghanistan" "Afghanistan" ...
$ continent : Factor w/ 6 levels "Africa","Americas",..: 3 3 3 3 3 3 3 3 3 3 ...
$ Date : Date[1:64176], format: "2020-01-22" "2020-01-23" "2020-01-24" ...
$ Confirmed : int [1:64176] 0 0 0 0 0 0 0 0 0 0 ...
$ Deaths : int [1:64176] 0 0 0 0 0 0 0 0 0 0 ...
$ Case_Fatality_Ratio: num [1:64176] 0 0 0 0 0 0 0 0 0 0 ...
$ CFR_level : Factor w/ 3 levels "Low","Med","High": 1 1 1 1 1 1 1 1 1 1 ...
grouped_cases_df %>% 
filter(Country.Region %in% top_10) %>%
str()

####### output #########

tibble [3,360 x 7] (S3: tbl_df/tbl/data.frame)
$ Country.Region : chr [1:3360] "Argentina" "Argentina" "Argentina" "Argentina" ...
$ continent : Factor w/ 6 levels "Africa","Americas",..: 2 2 2 2 2 2 2 2 2 2 ...
$ Date : Date[1:3360], format: "2020-01-22" "2020-01-23" "2020-01-24" ...
$ Confirmed : int [1:3360] 0 0 0 0 0 0 0 0 0 0 ...
$ Deaths : int [1:3360] 0 0 0 0 0 0 0 0 0 0 ...
$ Case_Fatality_Ratio: num [1:3360] 0 0 0 0 0 0 0 0 0 0 ...
$ CFR_level : Factor w/ 3 levels "Low","Med","High": 1 1 1 1 1 1 1 1 1 1 ...
grouped_cases_df %>% 
filter(Country.Region %in% top_10) %>%
summary()

####### output #########

Country.Region continent Date Confirmed Deaths
Length:3360 Africa : 0 Min. :2020-01-22 Min. : 0 Min. : 0
Class :character Americas:1008 1st Qu.:2020-04-14 1st Qu.: 46875 1st Qu.: 1382
Mode :character Asia : 336 Median :2020-07-07 Median : 283225 Median : 26286
Europe :2016 Mean :2020-07-07 Mean : 1338426 Mean : 39411
Oceania : 0 3rd Qu.:2020-09-29 3rd Qu.: 1344549 3rd Qu.: 41590
Unknown : 0 Max. :2020-12-22 Max. :18230242 Max. :322765
Case_Fatality_Ratio CFR_level
Min. : 0.000 Low : 455
1st Qu.: 1.746 Med : 719
Median : 2.791 High:2186
Mean : 4.722
3rd Qu.: 6.214
Max. :23.835
更新 2(回复“coloredline”)
我已经按照您的建议尝试了几种方法,但我不确定为什么会出现空行,即使这些数据点存在于那里
1.
grouped_cases_df %>% 

# filtering only couple of countries for simplicity
filter(Country.Region %in% c("France","India")) %>%

hchart(type = "coloredline",
hcaes(x = Date, y = Confirmed, group = Country.Region, segmentColor = CFR_level)) %>%
hc_add_series(name = "Low",
color = "green", marker = list(symbol = "line")) %>%

hc_add_series(name = "Med",
color = "yellow", marker = list(symbol = "line")) %>%

hc_add_series(name = "High",
color = "red", marker = list(symbol = "line")) %>%

hc_add_dependency("plugins/multicolor_series.js")
enter image description here
  • 灵感来自共享源链接
  • grouped_cases_df %>% 
    filter(Country.Region %in% c("France","India")) %>%

    hchart(type = "coloredline",
    hcaes(x = Date, y = Confirmed, group = Country.Region, segmentColor = CFR_level)) %>%
    hc_add_dependency("plugins/multicolor_series.js")
    enter image description here
  • 如果我使用 hc_add_series(data = .,然后我得到错误

  • 解决方案更新 (感谢@Ben 的建议)

    grouped_cases_df %>%
    filter(Country.Region %in% top_10) %>%
    mutate(CFR_col = case_when(CFR_level == "Low" ~ "#FFFF00",
    CFR_level == "Med" ~ "#008000",
    TRUE ~ "#FF0000")
    ) %>%

    hchart(type = "coloredline",
    hcaes(x = Date, y = Confirmed, group = Country.Region, segmentColor = CFR_col)) %>%
    hc_add_dependency("plugins/multicolor_series.js")
    enter image description here

    最佳答案

    请参阅与 highcharter github 问题 here 类似的问题的简要说明.
    我正在使用 COVID19包有一些现成的数据用作说明。我假设将几个国家设置为不同的 CFR_level值作为示例(这只是为了证明,它们并不反射(reflect)实际的病死率)。
    hchart使用 coloredlinemulticolor_series模块,它允许个人 segmentColor要使用的值(更多 info and examples here on plugins and modules )。然后,您可以使用 group 的列以及 segmentColor 的不同列.
    另外,我包括了hc_add_series使用空数据添加自定义图例。
    编辑 (12/24/20) : 如果你设置 colorhcaeshchart您可以根据 CFR_level 获得要匹配的工具提示悬停颜色.在本例中,我将颜色以十六进制表示(CFR_level 在这种情况下是颜色而不是字符值)。

    library(tidyverse)
    library(highcharter)
    library(COVID19)

    df <- covid19()

    df$CFR_level <- "#008000"
    df$CFR_level[df$id == "IND"] <- "#FFFF00"
    df$CFR_level[df$id == "USA"] <- "#FF0000"

    df %>%
    filter(id %in% c("USA", "BRA", "RUS", "IND")) %>%
    hchart(type = "coloredline",
    hcaes(x = date, y = confirmed, group = id, color = CFR_level, segmentColor = CFR_level),
    showInLegend = F) %>%
    hc_add_series(name = "Low", color = "#008000", marker = list(symbol = "line")) %>%
    hc_add_series(name = "Med", color = "#FFFF00", marker = list(symbol = "line")) %>%
    hc_add_series(name = "High", color = "#FF0000", marker = list(symbol = "line")) %>%
    hc_add_dependency("plugins/multicolor_series.js")
    剧情
    highcharter plot

    关于r - 如何根据r中的分类列更改highchart中折线图的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65427527/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com