Zizou
Zizou

Reputation: 503

Plotly: How to set different colors for x-axis labels using R?

I'd like to set different colors for date labels on the x axis so that each month has a different color. Like the example below. I don't know if such a topic already exists, but I can't find a solution anywhere.

library(plotly)
today <- Sys.Date()
tm <- seq(0, 100, by = 1)
x <- today + tm
y <- rnorm(length(x))
plot_ly(x = ~x, y = ~y, type = 'scatter', mode = 'lines')  %>%
  layout(
      xaxis = list(
      type = 'date',
      tickformat = "%d %b %Y"
    ))

enter image description here

Upvotes: 1

Views: 3440

Answers (1)

vestland
vestland

Reputation: 61104

The topic already exists. What you're looking to do is possible, but a bit complicated since the colors of the tick labels are associated with the axis and not the ticks themselves. The best current approach is to add extra axes.

Upvotes: 3

Related Questions