基于echarts4r中的变量添加彩色点

发布于 2025-02-02 15:05:07 字数 2495 浏览 3 评论 0 原文

我在Echarts4r中使用了一些e_lines完成了一个烛台图。我想在一些蜡烛中添加一些基于变量(“点”)颜色的点。

这是代码

library(quantmod)
library(echarts4r)
library(magrittr)

GS |> 
  e_charts(date) |> 
  e_candle(GS.Open, GS.Close, GS.Low, GS.High, name = "Goldman Sachs",
           lineStyle = list(
             color = "#42eff5", # bull
             color0 = "#f54242", # bear
             borderColor = NA,
             borderColor0 = NA)) |> 
  e_datazoom(type = "slider") |> 
  e_title("Candlestick chart", "Quantmod data") |>
  e_line(serie = level, name = "Fib", lineStyle = list(color = "green", opacity = 0.8, type = "dotted"), legend = T) %>% 
  e_line(serie = level2, name = "Fib", lineStyle = list(color = "blue", opacity = 0.8, type = "dotted"), legend = T) %>% 
  # e_scatter(serie = GS.Close, name = "Signal", symbol_size = 10) %>% 
  # e_visual_map(serie = points) %>% 
    e_datazoom(type = "slider") %>% 
  e_tooltip(trigger = "axis") 

一切都很好的代码,直到我添加 e_visual_map 的点颜色: 问题是:

  • e_visual_map
  • 蜡烛的颜色

影响


GS <- structure(list(GS.Open = c(200.600006, 200.220001, 198.429993, 
199.050003, 203.539993, 203.399994, 208.339996, 210.899994, 210.850006, 
212.199997, 213.75), GS.High = c(203.320007, 200.669998, 200, 
203.949997, 204.899994, 208.440002, 213.169998, 214.220001, 215.130005, 
214.089996, 214.809998), GS.Low = c(197.820007, 198.070007, 197.899994, 
198.100006, 202, 201.5, 207.600006, 210.399994, 210.850006, 210.850006, 
209.100006), GS.Close = c(200.720001, 198.850006, 199.050003, 
203.729996, 204.080002, 208.110001, 211.880005, 213.990005, 213.589996, 
213.229996, 209.679993), GS.Volume = c(6494900, 6460200, 5892900, 
7851000, 7147100, 8025700, 9039400, 6618900, 5846600, 5306300, 
6423200), GS.Adjusted = c(162.160843, 160.65007, 160.811646, 
164.59259, 164.875336, 168.131134, 171.177002, 172.881592, 172.558426, 
172.267532, 169.399551), date = c("2007-01-03", "2007-01-04", 
"2007-01-05", "2007-01-08", "2007-01-09", "2007-01-10", "2007-01-11", 
"2007-01-12", "2007-01-16", "2007-01-17", "2007-01-18"), level = c(200, 
200, 200, 200, 200, 200, 200, 200, 200, 200, 200), level2 = c(250, 
250, 250, 250, 250, 250, 250, 250, 250, 250, 250), points = -5:5), row.names = c("2007-01-03", 
"2007-01-04", "2007-01-05", "2007-01-08", "2007-01-09", "2007-01-10", 
"2007-01-11", "2007-01-12", "2007-01-16", "2007-01-17", "2007-01-18"
), class = "data.frame")

I have a Candlestick plot done in echarts4r with some e_lines. I would like to add points in some candles, coloured based on a variable ("points").

This is the code

library(quantmod)
library(echarts4r)
library(magrittr)

GS |> 
  e_charts(date) |> 
  e_candle(GS.Open, GS.Close, GS.Low, GS.High, name = "Goldman Sachs",
           lineStyle = list(
             color = "#42eff5", # bull
             color0 = "#f54242", # bear
             borderColor = NA,
             borderColor0 = NA)) |> 
  e_datazoom(type = "slider") |> 
  e_title("Candlestick chart", "Quantmod data") |>
  e_line(serie = level, name = "Fib", lineStyle = list(color = "green", opacity = 0.8, type = "dotted"), legend = T) %>% 
  e_line(serie = level2, name = "Fib", lineStyle = list(color = "blue", opacity = 0.8, type = "dotted"), legend = T) %>% 
  # e_scatter(serie = GS.Close, name = "Signal", symbol_size = 10) %>% 
  # e_visual_map(serie = points) %>% 
    e_datazoom(type = "slider") %>% 
  e_tooltip(trigger = "axis") 

Everything works great until I add the points colour with e_visual_map:
The problems are:

  • The colours of the candles are affected by the e_visual_map
  • The points get the colour of the close price and not the colour of the variable "points"

Here is some data


