繁体   English   中英

如何使用 geom_ribbon 绘图

[英]How to graph with geom_ribbon

我有三张表:

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))  

我试过了,但它给了我这个错误:

错误:Aesthetics 必须是长度为 1 或与数据相同 (624): ymin, ymax, x, y, group

谁能帮助我?

这是一个开始,您可以调整颜色和其他参数以获得您喜欢的动态效果。 我添加了一些美学,并描述了它们的作用:

#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)]

我使用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))
}

使用名为riders series <- 'riders'产生以下结果。

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM