无法正确执行带有两条线的绘图折线图

Unable to properly execute a plotly line graph with two lines

我正在尝试在绘图折线图上绘制 2 条线。当简单地绘制其中一条线时,它会起作用;但是,当我将 "add_trace" 用于另一条线时,它只是将两条线 运行 单独用于 x 轴(有点像 y=0)。我附上了下面的代码:

# #Profit Dataframe
 df5 <- data.frame(Account_Num,Account_Num_Spec,Account_Name,Billing_Num,Date,Tran_Type,Transaction_Amt2,Bar_Date, Bar_Month,Trannn) 
 df5_subset <- reactive({
   n1 <- input$PYR
   n2 <- input$PYT

   if(n2 == "Combined"){
     c5 <- subset(df5, Bar_Date == n1)
  }
else{
  c5 <- subset(df5, Bar_Date == n1 & Trannn == n2)
}

return(c5)
})     
clarify <- reactive({
check <- input$PYT

if(check == "Combined"){

  jan_dataB <- subset(df5_subset(), Bar_Month == 01, Tran_Type == "Bill")
  feb_dataB <- subset(df5_subset(), Bar_Month == 02, Tran_Type == "Bill")
  mar_dataB <- subset(df5_subset(), Bar_Month == 03, Tran_Type == "Bill")
  apr_dataB <- subset(df5_subset(), Bar_Month == 04, Tran_Type == "Bill")
  may_dataB <- subset(df5_subset(), Bar_Month == 05, Tran_Type == "Bill")
  jun_dataB <- subset(df5_subset(), Bar_Month == 06, Tran_Type == "Bill")
  jul_dataB <- subset(df5_subset(), Bar_Month == 07, Tran_Type == "Bill")
  aug_dataB <- subset(df5_subset(), Bar_Month == 08, Tran_Type == "Bill")
  sep_dataB <- subset(df5_subset(), Bar_Month == 09, Tran_Type == "Bill")
  oct_dataB <- subset(df5_subset(), Bar_Month == 10, Tran_Type == "Bill")
  nov_dataB <- subset(df5_subset(), Bar_Month == 11, Tran_Type == "Bill")
  dec_dataB <- subset(df5_subset(), Bar_Month == 12, Tran_Type == "Bill")
  jan_dataR <- subset(df5_subset(), Bar_Month == 01, Tran_Type == "Receipt")
  feb_dataR <- subset(df5_subset(), Bar_Month == 02, Tran_Type == "Receipt")
  mar_dataR <- subset(df5_subset(), Bar_Month == 03, Tran_Type == "Receipt")
  apr_dataR <- subset(df5_subset(), Bar_Month == 04, Tran_Type == "Receipt")
  may_dataR <- subset(df5_subset(), Bar_Month == 05, Tran_Type == "Receipt")
  jun_dataR <- subset(df5_subset(), Bar_Month == 06, Tran_Type == "Receipt")
  jul_dataR <- subset(df5_subset(), Bar_Month == 07, Tran_Type == "Receipt")
  aug_dataR <- subset(df5_subset(), Bar_Month == 08, Tran_Type == "Receipt")
  sep_dataR <- subset(df5_subset(), Bar_Month == 09, Tran_Type == "Receipt")
  oct_dataR <- subset(df5_subset(), Bar_Month == 10, Tran_Type == "Receipt")
  nov_dataR <- subset(df5_subset(), Bar_Month == 11, Tran_Type == "Receipt")
  dec_dataR <- subset(df5_subset(), Bar_Month == 12, Tran_Type == "Receipt")

  yloc1B <- sum(as.numeric(as.character(jan_dataB$Transaction_Amt2)))
  yloc2B <- sum(as.numeric(as.character(feb_dataB$Transaction_Amt2)))
  yloc3B <- sum(as.numeric(as.character(mar_dataB$Transaction_Amt2)))
  yloc4B <- sum(as.numeric(as.character(apr_dataB$Transaction_Amt2)))
  yloc5B <- sum(as.numeric(as.character(may_dataB$Transaction_Amt2)))
  yloc6B <- sum(as.numeric(as.character(jun_dataB$Transaction_Amt2)))
  yloc7B <- sum(as.numeric(as.character(jul_dataB$Transaction_Amt2)))
  yloc8B <- sum(as.numeric(as.character(aug_dataB$Transaction_Amt2)))
  yloc9B <- sum(as.numeric(as.character(sep_dataB$Transaction_Amt2)))
  yloc10B <- sum(as.numeric(as.character(oct_dataB$Transaction_Amt2)))
  yloc11B <- sum(as.numeric(as.character(nov_dataB$Transaction_Amt2)))
  yloc12B <- sum(as.numeric(as.character(dec_dataB$Transaction_Amt2)))
  yloc1R <- sum(as.numeric(as.character(jan_dataR$Transaction_Amt2)))
  yloc2R <- sum(as.numeric(as.character(feb_dataR$Transaction_Amt2)))
  yloc3R <- sum(as.numeric(as.character(mar_dataR$Transaction_Amt2)))
  yloc4R <- sum(as.numeric(as.character(apr_dataR$Transaction_Amt2)))
  yloc5R <- sum(as.numeric(as.character(may_dataR$Transaction_Amt2)))
  yloc6R <- sum(as.numeric(as.character(jun_dataR$Transaction_Amt2)))
  yloc7R <- sum(as.numeric(as.character(jul_dataR$Transaction_Amt2)))
  yloc8R <- sum(as.numeric(as.character(aug_dataR$Transaction_Amt2)))
  yloc9R <- sum(as.numeric(as.character(sep_dataR$Transaction_Amt2)))
  yloc10R <- sum(as.numeric(as.character(oct_dataR$Transaction_Amt2)))
  yloc11R <- sum(as.numeric(as.character(nov_dataR$Transaction_Amt2)))
  yloc12R <- sum(as.numeric(as.character(dec_dataR$Transaction_Amt2)))


xloc <- list("JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC")   
ylocB <- list(yloc1B,yloc2B,yloc3B,yloc4B,yloc5B,yloc6B,yloc7B,yloc8B,yloc9B,yloc10B,yloc11B,yloc12B)
ylocR <- list(yloc1R,yloc2R,yloc3R,yloc4R,yloc5R,yloc6R,yloc7R,yloc8R,yloc9R,yloc10R,yloc11R,yloc12R)

data <- data.frame(xloc,ylocB,ylocR)

p <- plot_ly(data, x=~xloc, y=~ylocB, name ='Billed', type = 'scatter', mode='lines',fill='tozeroy') %>%
  add_trace(data, x=~xloc, y=~ylocR, name = "Received", type = 'scatter', mode='lines',fill='tozeroy', secondary_y=TRUE) %>%
  layout(title= paste(LGT()), xaxis= list(title=paste(lgt())), yaxis= list(title="Amount ($)"))

我找不到我在哪里没有正确编码。

如果没有可重现的数据,很难看出问题到底是什么。但是,您似乎需要建立 x 轴值的顺序。但首先,从向量而不是列表创建数据框。

xloc <- c("JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC")   
ylocB <- c(yloc1B,yloc2B,yloc3B,yloc4B,yloc5B,yloc6B,yloc7B,yloc8B,yloc9B,yloc10B,yloc11B,yloc12B)
ylocR <- c(yloc1R,yloc2R,yloc3R,yloc4R,yloc5R,yloc6R,yloc7R,yloc8R,yloc9R,yloc10R,yloc11R,yloc12R)

data <- data.frame(xloc,ylocB,ylocR)

data$xloc <- factor(data$xloc, levels = data[["xloc"]])

在此之后,您应该可以绘图了。