GS <- structure(list(GS.Open = c(200.600006, 200.220001, 198.429993, 
199.050003, 203.539993, 203.399994, 208.339996, 210.899994, 210.850006, 
212.199997, 213.75), GS.High = c(203.320007, 200.669998, 200, 
203.949997, 204.899994, 208.440002, 213.169998, 214.220001, 215.130005, 
214.089996, 214.809998), GS.Low = c(197.820007, 198.070007, 197.899994, 
198.100006, 202, 201.5, 207.600006, 210.399994, 210.850006, 210.850006, 
209.100006), GS.Close = c(200.720001, 198.850006, 199.050003, 
203.729996, 204.080002, 208.110001, 211.880005, 213.990005, 213.589996, 
213.229996, 209.679993), GS.Volume = c(6494900, 6460200, 5892900, 
7851000, 7147100, 8025700, 9039400, 6618900, 5846600, 5306300, 
6423200), GS.Adjusted = c(162.160843, 160.65007, 160.811646, 
164.59259, 164.875336, 168.131134, 171.177002, 172.881592, 172.558426, 
172.267532, 169.399551), date = c("2007-01-03", "2007-01-04", 
"2007-01-05", "2007-01-08", "2007-01-09", "2007-01-10", "2007-01-11", 
"2007-01-12", "2007-01-16", "2007-01-17", "2007-01-18"), level = c(200, 
200, 200, 200, 200, 200, 200, 200, 200, 200, 200), level2 = c(250, 
250, 250, 250, 250, 250, 250, 250, 250, 250, 250), points = -5:5), row.names = c("2007-01-03", 
"2007-01-04", "2007-01-05", "2007-01-08", "2007-01-09", "2007-01-10", 
"2007-01-11", "2007-01-12", "2007-01-16", "2007-01-17", "2007-01-18"
), class = "data.frame")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沦落红尘 2025-02-09 15:05:07

您可以使用 group_by 向每个“点”值添加随机颜色。当您添加新蜡烛时,由于 group_by ,它将获得相同的颜色。这是一个可重现的示例:

library(quantmod)
library(echarts4r)
library(dplyr)
library(randomcoloR)

# To show you the colors for each points
GS |> 
  group_by(points) %>% 
  mutate(color = randomcoloR::randomColor()) %>%
  ungroup()
#> # A tibble: 11 × 11
#>    GS.Open GS.High GS.Low GS.Close GS.Volume GS.Adju…¹ date  level level2 points
#>      <dbl>   <dbl>  <dbl>    <dbl>     <dbl>     <dbl> <chr> <dbl>  <dbl>  <int>
#>  1    201.    203.   198.     201.   6494900      162. 2007…   200    250     -5
#>  2    200.    201.   198.     199.   6460200      161. 2007…   200    250     -4
#>  3    198.    200    198.     199.   5892900      161. 2007…   200    250     -3
#>  4    199.    204.   198.     204.   7851000      165. 2007…   200    250     -2
#>  5    204.    205.   202      204.   7147100      165. 2007…   200    250     -1
#>  6    203.    208.   202.     208.   8025700      168. 2007…   200    250      0
#>  7    208.    213.   208.     212.   9039400      171. 2007…   200    250      1
#>  8    211.    214.   210.     214.   6618900      173. 2007…   200    250      2
#>  9    211.    215.   211.     214.   5846600      173. 2007…   200    250      3
#> 10    212.    214.   211.     213.   5306300      172. 2007…   200    250      4
#> 11    214.    215.   209.     210.   6423200      169. 2007…   200    250      5
#> # … with 1 more variable: color <chr>, and abbreviated variable name
#> #   ¹​GS.Adjusted

GS |> 
  group_by(points) %>% 
  mutate(color = randomcoloR::randomColor()) %>%
  ungroup() |>
  e_charts(date) |> 
  e_scatter(serie = GS.Close, name = "Signal", symbol_size = 10) |>
  e_add_nested("itemStyle", color) |>
  e_candle(GS.Open, GS.Close, GS.Low, GS.High, name = "Goldman Sachs",
           lineStyle = list(
             color = "#42eff5", # bull
             color0 = "#f54242", # bear
             borderColor = NA,
             borderColor0 = NA)) |> 
  e_datazoom(type = "slider") |> 
  e_title("Candlestick chart", "Quantmod data") |>
  e_line(serie = level, name = "Fib", lineStyle = list(color = "green", opacity = 0.8, type = "dotted"), legend = T) |> 
  e_line(serie = level2, name = "Fib", lineStyle = list(color = "blue", opacity = 0.8, type = "dotted"), legend = T) |>
  e_datazoom(type = "slider") |> 
  e_tooltip(trigger = "axis") 

