将hcharts()条从纯色更改为渐变

时间:2019-02-08 17:17:52

标签: r highcharts shiny shinydashboard

我正在尝试通过使颜色在两个绿色阴影之间渐变来使我的highcharter图(对于R Shiny仪表板)更漂亮。我使用pokemon中的默认highcharter数据集做了以下基本示例。

library(shiny)
library(highcharter)

#Subset the data into the pokemon that are Grass and Poison
grass_subset <- subset(pokemon, type_1 == "grass" & type_2 == "poison")

#Define the chart parameters
grass_poison_chart <- hchart(grass_subset, type = "column", hcaes(x=pokemon, y=base_experience))

#Define theme elements
myTheme <- hc_theme(
  colors = c('#658D1B', '84BD00', 'white'),
  chart = list(
    backgroundColor = "white"    
  ))

#Apply theme to grass_poison_chart
grass_poison_chart %>% hc_add_theme(myTheme)

屈服

问题是我不知道用渐变色显示条形的方法。我尝试将myTheme的定义更改为以下内容:

 myTheme <- hc_theme(
   color = list(
     linearGradient = (0,0,0,1),
     stops = (0, '#658D1B'),(1, '#84BD00')
   )
  )

天真的尝试简单地复制highcharts语法:

color: {
    linearGradient: { x1: 0, x2: 0, y1: 0, y2: 1 },
    stops: [
        [0, '#658D1B'],
        [1, '#84BD00']
    ]
}

并将其更改为括号,但是我真的不知道从这里到哪里。我知道我所做的可能无法正常工作(并且无效),但是在highcharter中可能会发生这种情况吗?我想念什么?

1 个答案:

答案 0 :(得分:0)

很抱歉,我的R安装遇到了一些问题。

我做的第一件事是尝试用纯JS编写代码。这部分很简单:https://jsfiddle.net/BlackLabel/byht9jd6

我认为在R中实现它有点困难,但是..这部分也很容易:) 这是代码:

library(shiny)
library(highcharter)

#Subset the data into the pokemon that are Grass and Poison
grass_subset <- subset(pokemon, type_1 == "grass" & type_2 == "poison")

#Define the chart parameters
grass_poison_chart <- hchart(grass_subset, type = "column", hcaes(x=pokemon, 
y=base_experience)) %>%
  hc_plotOptions(
    column = list(
      color = list(
        linearGradient = list(x1 = 0, x2 = 0, y1 = 0, y2 = 1),
        stops = list(
          c(0, '#658D1B'),
          c(1, '#84BD00')
        )
      )
    )
  )

#Apply theme to grass_poison_chart
grass_poison_chart %>% hc_add_theme(myTheme)