簡體   English   中英

如何在 plot_ly.(R 語言) 中在 x 軸上繪制每年的周

[英]How to plot week for each year on x axis in plot_ly.(R Language)

我有一個數據集如下:

在此處輸入圖片說明

我想為 X 軸繪制 Y 軸上的實際值和擬合值,這將顯示周和年。

我創建了一個名為 Year_Week 的新變量並嘗試繪制它,因為它的 x 軸采用升序數據。 數據繪制正確,但我正在為 X 軸的命名而苦苦掙扎。

在此處輸入圖片說明

這是我正在使用的 plot_ly 代碼。

output$plot_forecast <- renderPlotly({
data <- forecast_data()
plot <- plot_ly(data, type = "scatter", mode = 'lines') %>% 
  add_lines(x = ~ data$year_week, 
            y = ~ data$actual, 
            name ="Actual",
            line=list(color="Red")) %>%
  add_lines(x = ~ data$year_week, 
            y = ~ data$fitted, 
            name ="Predicted",
            line=list(color="Green")) %>%
  layout(title = "Total Sales (Actual vs Predicted)",
         xaxis = list(title = "Year Week"),
         yaxis = list (title = "Total POS Sales"),
         legend = list(x = 100, y = 0.5))
return(plot) })

x 軸當前顯示 201.49K 表示 2014 年的第 9 周,201.53K 表示 2015 年的第 3 周。

我需要 x 軸比這些數字更好。

你如何在 ggplot 中做到這一點,有點工作:

    df$YearWeek=paste(df$Year,"Week",df$Week,sep="_")
df0=df%>%select(YearWeek,Actual,Fitted)%>%melt(id.vars="YearWeek")
ggplot(df0,aes(x=YearWeek,y=value,col=variable,group=variable))+geom_line()+
  theme(axis.text.x = element_text(angle = 45))

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM