[英]How to plot week for each year on x axis in plot_ly.(R Language)
I have a data set which is as follows:我有一个数据集如下:
I want to plot the actual and fitted on Y axis for X-axis which will show week and year.我想为 X 轴绘制 Y 轴上的实际值和拟合值,这将显示周和年。
I made a new variable named Year_Week and tried plotting it as it x-axis takes ascending order data.我创建了一个名为 Year_Week 的新变量并尝试绘制它,因为它的 x 轴采用升序数据。 The data is plotting correctly but I am struggling with the naming of X-axis.数据绘制正确,但我正在为 X 轴的命名而苦苦挣扎。
This is the plot_ly code I am using.这是我正在使用的 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) })
The x-axis currently shows 201.49K means 9th Week of 2014, 201.53K means 3rd week of 2015. x 轴当前显示 201.49K 表示 2014 年的第 9 周,201.53K 表示 2015 年的第 3 周。
I need x-axis to be better than these numbers.我需要 x 轴比这些数字更好。
How about you do it in ggplot, with a bit of a workaorund:你如何在 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.