Reputation: 934
Suppose I have some code such as,
library(plotly)
Animals <- c("giraffes", "orangutans", "monkeys")
SF_Zoo <- c(20, 14, 23)
LA_Zoo <- c(12, 18, 29)
data <- data.frame(Animals, SF_Zoo, LA_Zoo)
p <- plot_ly(data, x=~Animals, y=~SF_Zoo, type='bar', name='SF Zoo') %>%
add_trace(y=~LA_Zoo, name='LA Zoo') %>%
layout(yaxis=list(title='Count'), barmode='stack')
I'm trying to figure out how I color the three x-axis labels (giraffes, monkeys, orangutans) different colors, I've been trying to do this by using something like,
xaxis=list(tickfont=list(color=c('red', 'yellow', 'blue')))
inside the layout
function, but it doesn't work.
How can I modify tick labels individually?
Upvotes: 2
Views: 2618
Reputation: 51
I think I found a trick to use HTML syntax for this use case.
library(dplyr)
library(plotly)
plot_ly(x = 1:3, y = 1:3) %>%
layout(
xaxis = list(
tickmode = "array",
tickvals = 1:3,
ticktext = c(
"<span style='color:red'>A</span>",
"<span style='color:yellow'>B</span>",
"<span style='color:blue'>C</span>"
)
)
)
Upvotes: 2
Reputation: 61104
At the time being this isn't as straight forward as xaxis=list(tickfont=list(color=c('red', 'yellow', 'blue')))
since the color of the tick labels are associated with the properties of the axis and not the individual ticks. You can assign different colors to different parts of the visible axis, but you'll have to add extra axes. This has been done in detail here: Coloring the axis tick text by multiple colors
Upvotes: 4