Skip to content

Commit 7ad95e1

Browse files
committed
Fixed subannual plots for fields with non-overlapping layers
This is a common use case when plotting layers which cover different time periods. See: MagicForrest/DGVMTools#97
1 parent ef42f2e commit 7ad95e1

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

R/ozflux-plotting-internal.r

+20-7
Original file line numberDiff line numberDiff line change
@@ -417,13 +417,26 @@ plot_pvo <- function(gc, ylim = NULL, text_multiplier = NULL, marker_size = 3) {
417417
plot_subannual <- function(gc, ylim = NULL, text_multiplier = NULL) {
418418
colours <- get_colour_palette(length(names(gc)))
419419
colours <- setNames(colours, names(gc))
420-
agg <- DGVMTools::aggregateYears(gc)
421-
# plt <- DGVMTools::plotSubannual(gc, col.by = "Layer"
422-
# , summary.function = mean
423-
# , title = NULL, subtitle = NULL, point.size = 0, cols = colours
424-
# , text.multiplier = text_multiplier, summary.only = TRUE
425-
# , summary.as.points = FALSE)
426-
df_long <- tidyr::pivot_longer(agg@data, cols = names(gc)
420+
421+
# Process each layer separately to handle NAs correctly
422+
agg_data <- NULL
423+
for (layer_name in names(gc)) {
424+
# Create a temporary gc with just this layer
425+
temp_gc <- DGVMTools::selectLayers(gc, layer_name)
426+
temp_gc@data <- temp_gc@data[!is.na(temp_gc@data[[layer_name]]), ]
427+
# Aggregate this layer
428+
agg_result <- DGVMTools::aggregateYears(temp_gc)
429+
# layer_results[[layer_name]] <- agg_result@data[[layer_name]]
430+
if (is.null(agg_data)) {
431+
agg_data <- agg_result
432+
} else {
433+
agg_data <- DGVMTools::copyLayers(agg_result, agg_data, layer_name)
434+
}
435+
}
436+
437+
# Combine results into a data frame
438+
# Create the plot using the combined data
439+
df_long <- tidyr::pivot_longer(agg_data@data, cols = names(gc)
427440
, names_to = "variable"
428441
, values_to = "value")
429442
plt <- ggplot2::ggplot(df_long, aes(x = Day, y = value, color = variable)) +

0 commit comments

Comments
 (0)