Search code examples
rplotaxisquantmod

How to change the color of axes values in chart_Series()?


I want to change the color of the vertical axes values like in this chart col.axis=4

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

enter image description here

But I can't do the same with the chart_Series() function from quantmod package. As can be seen from the figure, only the horizontal axis changed color, but not the vertical axis.

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)

enter image description here


Solution

  • The parameter th$col$labels should do this, but the color is too hard-coded inside quantmod::chart_Series, so you have to alter the function. Use

    my_chart_Series <- fix("chart_Series")
    

    This opens an editor in which you can alter the line (around line 83)

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

    to the following one:

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

    After doing this, set the function into the correct namespace:

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

    Finally, convert your desired col.axis = 4 into HEX:

    rgb2hex <- function(x) rgb(x[1], x[2], x[3], maxColorValue = 255)
    
    > rgb2hex(col2rgb(4))
    [1] "#2297E6"
    

    and define

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

    Your new function will then yield the desired output:

    my_chart_Series(p1, theme = th)
    

    enter image description here