Skip to content

Commit e654852

Browse files
vandenmanJorisGoosen
authored andcommitted
implement R side for reference lines and points
1 parent 8e81aa9 commit e654852

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

R/plotEditing.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,23 @@ plotEditing <- function(graph, newOptions) {
104104
graph <- graph + internalUpdateAxis(currentAxis[["x"]], newOptions[["xAxis"]][["settings"]])
105105
graph <- graph + internalUpdateAxis(currentAxis[["y"]], newOptions[["yAxis"]][["settings"]])
106106

107+
# we technically don't need to store previousReferences atm but it could be useful
108+
previousReferences <- graph[["plot_env"]][[".____previousReferences____"]]
109+
if (length(previousReferences) > 0L) {
110+
currentNames <- vapply(graph$layers, \(l) {
111+
# either the hash, or ""
112+
hash <- attr(l, "hash") %||% ""
113+
}, character(1L))
114+
# newNames <- referencesToNames(newOptions[["references"]])
115+
# keep <- (currentNames %in% newNames) | (!nzchar(currentNames))
116+
keep <- !nzchar(currentNames)
117+
graph$layers <- graph$layers[keep]
118+
}
119+
if (length(newOptions[["references"]]) > 0L)
120+
graph <- graph + referencesToGeoms(newOptions[["references"]])
121+
122+
graph[["plot_env"]][[".____previousReferences____"]] <- newOptions[["references"]]
123+
107124
# 'remember' if an edited plot had options set to automatic or manual
108125
newOptions[["resetPlot"]] <- FALSE
109126
env <- list2env(list(oldOptions = origNewOptions), parent = emptyenv())

R/plotEditingReferenceLines.R

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
2+
referencesToGeoms <- function(references) {
3+
4+
# this is a bit ugly, but allows references to return multiple geoms
5+
result <- list()
6+
idx <- 1L
7+
for (i in seq_along(references)) {
8+
ref <- references[[i]]
9+
name <- referenceToName(ref)
10+
tmp <- referenceToGeom(ref)
11+
if (ggplot2::is_layer(tmp)) {
12+
attr(tmp, "hash") <- name
13+
result[[idx]] <- tmp
14+
names(result)[idx] <- name
15+
idx <- idx + 1L
16+
} else {
17+
ntmp <- length(tmp)
18+
fromTo <- idx:(idx + ntmp - 1L)
19+
for (j in seq_along(tmp)) {
20+
attr(tmp[[j]], "hash") <- name
21+
}
22+
result[fromTo] <- tmp
23+
names(result)[fromTo] <- name
24+
idx <- idx + ntmp
25+
}
26+
}
27+
28+
return(result)
29+
}
30+
31+
referenceToGeom <- function(references) {
32+
if (references[["point"]]) return(referenceToPoint(references))
33+
if (references[["horizontal"]]) return(referenceToHLine(references))
34+
return(referenceToVLine(references))
35+
}
36+
37+
referenceToHLine <- function(reference) {
38+
# ggplot2::geom_hline(yintercept = reference$y)
39+
df <- data.frame(x = I(.985), y = reference$y, text = reference$text)
40+
if (reference$text == "")
41+
return(ggplot2::geom_hline(data = df, mapping = aes(yintercept = y)))
42+
else return(list(
43+
ggplot2::geom_hline(data = df, mapping = aes(yintercept = y)),
44+
ggplot2::geom_label(data = df, mapping = aes(x = x, y = y, label = text), hjust = "inward")
45+
))
46+
}
47+
48+
referenceToVLine <- function(reference) {
49+
ggplot2::geom_vline(xintercept = reference$x)
50+
df <- data.frame(x = reference$x, y = I(.985), text = reference$text)
51+
if (reference$text == "")
52+
return(ggplot2::geom_vline(data = df, mapping = aes(xintercept = x)))
53+
else return(list(
54+
ggplot2::geom_vline(data = df, mapping = aes(xintercept = x)),
55+
ggplot2::geom_label(data = df, mapping = aes(x = x, y = y, label = text), hjust = "inward", angle = 90)
56+
))
57+
}
58+
59+
referenceToPoint <- function(reference) {
60+
df <- data.frame(x = reference$x, y = reference$y, text = reference$text)
61+
if (reference$text == "")
62+
return(ggplot2::geom_point(data = df, mapping = aes(x = x, y = y)))
63+
else return(list(
64+
ggplot2::geom_point(data = df, mapping = aes(x = x, y = y)),
65+
ggplot2::geom_text(data = df, mapping = aes(x = x, y = y, label = text), hjust = 0, nudge_x = 0.05)
66+
))
67+
}
68+
69+
referencesToNames <- function(references) {
70+
vapply(references, referenceToName, character(1L))
71+
}
72+
73+
referenceToName <- function(ref) {
74+
type <- if (ref[["point"]]) {
75+
"point"
76+
} else if (ref[["horizontal"]]) {
77+
"hline"
78+
} else {
79+
"vline"
80+
}
81+
return(paste0("jasp_ref_", type, "_", rlang::hash(ref)))
82+
}

0 commit comments

Comments
 (0)