Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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")),
Expand Down
52 changes: 52 additions & 0 deletions R/visualisation.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down Expand Up @@ -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)

}
15 changes: 9 additions & 6 deletions tests/testthat/test_linetypes.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@ 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)

rle_layer_1 <- rle(layer_1$linetype)
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"),
Expand Down