”在此处输入图像描述”


创建/strong>

在这里a viridis 选项:

library(quantmod)
library(echarts4r)
library(dplyr)
library(viridis)
numb_colors = length(unique(GS$points))

GS |> 
  mutate(color = case_when(points == -5 ~ viridis(numb_colors)[1],
                           points == -4 ~ viridis(numb_colors)[2],
                           points == -3 ~ viridis(numb_colors)[3],
                           points == -2 ~ viridis(numb_colors)[4],
                           points == -1 ~ viridis(numb_colors)[5],
                           points == 0 ~ viridis(numb_colors)[6],
                           points == 1 ~ viridis(numb_colors)[7],
                           points == 2 ~ viridis(numb_colors)[8],
                           points == 3 ~ viridis(numb_colors)[9],
                           points == 4 ~ viridis(numb_colors)[10],
                           points == 5 ~ viridis(numb_colors)[11])) |>
  e_charts(date) |> 
  e_scatter(serie = GS.Close, name = "Signal", symbol_size = 10) |>
  e_add_nested("itemStyle", color) |>
  e_candle(GS.Open, GS.Close, GS.Low, GS.High, name = "Goldman Sachs",
           lineStyle = list(
             color = "#42eff5", # bull
             color0 = "#f54242", # bear
             borderColor = NA,
             borderColor0 = NA)) |> 
  e_datazoom(type = "slider") |> 
  e_title("Candlestick chart", "Quantmod data") |>
  e_line(serie = level, name = "Fib", lineStyle = list(color = "green", opacity = 0.8, type = "dotted"), legend = T) |> 
  e_line(serie = level2, name = "Fib", lineStyle = list(color = "blue", opacity = 0.8, type = "dotted"), legend = T) |>
  e_datazoom(type = "slider") |> 
  e_tooltip(trigger = "axis") 

”在此处输入图像描述”


使用 e_add_nested 带有“ itemStyle”之后的 e_scatter 调用。确保您的列有颜色以表示要点。我使用了 Rainbow 颜色,但是您可以将它们更改为您想要的任何东西。这是一个可重复的示例:

library(quantmod)
library(echarts4r)
library(dplyr)

GS |> 
  mutate(color = rainbow(11)) |>
  e_charts(date) |> 
  e_scatter(serie = GS.Close, name = "Signal", symbol_size = 10) |>
  e_add_nested("itemStyle", color) |>
  e_candle(GS.Open, GS.Close, GS.Low, GS.High, name = "Goldman Sachs",
           lineStyle = list(
             color = "#42eff5", # bull
             color0 = "#f54242", # bear
             borderColor = NA,
             borderColor0 = NA)) |> 
  e_datazoom(type = "slider") |> 
  e_title("Candlestick chart", "Quantmod data") |>
  e_line(serie = level, name = "Fib", lineStyle = list(color = "green", opacity = 0.8, type = "dotted"), legend = T) |> 
  e_line(serie = level2, name = "Fib", lineStyle = list(color = "blue", opacity = 0.8, type = "dotted"), legend = T) |>
  e_datazoom(type = "slider") |> 
  e_tooltip(trigger = "axis") 

”在此处输入图像描述”

在2022-09-10创建的 reprex v2。 0.2

如您所见,每个点都基于您的“点”列具有不同的颜色。

You could add a random color to each "points" value using group_by. When you add a new candle, it will get the same color because of group_by. Here is a reproducible example:

library(quantmod)
library(echarts4r)
library(dplyr)
library(randomcoloR)

# To show you the colors for each points
GS |> 
  group_by(points) %>% 
  mutate(color = randomcoloR::randomColor()) %>%
  ungroup()
#> # A tibble: 11 × 11
#>    GS.Open GS.High GS.Low GS.Close GS.Volume GS.Adju…¹ date  level level2 points
#>      <dbl>   <dbl>  <dbl>    <dbl>     <dbl>     <dbl> <chr> <dbl>  <dbl>  <int>
#>  1    201.    203.   198.     201.   6494900      162. 2007…   200    250     -5
#>  2    200.    201.   198.     199.   6460200      161. 2007…   200    250     -4
#>  3    198.    200    198.     199.   5892900      161. 2007…   200    250     -3
#>  4    199.    204.   198.     204.   7851000      165. 2007…   200    250     -2
#>  5    204.    205.   202      204.   7147100      165. 2007…   200    250     -1
#>  6    203.    208.   202.     208.   8025700      168. 2007…   200    250      0
#>  7    208.    213.   208.     212.   9039400      171. 2007…   200    250      1
#>  8    211.    214.   210.     214.   6618900      173. 2007…   200    250      2
#>  9    211.    215.   211.     214.   5846600      173. 2007…   200    250      3
#> 10    212.    214.   211.     213.   5306300      172. 2007…   200    250      4
#> 11    214.    215.   209.     210.   6423200      169. 2007…   200    250      5
#> # … with 1 more variable: color <chr>, and abbreviated variable name
#> #   ¹​GS.Adjusted

