如何更改chart_Series()中轴值的颜色?

问题描述 投票:0回答:1

我想改变垂直轴值的颜色,就像这个图表中一样

col.axis=4

par(bg = 1, fg = 2, col.axis=4)
plot(rnorm(10),t="b", col=8)

但是我不能对

chart_Series()
包中的
quantmod
函数执行相同的操作。 从图中可以看出,只有横轴变色,纵轴没有变色。

library(quantmod)
p1 <- rnorm(8000) |> cumsum() |> xts(Sys.time()+1:8000) |> to.minutes(name = NULL) |> round(0)+100

par(bg = 1, fg = 2, col.axis=4)
th <- chart_theme()
th$col$dn.col <- "darkred"
th$col$up.col <- "darkgreen"
th$col$grid=NA
th$col$grid2=8
th$col$dn.border="#454545"
th$col$up.border="#454545"

chart_Series(p1, theme = th)

r plot axis quantmod
1个回答
1
投票

参数

th$col$labels
应该可以做到这一点,但是颜色在
quantmod::chart_Series
内部太硬编码了,所以你必须改变函数。使用

my_chart_Series <- fix("chart_Series")

这将打开一个编辑器,您可以在其中更改该行(第 83 行左右)

cs$Env$theme$labels <- "#333333"

至以下一位:

cs$Env$theme$labels <- theme$col$labels

完成此操作后,将函数设置到正确的命名空间中:

environment(my_chart_Series) <- asNamespace("quantmod")

最后,将您想要的

col.axis = 4
转换为十六进制:

rgb2hex <- function(x) rgb(x[1], x[2], x[3], maxColorValue = 255)

> rgb2hex(col2rgb(4))
[1] "#2297E6"

并定义

th$col$labels <- "#2297E6"

您的新函数将产生所需的输出:

my_chart_Series(p1, theme = th)

© www.soinside.com 2019 - 2024. All rights reserved.