基于多列的r-plotly图表

基于多列的r-plotly图表,r,plotly,r-plotly,R,Plotly,R Plotly,我有一个df,它可以有两列或更多列,第一列月份总是固定的。我正在尝试使用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

我有一个df,它可以有两列或更多列,第一列<代码>月份总是固定的。我正在尝试使用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
>样本测试
月苹果橙
8月2日至17日2 1
2017年12月3日2 1
2018年2月4日2 1
2018年1月5日2 1
7月6日至17日2 1
2017年6月7日2 1
5月8日至17日2 1
11月9日至17日2 1
10月10日至17日2 1
2017年9月11日2 1
p%
布局(#title=“abbb”,
xaxis=列表(title=“Time”),
yaxis=列表(title=“百分比”))
用于(COLNAME中的跟踪(样本_测试)){
p%plotly::添加跟踪(y=as.formula(粘贴0(“~`”,跟踪,“`”)),名称=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"))
这有帮助吗

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

sample\u您将获得一个月的跟踪测试从您的问题中不清楚您不希望这样。编辑以反映。
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