R: Plotting Multiple Confidence Intervals on the Same Graph

早过忘川 提交于 2021-01-29 15:14:09

问题


I am using the R programming language. I am trying to learn how to plot multiple time series on the same graph, as well as including their confidence intervals (in this case, their maximum and minimum values).

Suppose I have two time series like this:

library(xts)
library(ggplot2)
library(dplyr)
library(plotly)
library(lubridate)

#time series 1
date_decision_made = seq(as.Date("2014/1/1"), as.Date("2016/1/1"),by="day")

property_damages_in_dollars <- rnorm(731,100,10)

final_data <- data.frame(date_decision_made, property_damages_in_dollars)


#####aggregate

final_data$year_month <- format(as.Date(final_data$date_decision_made), "%Y-%m")
final_data$year_month <- as.factor(final_data$year_month)


f1 = final_data %>% group_by (year_month) %>% summarise(max_value = max(property_damages_in_dollars), mean_value = mean(property_damages_in_dollars), min_value = min(property_damages_in_dollars))

and

#time series 2
date_decision_made = seq(as.Date("2014/1/1"), as.Date("2016/1/1"),by="day")

property_damages_in_dollars <- rnorm(731,50,10)

final_data2 <- data.frame(date_decision_made, property_damages_in_dollars)


#####aggregate

final_data2$year_month <- format(as.Date(final_data$date_decision_made), "%Y-%m")
final_data2$year_month <- as.factor(final_data$year_month)


f2 = final_data2 %>% group_by (year_month) %>% summarise(max_value = max(property_damages_in_dollars), mean_value = mean(property_damages_in_dollars), min_value = min(property_damages_in_dollars))

I can plot each of them separately with their confidence intervals:

fig <- plot_ly(f1, x = ~year_month, y = ~max_value, type = 'scatter', mode = 'lines',
        line = list(color = 'transparent'),
        showlegend = FALSE, name = 'max_value') 

fig <- fig %>% add_trace(y = ~min_value, type = 'scatter', mode = 'lines',
            fill = 'tonexty', fillcolor='rgba(0,100,80,0.2)', line = list(color = 'transparent'),
            showlegend = FALSE, name = 'min_value') 

fig <- fig %>% add_trace(x = ~year_month, y = ~mean_value, type = 'scatter', mode = 'lines',
            line = list(color='rgb(0,100,80)'),
            name = 'Average') 


fig <- fig %>% layout(title = "Average Property Damages",
         paper_bgcolor='rgb(255,255,255)', plot_bgcolor='rgb(229,229,229)',
         xaxis = list(title = "Months",
                      gridcolor = 'rgb(255,255,255)',
                      showgrid = TRUE,
                      showline = FALSE,
                      showticklabels = TRUE,
                      tickcolor = 'rgb(127,127,127)',
                      ticks = 'outside',
                      zeroline = FALSE),
         yaxis = list(title = "Dollars",
                      gridcolor = 'rgb(255,255,255)',
                      showgrid = TRUE,
                      showline = FALSE,
                      showticklabels = TRUE,
                      tickcolor = 'rgb(127,127,127)',
                      ticks = 'outside',
                      zeroline = FALSE))

fig

and

fig1 <- plot_ly(f2, x = ~year_month, y = ~max_value, type = 'scatter', mode = 'lines',
        line = list(color = 'transparent'),
        showlegend = FALSE, name = 'max_value') 

fig1 <- fig1 %>% add_trace(y = ~min_value, type = 'scatter', mode = 'lines',
            fill = 'tonexty', fillcolor='rgba(0,100,80,0.2)', line = list(color = 'transparent'),
            showlegend = FALSE, name = 'min_value') 

fig1 <- fig1 %>% add_trace(x = ~year_month, y = ~mean_value, type = 'scatter', mode = 'lines',
            line = list(color='rgb(0,100,80)'),
            name = 'Average') 


fig1 <- fig1 %>% layout(title = "Average Property Damages",
         paper_bgcolor='rgb(255,255,255)', plot_bgcolor='rgb(229,229,229)',
         xaxis = list(title = "Months",
                      gridcolor = 'rgb(255,255,255)',
                      showgrid = TRUE,
                      showline = FALSE,
                      showticklabels = TRUE,
                      tickcolor = 'rgb(127,127,127)',
                      ticks = 'outside',
                      zeroline = FALSE),
         yaxis = list(title = "Dollars",
                      gridcolor = 'rgb(255,255,255)',
                      showgrid = TRUE,
                      showline = FALSE,
                      showticklabels = TRUE,
                      tickcolor = 'rgb(127,127,127)',
                      ticks = 'outside',
                      zeroline = FALSE))

fig1

But is there a way to plot them on the same graph? In the past, I have been able to plot two time series on the same graph as follows:

final_data$class = "series 1"
final_data2$class = "series 2"
data = rbind(final_data, final_data2)

data$class = as.factor(data$class)

a = data  %>% 

group_by(class) %>% 
plot_ly(x = ~ (date_decision_made)) %>% 
add_lines(y = ~ property_damages_in_dollars, 
          color = ~ factor(class)
)

In short, is it possible to combine the two original series:

 f1$class = "series 1"
    f2$class = "series 2"
    data = rbind(f1, f2)

    data$class = as.factor(data$class)

And now simultaneously plot both series from the "data" object on the same graph along with their confidence intervals?

Thanks

来源:https://stackoverflow.com/questions/65655864/r-plotting-multiple-confidence-intervals-on-the-same-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!