我正在try 将水平线添加到R
plotly
heatmap
,它将位于heatmap
的几行之间.
下面是示例data.frame
和heatmap
:
library(plotly)
set.seed(1)
df <- matrix(rnorm(18), nrow = 6, ncol = 3, dimnames = list(paste0("r",1:6),1:3)) %>%
reshape2::melt() %>%
dplyr::rename(row=Var1,col=Var2)
plot_ly(x = df$col, y = df$row,z = df$value,type = "heatmap")
现在假设我想在"r2"
和"r3"
之间添加一条横穿整个heatmap
的水平线,在"r4"
和"r5"
之间添加一条类似的线.
我不知道对应的y
个位置应该是什么.
如果我的df$row
岁是integer/numeric
岁而不是character
岁,我就能做到这一点:
library(plotly)
set.seed(1)
df <- matrix(rnorm(18), nrow = 6, ncol = 3, dimnames = list(1:6,1:3)) %>%
reshape2::melt() %>%
dplyr::rename(row=Var1,col=Var2)
plot_ly(x = df$col, y = df$row,z = df$value,type = "heatmap") %>%
add_lines(y = 2.5, x = c(min(df$col)-0.5,max(df$col)+0.5), line = list(color = "black",dash = "dot",size = 5),inherit = FALSE,showlegend = FALSE) %>%
add_lines(y = 4.5, x = c(min(df$col)-0.5,max(df$col)+0.5), line = list(color = "black",dash = "dot",size = 5),inherit = FALSE,showlegend = FALSE)
所以我的问题是:
- 如果热图的行数为
character
,有没有办法在行之间放置水平线? - 有没有一种更紧凑的方法来添加多条水平线,而不是像我上面的代码那样显式地编码每一条?