【问题标题】:Change grouping color with highcharter使用 highcharter 更改分组颜色
【发布时间】:2020-03-19 10:56:20
【问题描述】:

我有一个不同类别的条形图,我为每个类别自定义了条形颜色,但我不知道如何更改下图点颜色

我的数据:

library(highcharter)
library(dplyr)

x = structure(list(stat = c("abandonne_pro", "admissible", "admissible", 
                            "admissible", "non_recu", "refuse", "refuse", "en_cours", 
                            "non_recu"), 
                   annee_recep = structure(c(17532, 17532, 17897, 18262, 17897, 17532, 17897, 17897, 17532), class = "Date"), 
                   nb_stat = c(4L, 57L, 112L, 1L, 2L, 5L, 5L, 1L, 1L)), 
              row.names = c(NA, -9L), 
              class = "data.frame",
              .Names = c("stat", "annee_recep", "nb_stat"))

添加颜色变量:

x = x %>%
  mutate(
    color = case_when(
      stat == 'abandonne_pro' ~ '#87CEEB',
      stat == 'admissible' ~ '#7CFC00',
      stat == 'non_recu' ~ '#FF7F50',
      stat == 'refuse' ~ '#FF0000',
      stat == 'en_cours' ~ '#FFFF00',
      TRUE ~ '#C0C0C0'
    ),
    date = format(annee_recep, format="%Y")
  )

图形:

hchart(
  x,
  "column", 
  hcaes(x = annee_recep, y = nb_stat, group = stat, color = color)
) %>% 
  hc_plotOptions(column = list(stacking = "normal"), type = "datetime") %>% 
  hc_xAxis(
    title = list(text = 'Date'),
    type = 'datetime'
  ) %>% 
  hc_yAxis(
    title = list(text = "Nombre de chantier")
  ) %>% 
  hc_add_theme(hc_theme_gridlight())

输出:

感谢您的帮助!

【问题讨论】:

    标签: r r-highcharter


    【解决方案1】:

    一个简单的解决方案是使用hc_colors() 手动设置颜色,而不是在颜色美学上进行映射。试试这个:

    library(highcharter)
    library(dplyr)
    library(tibble)
    
    x <- structure(list(
      stat = c(
        "abandonne_pro", "admissible", "admissible",
        "admissible", "non_recu", "refuse", "refuse", "en_cours",
        "non_recu"
      ),
      annee_recep = structure(c(17532, 17532, 17897, 18262, 17897, 17532, 17897, 17897, 17532), class = "Date"),
      nb_stat = c(4L, 57L, 112L, 1L, 2L, 5L, 5L, 1L, 1L)
    ),
    row.names = c(NA, -9L),
    class = "data.frame",
    .Names = c("stat", "annee_recep", "nb_stat")
    )
    
    x <- x %>%
      mutate(
        color = case_when(
          stat == 'abandonne_pro' ~ '#87CEEB',
          stat == 'admissible' ~ '#7CFC00',
          stat == 'non_recu' ~ '#FF7F50',
          stat == 'refuse' ~ '#FF0000',
          stat == 'en_cours' ~ '#FFFF00',
          TRUE ~ '#C0C0C0'
        ),
        date = format(annee_recep, format="%Y")
      )
    
    # Filter dataset
    x <- filter(x, stat != 'en_cours')
    # Named color vector
    color_vec <- count(x, stat, color) %>% 
      select(stat, color) %>% 
      deframe() %>% 
      unname()
    
    hchart(
      x,
      "column", 
      hcaes(x = annee_recep, y = nb_stat, group = stat)
    ) %>% 
      hc_plotOptions(column = list(stacking = "normal"), type = "datetime") %>% 
      hc_xAxis(
        title = list(text = 'Date'),
        type = 'datetime'
      ) %>% 
      hc_yAxis(
        title = list(text = "Nombre de chantier")
      ) %>% 
      hc_add_theme(hc_theme_gridlight()) %>% 
      hc_colors(colors = color_vec)
    

    reprex package (v0.3.0) 于 2020-03-19 创建

    【讨论】:

    • 感谢您的帮助,我希望使用您的解决方案为一个特定的“统计”设置一种颜色,如果我过滤filter(x, stat != 'en_cours'),颜色将相互交换...
    • 嗨穆斯塔法。我意识到了这个问题。硬编码从来都不是一个好的选择。我刚刚编辑了我的代码。与 ggplot2 scale_color_identiy 相比,仍然不是完美的解决方案......但是看看。为了允许过滤,我引入了一个颜色向量,它只包含 df 中存在的统计信息的颜色。作为一个例子,我使用filter(x, stat != 'en_cours')
    • 感谢 stefan,它做得非常聪明;)我正在考虑添加所有 stat 并在 nb_stat 列中通过基本的 rbind 给它们 0 以保持颜色顺序。 ..但你的做事方式绝对更好!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-19
    • 2019-02-03
    • 1970-01-01
    • 2021-11-20
    • 2019-07-10
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多