简体   繁体   English

绘制矩阵的所有列与矩阵的第一列,并在R中进行绘制

[英]Plot all the colums of matrix vs first column of matrix with plotly in R

I am having a 2d matrix which i wish to plot column wise against a first column,I am able to plot it using matplot, is there any way to do a similar thing using plotly, I tried using by adding them as traces but it was overwriting the traces not adding them. 我有一个二维矩阵,我想按第一列对列进行逐行绘制,我可以使用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")

You need to reshape to long format. 您需要重塑为长格式。 Using readable columns names would help. 使用可读的列名会有所帮助。

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)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM