R highcharter绘图根据值

时间:2017-10-26 18:47:59

标签: javascript r highcharts shiny r-highcharter

我是highcharts和R highcharter的新手。 我有这样的数据框

tmp <- data.frame(x = 1:5, y = rnorm(5), color = c("green", "red", "green", "orange", "red"))
# x       y  color
# 1  0.4421  green
# 2 -0.8924    red
# 3  0.8264  green
# 4  0.6695 orange
# 5 -0.0966    red

我想用highcharter绘制它。 enter image description here

对于点颜色,我想使点颜色成为数据框中的相应颜色。

我该怎么办? 即使它是R代码,JS中的任何解决方案都会欣赏它,因为我可以在R中重写它。

2 个答案:

答案 0 :(得分:2)

如果您以十六进制格式提供颜色变量,则highcharter将识别颜色

tmp <- data.frame(x = 1:5, y = rnorm(5), color = c("#00FF00", "#FF0000", "#00FF00", "#ffa500", "#FF0000"))

hchart(tmp, "line", hcaes(x, y, color = color))

enter image description here

答案 1 :(得分:1)

略微改进的解决方案:

set.seed(1)
tmp <- data.frame(x = 1:5, y = rnorm(5), 
          color = c("green", "red", "green", "orange", "red"))
dummy <- "two"

library(highcharter)
hchart(tmp, type="scatter", hcaes(x=x, y=y, color=color), 
       marker=list(symbol='circle', radius=5), zIndex=1) %>% 
  hc_add_series(tmp, type="line", color="lightblue", zIndex=0)

enter image description here