r基于多列的绘图

时间:2018-08-15 18:41:10

标签: r plotly r-plotly

我有一个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

输出看起来像这样:enter image description here

2 个答案:

答案 0 :(得分: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并将类别变量(在这种情况下为水果)映射为百分比,则可以执行此操作。

Plotly plot

编辑:根据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

Edited plot

答案 1 :(得分:1)

有帮助吗?

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

enter image description here

这里有几件事要注意-

  1. 在处理日期时,最好将其格式化为日期。以后可以节省很多头痛。这也很有用,因为大多数(即使不是全部)需要处理日期的函数都具有处理它们的方法。
  2. for循环中添加迹线时,总是像data$vectordata[['vector']]而不是y = ~vector那样引用要显式绘制的矢量,因为plotly由于某种原因最终只能一遍又一遍地绘制出一条轨迹。