我有三张桌子:
Upper Bound
Q C
1 30
2 50
3 40
Lower Bound
Q C
1 10
2 15
3 20
Run Code Online (Sandbox Code Playgroud)
不良数据:
Q C Name
1 50 Sample 1
2 40 Sample 1
3 30 Sample 1
1 0 Sample 2
2 60 Sample 2
3 5 Sample 2
Run Code Online (Sandbox Code Playgroud)
我想要一个图表,以灰色绘制下限和上限,并填充之间的所有内容,并用不同的颜色和图例在顶部绘制不良样本:
plot <- ggplot(Bad_Data, aes(x = Bad_Data$Q, y = Bad_Data$C, group = 1))
plot + geom_line(aes(color = N)) + geom_ribbon(aes(ymin = Lower_Bound$C, ymax = Upper_Bound$C))
Run Code Online (Sandbox Code Playgroud)
我尝试过,但它给了我这个错误:
错误:美学必须是长度 1 或与数据 (624) 相同:ymin、ymax、x、y、group
谁能帮助我?
我用geom_ribbon预测来遮蔽预测置信区间。您可以组合多个数据框以使其全部正常工作。您需要传递数据框、x 值(日期)和 y 值(之间有阴影的两条线)。
以下函数用于绘制序列、模型回测、预测和置信区间。
plot_predictions <- function(start_date) {
# function to use Plotly to plot the predictions,
# confidence interval and actuals
# inputs:
# plot_df, starting date of the series, r-pred (prediction data frame)
# output: interactive plot of the actual and the model backtest
# set the end date parameter first
pred_end <- as.Date(tail(r_pred$week_ending, 1))
p <- ggplot() +
# add the backtest series from the backtest data frame
geom_line(data=r_back, mapping = aes(x=week_ending, y=Backtest_Model,
color = 'Backtest Model'), linetype='solid') +
# add the actual series from the training dataframe
# use aes_string to pass the series variable
geom_line(data=r_train, mapping = aes_string(x='week_ending', y=series,
color = 'series'), linetype='solid') +
# r_pred holds he predictions and confidence levels
geom_line(data=r_pred, mapping = aes(x=week_ending, y=Predictions,
color = 'Predictions'), linetype='solid') +
# upper forecast limit
geom_line(data=r_pred, mapping = aes(x=week_ending, y=upper_conf_limit,
color = 'upper_limit'), linetype='solid') +
# lower forecast limit
geom_line(data=r_pred, mapping = aes(x=week_ending, y=lower_conf_limit,
color = 'lower_limit'), linetype='solid') +
# format the plot
scale_linetype_manual() +
# prediction_pal is a five color palette
scale_color_manual(values = pred_pal, name = "Series") +
# fill between lines with geom_ribbon using a blue toned down by alpha
geom_ribbon(data=r_pred, aes(x = week_ending,
ymin=lower_conf_limit,
ymax=upper_conf_limit),
fill="blue", alpha=0.2) +
# format the y axis with commas
scale_y_continuous(label = comma)
# extend the date series from the beginning to end of predictions
scale_x_date(breaks = pretty_breaks(20),
limits = c(as.Date(start_date), pred_end)) +
# add the custom theme
theme_bryan() +
# customize the plot
# rotate the text of the x axis
theme(axis.text.x = element_text(angle= 45, hjust = 1)) +
labs(fill = 'Series') +
guides(color = guide_legend(reverse = FALSE)) +
# add the holiday lines as vertical red dotted lines
# holidays in the training set
geom_vline(xintercept = as.numeric(holiday$week_ending),
linetype='dotted', colour = 'red', alpha =0.5) +
# holidays in the forecasting set
geom_vline(xintercept = as.numeric(r_exog_lines$week_ending),
linetype='dotted', colour = 'red', alpha =0.5)
# output the plot in Plotly format
subplot(with_options(list(digits = 0),
ggplotly(p))) %>%
layout(legend = list(orientation = 'v', y = .1, x = 0))
}
Run Code Online (Sandbox Code Playgroud)
使用称为“riders”的系列series <- 'riders'会产生以下结果。