【问题标题】:Plot all the colums of matrix vs first column of matrix with plotly in R在 R 中用 plotly 绘制矩阵的所有列与矩阵的第一列
【发布时间】:2018-06-18 09:44:05
【问题描述】:

我有一个二维矩阵,我希望根据第一列逐列绘制,我可以使用 matplot 绘制它,有没有办法使用 plotly 做类似的事情,我尝试通过将它们添加为跟踪来使用但它覆盖了没有添加它们的痕迹。

mat <- array(rnorm(10*10*10), dim=c(10, 10, 10))
df <- data.frame( 500*(1:10))
for(i in 3:5){
  for(j in 4:5){
    df <-  cbind(df,mat[1:10,i,j])
  }
}
matplot(x= df[,1], y= as.matrix(df[-1]), type='l')
# plot_ly(y= as.matrix(df[-1]), x= df[,1] ,type="scatter", mode="markers+lines")

【问题讨论】:

    标签: r ggplot2 r-plotly


    【解决方案1】:

    您需要重塑为长格式。使用可读的列名会有所帮助。

    library(tidyr)
    df2 <- df %>% 
      setNames(paste0('V', 1:7)) %>% 
      gather(key, value, -V1)
    
    p <- ggplot(df2, aes(V1, value, color = key)) + geom_line()
    plotly::ggplotly(p)
    

    【讨论】:

      猜你喜欢
      • 2019-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-19
      • 1970-01-01
      • 2019-02-18
      • 2021-10-30
      相关资源
      最近更新 更多