我有三张桌子:
Upper Bound
Q C
1 30
2 50
3 40
Lower Bound
Q C
1 10
2 15
3 20
错误数据:
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
我想要一个图表,以灰色显示下边界和上边界,并填充其间的所有内容,并使用不同的颜色和图例将不良样本绘制在顶部:
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))
我试过了,但它给了我这个错误:
错误:美学必须是长度1或与数据相同(624):ymin,ymax,x,y,group
任何可以帮助我的人?
答案 0 :(得分:2)
我使用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))
}
使用称为车手series <- 'riders'
的系列会产生以下结果。
答案 1 :(得分:1)
这是一个开始,您可以调整颜色和其他参数,以获得您想要的动态。我添加了一些美学,并描述了每个人的作用:
#Prepare
Bad_Data$Lower_Bound <- Lower_Bound$C
Bad_Data$Upper_Bound <- Upper_Bound$C
#Plot
library(ggplot2)
p <- ggplot(Bad_Data, aes(x = Q, y = C, color=Name, group=Name))
p <- p + geom_line()
p + geom_ribbon(aes(ymin=Lower_Bound, ymax=Upper_Bound),
alpha=0.1, #transparency
linetype=1, #solid, dashed or other line types
colour="grey70", #border line color
size=1, #border line size
fill="green") #fill color
以下是数据:
Upper_Bound <- read.table(text="Q C
1 30
2 50
3 40", header=T)
Lower_Bound <- read.table(text="Q C
1 10
2 15
3 20", header=T)
Bad_Data <- read.table(text="Q C Name
1 50 Sample1
2 40 Sample1
3 30 Sample1
1 0 Sample2
2 60 Sample2
3 5 Sample2", header=T)
修改强>
更安全的准备:
Bad_Data$Lower_Bound <- Lower_Bound$C[match(Bad_Data$Q, Lower_Bound$Q)]
Bad_Data$Upper_Bound <- Upper_Bound$C[match(Bad_Data$Q, Upper_Bound$Q)]