【问题标题】:r highcharter package barplot group vs. colorr highcharter 包 barplot 组与颜色
【发布时间】:2016-10-27 13:47:18
【问题描述】:

我有一个整洁的数据框,类似于以下内容:

tidyDF <- data.frame(PORT_NAME = c("South Louisiana, LA, Port of",
                               "Houston, TX", "Long Beach, CA",
                               "New York, NY and NJ",
                               "Los Angeles, CA", "Beaumont, TX",
                               "Corpus Christi, TX", "New Orleans, LA",
                               "Baton Rouge, LA", "Mobile, AL"),
                 TOTAL_TONS = c(267390209, 234304391, 170052128, 
                                126158655, 122033848, 87283716, 84928330,
                                84465052, 69185878, 64287565),
                 portSel = c("NO", "NO", "NO", "NO", "NO", "YES",
                             "NO", "NO", "NO", "NO"))

我想根据portSel 变量创建具有特定颜色的条形图。

这是我正在使用的代码:

library(highcharter)
myColors <- c("#002F80", "#F9AF38")

highchart() %>%
  hc_add_series_df(data = tidyDF,
                   type = "bar",
                   x = PORT_NAME,
                   y = TOTAL_TONS,
                   group = portSel) %>%
  hc_xAxis(title = list(text = "Ports"),
           tickmarkPlacement = "on",
           tickLength = 0,
           labels = list(
             enabled = FALSE
           )) %>%
  hc_yAxis(title = list(text = "2014 Total Tonnage")) %>% 
  hc_legend(enabled = FALSE) %>%
  hc_colors(myColors)

我在hc_add_series_df 中尝试过groupcolor。两者都不能正常工作。当我使用group = portSel 时,如上所述,颜色是正确的,但它会将单个YES 端口与第一个NO 端口组合在一起。当我使用color = portSel 时,它会将YES 端口放在正确的位置,但它不再使用myColors 中的自定义颜色。

欢迎任何建议! 谢谢。

【问题讨论】:

    标签: r highcharts


    【解决方案1】:

    我修改了之前的答案。

    您没有以正确的方式使用组。 group 选项是创建/添加多个系列,这就是为什么第一个 yes 位于第一个 no 旁边的原因,因为值是按顺序排列的。

    关于颜色,函数hc_add_series_df根据给定的颜色变量对点(条、柱)着色,所以不要使用hc_colors给出的颜色。

    所以,我认为一个简单的方法是“手动”添加系列。这意味着使用您想要的特定数据(和颜色)从数据中创建一个列表。

    tidyDF2 <- tidyDF %>% 
      mutate(color = ifelse(portSel == "NO", myColors[1], myColors[2])) %>% 
      select(y = TOTAL_TONS, color)
    
    
    highchart() %>% 
      hc_chart(type = "bar") %>% 
      hc_xAxis(labels = list(
        enabled = FALSE
        )) %>%
      hc_add_series(data = list_parse(tidyDF2), showInLegend = FALSE)
    

    这对你有帮助吗?


    上一个答案。

    您可以尝试添加hc_xAxis 下一个参数:type = "categorical", categories = tidyDF$PORT_NAME, 并使用hc_add_series_df 中的组。由于您放置了 2 个系列(每组一个),您将看到一个带有“是”列的条形图,例如 http://www.highcharts.com/demo/column-parsed

    highchart() %>%
      hc_add_series_df(data = tidyDF,
                       type = "bar",
                       x = PORT_NAME,
                       y = TOTAL_TONS,
                       group = portSel) %>%
      hc_xAxis(title = list(text = "Ports"),
               type = "categorical",
               categories = tidyDF$PORT_NAME,
               tickmarkPlacement = "on",
               tickLength = 0,
               labels = list(
                 enabled = TRUE
               )) %>%
      hc_yAxis(title = list(text = "2014 Total Tonnage")) %>% 
      hc_legend(enabled = FALSE) %>%
      hc_colors(myColors)
    

    【讨论】:

    • 这不起作用。类别不对齐。博蒙特位于与路易斯安那州南部对齐的顶部。如果我们使用color = portSel 而不是group,是否存在无法控制颜色的原因。 color 保持条正确排列,但我失去了控制特定颜色的能力。我尝试了hc_colors,并通过hc_theme 为主题添加颜色。看起来很奇怪。
    猜你喜欢
    • 2022-01-06
    • 1970-01-01
    • 2018-04-08
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    • 2021-08-24
    • 1970-01-01
    • 2018-11-17
    相关资源
    最近更新 更多