使用 R/Plotly 更改热图的 y 轴刻度值

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

我有以下 R 代码:

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

  1. 如何更改绘图以仅查看 y 轴上的值 0、1 和 2 而看不到值 -0.5、0.5、1.5 和 2.5(类似于 x-轴)

  2. 如何将 10 添加到 y 轴上的所有刻度值,以便得到 10、11 和 12 而不是 0、1 和 2。

请注意,我正在寻找一个通用概念,因为我的矩阵在现实中要大得多。

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)

dtick = 1 添加到 layout 中的 yaxis

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.

您可以在 ticktext 中指定应该为每个刻度显示的文本。您还需要提供 tickvals 并将 tickmode 设置为 array.

完整代码

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