GS |> 
  group_by(points) %>% 
  mutate(color = randomcoloR::randomColor()) %>%
  ungroup() |>
  e_charts(date) |> 
  e_scatter(serie = GS.Close, name = "Signal", symbol_size = 10) |>
  e_add_nested("itemStyle", color) |>
  e_candle(GS.Open, GS.Close, GS.Low, GS.High, name = "Goldman Sachs",
           lineStyle = list(
             color = "#42eff5", # bull
             color0 = "#f54242", # bear
             borderColor = NA,
             borderColor0 = NA)) |> 
  e_datazoom(type = "slider") |> 
  e_title("Candlestick chart", "Quantmod data") |>
  e_line(serie = level, name = "Fib", lineStyle = list(color = "green", opacity = 0.8, type = "dotted"), legend = T) |> 
  e_line(serie = level2, name = "Fib", lineStyle = list(color = "blue", opacity = 0.8, type = "dotted"), legend = T) |>
  e_datazoom(type = "slider") |> 
  e_tooltip(trigger = "axis") 

enter image description here
Created on 2022-09-10 with reprex v2.0.2


Viridis

Here a viridis option:

library(quantmod)
library(echarts4r)
library(dplyr)
library(viridis)
numb_colors = length(unique(GS$points))

GS |> 
  mutate(color = case_when(points == -5 ~ viridis(numb_colors)[1],
                           points == -4 ~ viridis(numb_colors)[2],
                           points == -3 ~ viridis(numb_colors)[3],
                           points == -2 ~ viridis(numb_colors)[4],
                           points == -1 ~ viridis(numb_colors)[5],
                           points == 0 ~ viridis(numb_colors)[6],
                           points == 1 ~ viridis(numb_colors)[7],
                           points == 2 ~ viridis(numb_colors)[8],
                           points == 3 ~ viridis(numb_colors)[9],
                           points == 4 ~ viridis(numb_colors)[10],
                           points == 5 ~ viridis(numb_colors)[11])) |>
  e_charts(date) |> 
  e_scatter(serie = GS.Close, name = "Signal", symbol_size = 10) |>
  e_add_nested("itemStyle", color) |>
  e_candle(GS.Open, GS.Close, GS.Low, GS.High, name = "Goldman Sachs",
           lineStyle = list(
             color = "#42eff5", # bull
             color0 = "#f54242", # bear
             borderColor = NA,
             borderColor0 = NA)) |> 
  e_datazoom(type = "slider") |> 
  e_title("Candlestick chart", "Quantmod data") |>
  e_line(serie = level, name = "Fib", lineStyle = list(color = "green", opacity = 0.8, type = "dotted"), legend = T) |> 
  e_line(serie = level2, name = "Fib", lineStyle = list(color = "blue", opacity = 0.8, type = "dotted"), legend = T) |>
  e_datazoom(type = "slider") |> 
  e_tooltip(trigger = "axis") 

enter image description here
Created on 2022-09-10 with reprex v2.0.2


You could use the function e_add_nested with "itemstyle" after your e_scatter call. Make sure you have a column with the colors to represent the points. I used the rainbow colors, but you can change them to whatever you want. Here is a reproducible example:

library(quantmod)
library(echarts4r)
library(dplyr)

GS |> 
  mutate(color = rainbow(11)) |>
  e_charts(date) |> 
  e_scatter(serie = GS.Close, name = "Signal", symbol_size = 10) |>
  e_add_nested("itemStyle", color) |>
  e_candle(GS.Open, GS.Close, GS.Low, GS.High, name = "Goldman Sachs",
           lineStyle = list(
             color = "#42eff5", # bull
             color0 = "#f54242", # bear
             borderColor = NA,
             borderColor0 = NA)) |> 
  e_datazoom(type = "slider") |> 
  e_title("Candlestick chart", "Quantmod data") |>
  e_line(serie = level, name = "Fib", lineStyle = list(color = "green", opacity = 0.8, type = "dotted"), legend = T) |> 
  e_line(serie = level2, name = "Fib", lineStyle = list(color = "blue", opacity = 0.8, type = "dotted"), legend = T) |>
  e_datazoom(type = "slider") |> 
  e_tooltip(trigger = "axis") 

enter image description here

Created on 2022-09-10 with reprex v2.0.2

As you can see, each point has a different color based on your "points" column.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文