【问题标题】:r plotly chart based on multiple columns [duplicate]基于多列的r plotly图表[重复]
【发布时间】:2019-01-22 16:16:36
【问题描述】:

我有一个 df 可以有 2 列或更多列,第一列 month 始终固定。我正在尝试使用 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

输出如下所示:

【问题讨论】:

    标签: r plotly r-plotly


    【解决方案1】:

    您可以为第一个 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 并出于某种原因添加% 标签,而不是实际计算百分比。从这个SO answer, looks like that's all it does。我并没有真正使用 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
    

    【讨论】:

    • 你得到一个月的踪迹
    • 从你的问题中不清楚你不想要那个。编辑反映。
    【解决方案2】:

    这有帮助吗?

    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 出于某种原因最终只绘制了一条迹线一遍又一遍。

    【讨论】:

      猜你喜欢
      • 2017-10-14
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 1970-01-01
      • 2021-01-10
      • 2018-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多