diff --git a/DESCRIPTION b/DESCRIPTION index 51dacdc..92c1603 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: autospc Title: Automatically Partitioned SPC Charts -Version: 0.0.0.9041 +Version: 0.0.0.9044 Authors@R: c( person("Thomas", "Woodcock", , "woodcock.thomas@gmail.com", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-4735-4856")), diff --git a/R/visualisation.R b/R/visualisation.R index 65aad81..8b83167 100644 --- a/R/visualisation.R +++ b/R/visualisation.R @@ -40,6 +40,8 @@ create_spc_plot <- function(df, value, everything()) + df_long <- add_limit_connectors(df_long) + # Create initial plot object without formatting pct <- ggplot2::ggplot(df_long %>% dplyr::filter(!is.na(value)), @@ -345,3 +347,53 @@ format_x_axis <- function(p, return(p) } + + +add_limit_connectors <- function(df_long) { + + x_sequence <- df_long %>% + dplyr::distinct(x) %>% + dplyr::arrange(x) %>% + dplyr::pull(x) + + # Dataframe listing each display period in the data, with information on + # first x value in period, and previous x value to that, along with series + # values for that previous point + display_periods <- df_long %>% + dplyr::filter(periodType == "display") %>% + dplyr::distinct(plotPeriod, + x) %>% + dplyr::group_by(plotPeriod) %>% + dplyr::summarise(x = dplyr::first(x)) %>% + dplyr::rowwise() %>% + dplyr::mutate(prev_x = x_sequence[which(x_sequence == x) - 1L]) %>% + dplyr::ungroup() %>% + dplyr::left_join(df_long %>% + dplyr::distinct(x, series, value) %>% + dplyr::rename(prev_value = value), + by = c("prev_x" = "x")) + + # Create additional rows to be added into df_long with series values at the + # point immediately before the start of each display period. This has the + # effect of creating an additional point for the control limits and centre + # line to connect with the preceding calculation period limits and centre line + display_starts <- df_long %>% + dplyr::inner_join(display_periods %>% + dplyr::select(-plotPeriod), + by = c("x" = "x", + "series" = "series")) %>% + dplyr::filter(series %in% c("cl", "ucl", "lcl")) %>% + dplyr::mutate(x = prev_x, + value = prev_value) %>% + dplyr::select(-prev_x, + -prev_value) + + # Add the extra rows into the data, and sort into x order + df_long <- df_long %>% + dplyr::bind_rows(display_starts) %>% + dplyr::arrange(x, + series) + + return(df_long) + +} diff --git a/tests/testthat/test_linetypes.R b/tests/testthat/test_linetypes.R index e351ace..d53f49e 100644 --- a/tests/testthat/test_linetypes.R +++ b/tests/testthat/test_linetypes.R @@ -6,12 +6,15 @@ test_that("Linetypes are formed correctly", { chart_type = "C'") # layer 1 holds the centre line and control limits - layer_1 <- ggplot2::layer_data(test_plt, 1) %>% dplyr::arrange(x) + layer_1 <- ggplot2::layer_data(test_plt, 1) %>% + dplyr::arrange(x) # layer 2 holds the data series - layer_2 <- ggplot2::layer_data(test_plt, 2) %>% dplyr::arrange(x) + layer_2 <- ggplot2::layer_data(test_plt, 2) %>% + dplyr::arrange(x) - # layer 1 has 3n rows, as it holds three series - expect_equal(nrow(layer_1), 450L) + # layer 1 has 3n rows, as it holds three series; then an additional 3*3 = 9 + # rows for the additional limit connectors added for continuity of limit lines + expect_equal(nrow(layer_1), 450L + 9L) # layer 2 has n rows, as it holds one series expect_equal(nrow(layer_2), 150L) @@ -19,9 +22,9 @@ test_that("Linetypes are formed correctly", { rle_layer_2 <- rle(layer_2$linetype) correct_answer_1 <- structure(list(lengths = rep(c(1L, 2L), - 150), + 153), values = rep(c("solid", "42"), - 150)), + 153)), class = "rle") correct_answer_2 <- structure(list(lengths = 150L, values = "solid"),