| | render_time_series_plot <- function(data, station_id, month, y_label = "Temperature (°C)", title_prefix = "Daily Mean Temperature") { |
| | |
| | |
| | |
| | |
| | |
| |
|
| | station_name <- NA |
| | if (station_id %in% tavg_meta$ID) { |
| | station_name <- tavg_meta$NAME[tavg_meta$ID == station_id] |
| | } else if (exists("prec_meta") && station_id %in% prec_meta$ID) { |
| | station_name <- prec_meta$NAME[prec_meta$ID == station_id] |
| | } |
| |
|
| | if (is.na(station_name)) { |
| | |
| | |
| | station_name <- "Unknown Station" |
| | } |
| |
|
| | |
| | linear_model <- lm(VALUE ~ YEAR, data = data) |
| | slope <- coef(linear_model)["YEAR"] |
| |
|
| | |
| | title_text <- paste( |
| | month, min(data$YEAR), max(data$YEAR), "-", station_name, station_id |
| | ) |
| |
|
| | |
| | |
| | line_color <- if (grepl("Precipitation", y_label)) "blue" else "red" |
| |
|
| | |
| | plot <- plot_ly(data, |
| | x = ~YEAR, y = ~VALUE, type = "scatter", mode = "lines", |
| | name = "Value", |
| | line = list(color = line_color) |
| | ) %>% |
| | layout( |
| | title = list( |
| | text = title_text, |
| | x = 0, |
| | y = 0.99, |
| | xanchor = "left", |
| | font = list(size = 12) |
| | ), |
| | xaxis = list( |
| | zeroline = FALSE, |
| | gridcolor = "lightgray", |
| | title = "", |
| | fixedrange = TRUE |
| | ), |
| | yaxis = list( |
| | title = list(text = paste(month, y_label), font = list(size = 10)), |
| | zeroline = FALSE, |
| | gridcolor = "lightgray", |
| | fixedrange = TRUE |
| | ), |
| | showlegend = FALSE, |
| | plot_bgcolor = "rgba(255, 255, 255, 0)", |
| | paper_bgcolor = "rgba(255, 255, 255, 0)", |
| | margin = list(l = 20, r = 5, t = 20, b = 3), |
| | shapes = list( |
| | list( |
| | type = "rect", |
| | x0 = 0, x1 = 1, y0 = 0, y1 = 1, |
| | xref = "paper", yref = "paper", |
| | fillcolor = "rgba(255, 255, 255, 0)", |
| | line = list(width = 0) |
| | ) |
| | ), |
| | hovermode = "x unified", |
| | hoverlabel = list( |
| | bgcolor = "white", |
| | bordercolor = "rgba(255, 255, 255, 0)" |
| | ), |
| | annotations = list( |
| | list( |
| | x = 0.5, |
| | y = -0.12, |
| | xref = "paper", |
| | yref = "paper", |
| | showarrow = FALSE, |
| | text = paste("Slope:", round(slope, 3), units = strsplit(y_label, " ")[[1]][2], "/year | Mean: ", round(mean(data$VALUE, na.rm = TRUE), 1), strsplit(y_label, " ")[[1]][2]), |
| | xanchor = "center", |
| | yanchor = "top", |
| | font = list(size = 11, color = "black") |
| | ) |
| | ) |
| | ) %>% |
| | add_trace( |
| | x = ~YEAR, |
| | y = fitted(linear_model), |
| | mode = "lines", |
| | name = "Linear Trend", |
| | line = list(color = "gray"), |
| | hoverinfo = "x+y" |
| | ) |
| |
|
| |
|
| | |
| | plot <- plot %>% |
| | config( |
| | modeBarButtonsToRemove = list( |
| | "zoom2d", "pan2d", "select2d", "lasso2d", "zoomIn2d", "zoomOut2d", "autoScale2d", "resetScale2d" |
| | ), |
| | displaylogo = FALSE, |
| | toImageButtonOptions = list( |
| | format = "png", |
| | filename = paste0(station_id, "_", month, "_time_series") |
| | ) |
| | ) |
| |
|
| | return(plot) |
| | } |
| |
|