Reputation: 45
I am trying to plot a heatmap with plotly in R, and for event listener purposes (click, lasso) I need to keep the data in its original form, but I would like to change the ticks' names on the x axis.
If the ticks resemble a date (even though they're stored as a string), plotly ignores my manual tick names - plot1. In any other case, this works as expected - plot2.
Is this a bug or a feature in plotly? Or, how can I force plotly to not treat the column x as date, but as a string, and to enable manual tick names?
library("data.table")
library("plotly")
set.seed(564351654)
dt <- CJ(x= c("2018-01-01","2019-01-01","2020-01-01"),
y= c("A","B"))
dt[,x2:=paste0("blabla",x)]
dt[,z:=runif(nrow(dt))]
#plot1 ignores my manual ticks
xticks <- dt[,unique(x)]
xvals <- paste0("year",substr(xticks,3,4))
# [1] "year18" "year19" "year20"
plot_ly(dt,
x=~x,
y=~y,
z=~z,
type="heatmap") %>%
layout(
title = "plot1",
xaxis = list(
autotick = F,
tickmode = "array",
ticktext = xvals,
tickvals = xticks
)
)
#plot2 is all good.
xticks <- dt[,unique(x2)]
xvals <- paste0("year",substr(xticks,9,10))
# [1] "year18" "year19" "year20"
plot_ly(dt,
x=~x2,
y=~y,
z=~z,
type="heatmap") %>%
layout(
title = "plot2",
xaxis = list(
autotick = F,
tickmode = "array",
ticktext = xvals,
tickvals = xticks
)
)
Upvotes: 0
Views: 377
Reputation: 6956
You can set type
to category:
#plot1 ignores my manual ticks
xticks <- dt[,unique(x)]
xvals <- paste0("year",substr(xticks,3,4))
# [1] "year18" "year19" "year20"
plot_ly(dt,
x=~x,
y=~y,
z=~z,
type="heatmap") %>%
layout(
title = "plot1",
xaxis = list(
autotick = F,
tickmode = "array",
ticktext = xvals,
tickvals = xticks,
type = "category"
)
)
Upvotes: 1