Skip to content

Commit 584e7af

Browse files
authored
Merge pull request #220 from HorridTom/refactor-tidyplot-64-65
Refactor tidyplot 64 65
2 parents ff729b6 + 0075581 commit 584e7af

8 files changed

Lines changed: 308 additions & 222 deletions

File tree

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: autospc
22
Title: Automatically Partitioned SPC Charts
3-
Version: 0.0.0.9040
3+
Version: 0.0.0.9041
44
Authors@R: c(
55
person("Thomas", "Woodcock", , "woodcock.thomas@gmail.com", role = c("aut", "cre"),
66
comment = c(ORCID = "0000-0002-4735-4856")),
@@ -14,6 +14,7 @@ Imports:
1414
DiagrammeR,
1515
dplyr,
1616
fpCompare,
17+
ggnewscale,
1718
ggplot2,
1819
lifecycle,
1920
magrittr,

R/add_annotation_data.R

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,17 @@ add_annotations_to_plot_pp <- function(p,
128128
if(annotation_arrows) {
129129

130130
p_annotated <- p + ggrepel::geom_text_repel(
131+
data = . %>% dplyr::filter(series %in% c("cl"),
132+
!is.na(annotation_level)),
131133
ggplot2::aes(x = x,
132-
y = cl,
134+
y = value,
133135
label = cl_label),
134-
position = ggpp::position_nudge_to(y = df %>%
135-
dplyr::filter(!is.na(y)) %>%
136-
dplyr::pull(annotation_level)),
136+
position = ggpp::position_nudge_to(
137+
y = df %>%
138+
dplyr::filter(series %in% c("cl"),
139+
!is.na(value),
140+
!is.na(annotation_level)) %>%
141+
dplyr::pull(annotation_level)),
137142
color = "grey40",
138143
size = annotation_size,
139144
fontface = "bold",
@@ -143,7 +148,9 @@ add_annotations_to_plot_pp <- function(p,
143148
hjust = 0,
144149
segment.size = 0.75,
145150
segment.curvature = df %>%
146-
dplyr::filter(!is.na(y)) %>%
151+
dplyr::filter(series %in% c("cl"),
152+
!is.na(value),
153+
!is.na(annotation_level)) %>%
147154
dplyr::pull(annotation_curvature),
148155
segment.ncp = 4,
149156
segment.inflect = FALSE,
@@ -153,12 +160,17 @@ add_annotations_to_plot_pp <- function(p,
153160
max.overlaps = Inf)
154161
} else {
155162
p_annotated <- p + ggrepel::geom_text_repel(
163+
data = . %>% dplyr::filter(series %in% c("cl"),
164+
!is.na(annotation_level)),
156165
ggplot2::aes(x = x,
157-
y = cl,
166+
y = value,
158167
label = cl_label),
159-
position = ggpp::position_nudge_to(y = df %>%
160-
dplyr::filter(!is.na(y)) %>%
161-
dplyr::pull(annotation_level)),
168+
position = ggpp::position_nudge_to(
169+
y = df %>%
170+
dplyr::filter(series %in% c("cl"),
171+
!is.na(value),
172+
!is.na(annotation_level)) %>%
173+
dplyr::pull(annotation_level)),
162174
color = "grey40",
163175
size = annotation_size,
164176
fontface = "bold",
@@ -184,14 +196,17 @@ add_annotations_to_plot_basic <- function(p,
184196
x_nudge <- x_range/25
185197

186198
p_annotated <- p +
187-
ggplot2::geom_text(mapping = ggplot2::aes(x = x,
188-
y = annotation_level,
189-
label = cl_label),
190-
nudge_x = x_nudge,
191-
na.rm = TRUE,
192-
color = "grey40",
193-
size = annotation_size,
194-
fontface = "bold")
199+
ggplot2::geom_text(
200+
data = . %>% dplyr::filter(series %in% c("cl"),
201+
!is.na(annotation_level)),
202+
mapping = ggplot2::aes(x = x,
203+
y = annotation_level,
204+
label = cl_label),
205+
nudge_x = x_nudge,
206+
na.rm = TRUE,
207+
color = "grey40",
208+
size = annotation_size,
209+
fontface = "bold")
195210

196211
return(p_annotated)
197212

R/add_floating_median.R

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Add floating median line to the plot p
2+
add_floating_median <- function(df,
3+
p,
4+
floating_median_n) {
5+
6+
p <- p +
7+
ggplot2::geom_line(data = df %>%
8+
dplyr::filter(series == "cl"),
9+
ggplot2::aes(x, median),
10+
linetype = "75551555",
11+
colour = "gray50",
12+
linewidth = 0.5,
13+
show.legend = TRUE,
14+
na.rm = TRUE) +
15+
ggplot2::annotate(
16+
"text",
17+
x = df %>%
18+
dplyr::filter(series == "cl") %>%
19+
dplyr::filter(dplyr::row_number() ==
20+
nrow(df %>%
21+
dplyr::filter(series == "cl")) -
22+
floating_median_n + 1L) %>%
23+
dplyr::pull(x),
24+
y = df %>%
25+
dplyr::filter(series == "cl") %>%
26+
dplyr::filter(dplyr::row_number() ==
27+
nrow(df %>%
28+
dplyr::filter(series == "cl")) -
29+
floating_median_n + 1L) %>%
30+
dplyr::pull(median)*0.95,
31+
label = "Median",
32+
size = 3,
33+
colour = "gray50",
34+
na.rm = TRUE)
35+
36+
return(p)
37+
38+
}

R/algorithm_helpers.R

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -481,39 +481,6 @@ floating_median_column <- function(df,
481481
}
482482

483483

484-
# Add floating median line to the plot p
485-
add_floating_median <- function(df,
486-
p,
487-
floating_median_n) {
488-
489-
p <- p +
490-
ggplot2::geom_line(data = df,
491-
ggplot2::aes(x, median),
492-
linetype = "75551555",
493-
colour = "gray50",
494-
linewidth = 0.5,
495-
show.legend = TRUE,
496-
na.rm = TRUE) +
497-
ggplot2::annotate(
498-
"text",
499-
x = df %>%
500-
dplyr::filter(dplyr::row_number() == nrow(df) -
501-
floating_median_n + 1L) %>%
502-
dplyr::pull(x),
503-
y = df %>%
504-
dplyr::filter(dplyr::row_number() == nrow(df) -
505-
floating_median_n + 1L) %>%
506-
dplyr::pull(median)*0.95,
507-
label = "Median",
508-
size = 3,
509-
colour = "gray50",
510-
na.rm = TRUE)
511-
512-
return(p)
513-
514-
}
515-
516-
517484
sign_chr <- function(x) {
518485
y <- dplyr::case_when(
519486
x < 0 ~ "01",

0 commit comments

Comments
 (0)