gpt4 book ai didi

r - 使用 R 中的 slider 绘制多个数据帧的热图

转载 作者:行者123 更新时间:2023-12-04 09:36:41 25 4
gpt4 key购买 nike

我有多个 data.frames,每一个都代表不同时间点的个人成对交互。
这是我的 data.frames 外观的示例。

df1 <- matrix(data = rexp(9, rate = 10), nrow = 3, ncol = 3)
df2 <- matrix(data = rexp(16, rate = 10), nrow = 4, ncol = 4)
df3 <- matrix(data = rexp(4, rate = 10), nrow = 2, ncol = 2)
我想绘制它们,因为它在此页面中指出( https://plotly.com/r/sliders/)
使用 slider ,我可以从一个热图移动到另一个。
到目前为止,我已经尝试过 plotly 但我没有成功。非常感谢任何帮助。
我在这个问题上挣扎了很长时间。在这一点上我可能有点盲目,所以如果问题很愚蠢,请原谅我。

最佳答案

关注 Sine Wave Slider https://plotly.com/r/sliders/ 上的示例这可以像这样实现。我的方法的第一步涉及将矩阵转换为具有 x、y、z 列的数据帧。其次,我们绘制热图而不是线条。

df1 <- matrix(data = rexp(9, rate = 10), nrow = 3, ncol = 3)
df2 <- matrix(data = rexp(16, rate = 10), nrow = 4, ncol = 4)
df3 <- matrix(data = rexp(4, rate = 10), nrow = 2, ncol = 2)

library(tibble)
library(tidyr)
library(plotly)

# Make dataframes
d <- lapply(list(df1, df2, df3), function(d) {
d %>%
as_tibble(.colnames = seq(ncol(.))) %>%
rowid_to_column("x") %>%
pivot_longer(-x, names_to = "y", values_to = "z") %>%
mutate(y = stringr::str_extract(y, "\\d"),
y = as.numeric(y))
})

aval <- list()
for(step in seq_along(d)){
aval[[step]] <-list(visible = FALSE,
name = paste0('v = ', step),
x = d[[step]]$x,
y = d[[step]]$y,
z = d[[step]]$z)
}
aval[1][[1]]$visible = TRUE

steps <- list()
fig <- plot_ly()

for (i in seq_along(aval)) {
fig <- add_trace(fig, x = aval[i][[1]]$x, y = aval[i][[1]]$y, z = aval[i][[1]]$z, visible = aval[i][[1]]$visible,
name = aval[i][[1]]$name, type = "heatmap")
fig
step <- list(args = list('visible', rep(FALSE, length(aval))), method = 'restyle')

step$args[[2]][i] = TRUE
steps[[i]] = step
}

fig <- fig %>%
layout(sliders = list(list(active = 0,
currentvalue = list(prefix = "Heatmap: "),
steps = steps)))
fig
enter image description here

关于r - 使用 R 中的 slider 绘制多个数据帧的热图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62544749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com