简体   繁体   中英

R highcharter plot points color based on value

I am new to highcharts and R highcharter. I have a dataframe like this

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

I want to draw it using highcharter. 在此处输入图片说明

For the point color, I want to make the point color to be the corresponding color in the data frame.

What should I do? Even though it is R codes, any solutions in JS would be appreciate it because I can rewrite it in R.

If you give the color variable in hexadecimal format, highcharter will recognize the colors

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

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

在此处输入图片说明

A slightly improved solution:

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)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM