使用plotly()绘制数据帧的列,而不管列数

问题描述 投票:2回答:2

我有一个名为plt.total的数据帧,由5行(obs)和7列(变量)组成,如下所示:

set.seed(2018)
plt.total <- data.frame(replicate(7,runif(5, min=0.80, max=0.9)))
colnames(plt.total) <- c("R", paste0("Matrix",1:6))
plt.total[,1] <- c(1:5)

然后我用plotly绘制它如下:

plot_ly(data=plt.total, x = ~R, y = ~Matrix1, name = 'Matrix 1', type = 'scatter', mode = 'lines',
                  line = list(color = 'rgb(205, 12, 24)', width = 3, dash = 'dashdot')) %>%
        add_trace(y = ~Matrix2, name = 'Matrix 2', line = list(color = 'rgb(22, 96, 167)', width = 3, dash = 'solid' )) %>%
        add_trace(y = ~Matrix3, name = 'Matrix 3', line = list(color = 'rgb(205, 12, 24)', width = 3, dash = 'longdashdot')) %>%
        add_trace(y = ~Matrix4, name = 'Matrix 4', line = list(color = 'rgb(22, 96, 167)', width = 3, dash = 'dash')) %>%
        add_trace(y = ~Matrix5, name = 'Matrix 5', line = list(color = 'rgb(205, 12, 24)', width = 3, dash = 'dot')) %>%
        add_trace(y = ~Matrix6, name = 'Matrix 6', line = list(color = 'rgb(22, 96, 167)', width = 3, dash = 'dot')) %>%
layout(title = "", legend = list(x=0, y=0, font = list(family = "Times", size = 14, color = "#000")))

enter image description here

但是,我的问题是plt.total不时会改变“列”的数量,我试图找到一种方法来避免每次修改我的脚本(使用add_trace)。因此,我的目标是使用plotly绘制数据框的每一列的输入,而不管列数。

谢谢

r dataframe plotly
2个回答
2
投票

一种方法是将plt.total表转换为长整齐的格式,以便您可以映射到矩阵编号而不是手动添加单独的系列。

在这里,我们将数据的形状更改为长,使用新的“矩阵”字段来表示观察属于哪个系列。

plt.total.tidy <-
  plt.total %>%
  tidyr::gather(matrix, value, -R)

我的情节还不是很好,所以我先在ggplot中制作图表然后使用plotly::ggplotly将其移植到图表中,添加你所做的布局选择。如果您想手动指定颜色和线型,而不是为您指定颜色和线型,则可以使用scale_color_manualscale_linetype_manual函数来执行此操作。

library(ggplot2)
p <- ggplot(plt.total.tidy, aes(R, value, color = matrix, lty = matrix)) +
  geom_line(size = 1.1) +
  theme(legend.position = c(0.1,0.1)) #+   
  # Could add below lines if you want to manually pick colors and linetypes
  # scale_color_manual(....) +
  # scale_linetype_manual(....) +

ggplotly(p) %>%
  layout(title = "", legend = list(x=0, y=0, font = list(family = "Times", size = 14, color = "#000")))

enter image description here


2
投票

我设法使用列表而不是数据帧,如下所示。也许有人知道更好的方式?

set.seed(2018)
plt.total <- list()
n <- 10 # number of lines in the plot
for (i in 1:n) {
    plt.total[i] <- list(
    list(x=c(1:5), y=replicate(1,runif(5, min=0.80, max=0.9)), color=randomcoloR::randomColor(n, luminosity="light")))
}

p <- plot_ly()
p
for(i in 1:length(plt.total)) {  p <- add_trace(p, y=plt.total[[i]]$y, 
                                                   x=plt.total[[i]]$x, line=list(color=plt.total[[i]]$color), 
                                                   name=paste0("Matrix",i), mode = "lines")%>%
                                          layout(title = "", legend = list(x=0, y=0, 
                                                   font = list(family = "Times", 
                                                   size = 14, color = "#000")),
                                          xaxis = list(title = "R Value", showline = TRUE, 
                                                   annotations= x, range=c(1, 5), linewidth = 2, mirror = "ticks",
                                                   titlefont = list(family = "Times", size = 18, color = "#000"), 
                                                   tickfont = list(family = "Times", size = 14, color = "#000")),
                                          yaxis = list (title = "Value", showline = TRUE, 
                                                   linewidth = 2,mirror = "ticks", 
                                                   titlefont = list(family = "Times", size = 18, color = "#000"), 
                                                   tickfont = list(family = "Times", size = 14, color = "#000")) )
}
p

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.