Liky
Liky

Reputation: 1247

Change y-axis tick values of a heatmap with R/Plotly

I have the following R code:

library(plotly)
A <- matrix(seq(1, 12), nrow = 4, ncol = 3)
p <- plot_ly(z = t(A), type = "heatmap", colorscale = "Greys")
p 

enter image description here

  1. How can I change the plot to see only the values 0, 1 and 2 on the y-axis but not the values -0.5, 0.5, 1.5 and 2.5 (something similar to the x-axis)

  2. How can I add 10 to all my tick values on the y-axis in order to have 10, 11 and 12 instead of 0, 1 and 2.

Note that I am looking for a general concept as my matrix in reality is much bigger.

Upvotes: 1

Views: 2224

Answers (1)

Maximilian Peters
Maximilian Peters

Reputation: 31669

How can I change the plot to see only the values 0, 1 and 2 on the y-axis but not the values -0.5, 0.5, 1.5 and 2.5 (something similar to the x-axis)

Add dtick = 1 to your yaxis in layout.

How can I add 10 to all my tick values on the y-axis in order to have 10, 11 and 12 instead of 0, 1 and 2.

You can specify in ticktext the text which is supposed to be shown for each tick. You also need to supply the tickvals and set tickmode to array.

enter image description here

Complete code

library(plotly)
A <- matrix(seq(1, 12), nrow = 4, ncol = 3)
p <- plot_ly(z = t(A), type = "heatmap", colorscale = "Greys") %>%
  layout(yaxis = list(dtick = 1, ticktext = 10:12, tickmode="array", tickvals = 0:2))
p 

Upvotes: 5

Related Questions