|
| 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