r 基于多列绘制图表

r plotly chart based on multiple columns

我有一个 df,它可以有 2 列或更多列,第一列 month 总是 fixed.I 我正在尝试使用 plotly r 绘制它们。截至目前,它具有三列:月份、苹果、橙色。根据分析,它可以有另一列香蕉。下面是我现在正在使用的代码,但它甚至需要 y 轴的月份列。我该如何解决这个问题:

> sample_test
    month apple orange
2  Aug-17     2      1
3  Dec-17     2      1
4  Feb-18     2      1
5  Jan-18     2      1
6  Jul-17     2      1
7  Jun-17     2      1
8  May-17     2      1
9  Nov-17     2      1
10 Oct-17     2      1
11 Sep-17     2      1

p<- plot_ly(sample_test, x = sample_test$month,  name = 'alpha', type = 'scatter', mode = 'lines',
            line = list(color = 'rgb(24, 205, 12)', width = 4)) %>% 
  layout(#title = "abbb",
    xaxis = list(title = "Time"),
    yaxis = list (title = "Percentage"))

for(trace in colnames(sample_test)){
  p <- p %>% plotly::add_trace(y = as.formula(paste0("~`", trace, "`")), name = trace)
}
p

输出如下所示:

您可以为第一个 y 元素指定轨迹,这将为您提供原始计数。接下来,您可以使用 tickformat 为您的 y 轴添加格式,这将转换为百分比。

sample_test <- data.frame(month = c("Aug-17", "Dec-17", "Feb-18"), apple = c(2,2,2), orange = c(1,1,1))
p <- plot_ly(sample_test, x = sample_test$month, y = ~apple, name = 'alpha', type = 'scatter', mode = 'lines',
        line = list(color = 'rgb(24, 205, 12)', width = 4)) %>% 
     layout(xaxis = list(title = "Time")) %>% 
     layout(yaxis = list(tickformat = "%", title = "Percentage"))

尽管出于某种原因,这似乎只是乘以 100 并出于某种原因添加了 % 标签,而不是实际计算百分比。从此。我并没有真正使用 plotly,但是在 ggplot 中,如果你将数据重塑为 long 并将你的分类变量(在本例中为水果)映射为百分比,你可以这样做。

编辑:根据 OP 的评论,删除了跟踪月份。

p <- plot_ly(type = 'scatter', mode = 'lines') %>% 
  layout(yaxis = list(tickformat = "%", title = "Percentage"))
colNames <- names(sample_test)
colNames <- colNames[-which(colNames == 'month')]
for(trace in colNames){
  p <- p %>% plotly::add_trace(data = sample_test, x = ~ month, y = as.formula(paste0("~`", trace, "`")), name = trace)
  print(paste0("~`", trace, "`"))
}
p

这有帮助吗?

sample_test <- read.table(
  text = '    month apple orange
2  Aug-17     2      1
  3  Dec-17     2      1
  4  Feb-18     2      1
  5  Jan-18     2      1
  6  Jul-17     2      1
  7  Jun-17     2      1
  8  May-17     2      1
  9  Nov-17     2      1
  10 Oct-17     2      1
  11 Sep-17     2      1'
)
sample_test$month <- as.Date(paste('01', sample_test$month, sep = '-'), format = '%d-%b-%y')
library(plotly)
p <- plot_ly(sample_test, type = 'scatter', mode = 'lines',
            line = list(color = 'rgb(24, 205, 12)', width = 4)) %>% 
  layout(#title = "abbb",
    xaxis = list(title = "Time"),
    yaxis = list (title = "Percentage", tickformat = '%'))
for(trace in colnames(sample_test)[2:ncol(sample_test)]){
  p <- p %>% plotly::add_trace(x = sample_test[['month']], y = sample_test[[trace]], name = trace)
}
p

这里有两点需要注意 -

  1. 在处理日期时,最好将它们格式化为日期。这可以在以后避免很多麻烦。它也很有用,因为大多数(如果不是全部)需要处理日期的函数都有处理它们的方法。
  2. for 循环中添加轨迹时,请始终像 data$vectordata[['vector']] 那样显式引用要绘制的矢量,而不是像 y = ~vector,因为 plotly 出于某种原因最终一次又一次地绘制一条轨迹。