|
22 | 22 | #' |
23 | 23 |
|
24 | 24 | draw_circle <- function(col = "#00BFFF", use = "focus", max.circle) { |
25 | | - if (!requireNamespace("ggplot2", quietly = TRUE)) { |
26 | | - stop("ggplot2 needed for this function to work. Please install it.", call. = FALSE) |
27 | | - } |
28 | | - if (!use %in% c("focus", "cent")) { |
29 | | - stop("use must be one of 'focus' or 'cent'") |
30 | | - } |
31 | | - if (use == "focus" && missing(max.circle)) { |
32 | | - stop("max.circle missing. Should be set to the max distance from focal node.") |
| 25 | + if (!requireNamespace("ggplot2", quietly = TRUE)) { |
| 26 | + stop( |
| 27 | + "ggplot2 needed for this function to work. Please install it.", |
| 28 | + call. = FALSE |
| 29 | + ) |
| 30 | + } |
| 31 | + if (!use %in% c("focus", "cent")) { |
| 32 | + stop("use must be one of 'focus' or 'cent'") |
| 33 | + } |
| 34 | + if (use == "focus" && missing(max.circle)) { |
| 35 | + stop( |
| 36 | + "max.circle missing. Should be set to the max distance from focal node." |
| 37 | + ) |
| 38 | + } |
| 39 | + dat <- data.frame() |
| 40 | + if (use == "cent") { |
| 41 | + for (d in seq(0, 100, 20) * 2) { |
| 42 | + tmp <- as.data.frame(circleFun(center = c(0, 0), diameter = d)) |
| 43 | + tmp[["grp"]] <- d |
| 44 | + dat <- rbind(dat, tmp) |
33 | 45 | } |
34 | | - dat <- data.frame() |
35 | | - if (use == "cent") { |
36 | | - for (d in seq(0, 100, 20) * 2) { |
37 | | - tmp <- as.data.frame(circleFun(center = c(0, 0), diameter = d)) |
38 | | - tmp[["grp"]] <- d |
39 | | - dat <- rbind(dat, tmp) |
40 | | - } |
41 | | - } else if (use == "focus") { |
42 | | - for (d in 1:max.circle) { |
43 | | - tmp <- as.data.frame(circleFun(center = c(0, 0), diameter = 2 * d)) |
44 | | - tmp[["grp"]] <- d |
45 | | - dat <- rbind(dat, tmp) |
46 | | - } |
| 46 | + } else if (use == "focus") { |
| 47 | + for (d in 1:max.circle) { |
| 48 | + tmp <- as.data.frame(circleFun(center = c(0, 0), diameter = 2 * d)) |
| 49 | + tmp[["grp"]] <- d |
| 50 | + dat <- rbind(dat, tmp) |
47 | 51 | } |
48 | | - circs <- ggplot2::geom_path(data = dat, ggplot2::aes_(x = ~x, y = ~y, group = ~grp), col = col, alpha = 0.5) |
49 | | - return(circs) |
| 52 | + } |
| 53 | + circs <- ggplot2::geom_path( |
| 54 | + data = dat, |
| 55 | + ggplot2::aes_(x = ~x, y = ~y, group = ~grp), |
| 56 | + col = col, |
| 57 | + alpha = 0.5 |
| 58 | + ) |
| 59 | + return(circs) |
50 | 60 | } |
51 | 61 |
|
52 | 62 |
|
@@ -75,48 +85,68 @@ draw_circle <- function(col = "#00BFFF", use = "focus", max.circle) { |
75 | 85 | #' } |
76 | 86 | #' @export |
77 | 87 | #' |
78 | | -annotate_circle <- function(cent, col = "#00BFFF", format = "", pos = "top", text_size = 3) { |
79 | | - if (!requireNamespace("ggplot2", quietly = TRUE)) { |
80 | | - stop("ggplot2 needed for this function to work. Please install it.", call. = FALSE) |
81 | | - } |
82 | | - if (length(cent) == 1) { |
83 | | - cent <- seq(1, cent, 1) |
84 | | - dat_annot <- data.frame(y = seq(0, 100, 20), x = 0, val = interpolate_cent(cent, seq(0, 100, 20))) |
85 | | - dat_annot[["val"]] <- round(dat_annot[["val"]], 8) |
86 | | - } else { |
87 | | - dat_annot <- data.frame(y = 100 - seq(0, 100, 20), x = 0, val = interpolate_cent(cent, seq(0, 100, 20))) |
88 | | - dat_annot[["val"]] <- round(dat_annot[["val"]], 8) |
89 | | - } |
90 | | - vju <- 0 |
91 | | - if (format == "scientific") { |
92 | | - dat_annot[["val"]] <- format(dat_annot[["val"]], scientific = TRUE) |
93 | | - } |
94 | | - if (pos == "bottom") { |
95 | | - dat_annot[["y"]] <- -dat_annot[["y"]] |
96 | | - vju <- 1 |
97 | | - } |
98 | | - |
99 | | - circs <- ggplot2::geom_text( |
100 | | - data = dat_annot, ggplot2::aes_(x = ~x, y = ~y, label = ~val), |
101 | | - col = col, vjust = vju, size = text_size |
| 88 | +annotate_circle <- function( |
| 89 | + cent, |
| 90 | + col = "#00BFFF", |
| 91 | + format = "", |
| 92 | + pos = "top", |
| 93 | + text_size = 3 |
| 94 | +) { |
| 95 | + if (!requireNamespace("ggplot2", quietly = TRUE)) { |
| 96 | + stop( |
| 97 | + "ggplot2 needed for this function to work. Please install it.", |
| 98 | + call. = FALSE |
102 | 99 | ) |
103 | | - return(circs) |
| 100 | + } |
| 101 | + if (length(cent) == 1) { |
| 102 | + cent <- seq(1, cent, 1) |
| 103 | + dat_annot <- data.frame( |
| 104 | + y = seq(0, 100, 20), |
| 105 | + x = 0, |
| 106 | + val = interpolate_cent(cent, seq(0, 100, 20)) |
| 107 | + ) |
| 108 | + dat_annot[["val"]] <- round(dat_annot[["val"]], 8) |
| 109 | + } else { |
| 110 | + dat_annot <- data.frame( |
| 111 | + y = 100 - seq(0, 100, 20), |
| 112 | + x = 0, |
| 113 | + val = interpolate_cent(cent, seq(0, 100, 20)) |
| 114 | + ) |
| 115 | + dat_annot[["val"]] <- round(dat_annot[["val"]], 8) |
| 116 | + } |
| 117 | + vju <- 0 |
| 118 | + if (format == "scientific") { |
| 119 | + dat_annot[["val"]] <- format(dat_annot[["val"]], scientific = TRUE) |
| 120 | + } |
| 121 | + if (pos == "bottom") { |
| 122 | + dat_annot[["y"]] <- -dat_annot[["y"]] |
| 123 | + vju <- 1 |
| 124 | + } |
| 125 | + |
| 126 | + circs <- ggplot2::geom_text( |
| 127 | + data = dat_annot, |
| 128 | + ggplot2::aes_(x = ~x, y = ~y, label = ~val), |
| 129 | + col = col, |
| 130 | + vjust = vju, |
| 131 | + size = text_size |
| 132 | + ) |
| 133 | + return(circs) |
104 | 134 | } |
105 | 135 |
|
106 | 136 |
|
107 | 137 | # helper ----- |
108 | 138 | circleFun <- function(center = c(0, 0), diameter = 1, npoints = 100) { |
109 | | - r <- diameter / 2 |
110 | | - tt <- seq(0, 2 * pi, length.out = npoints) |
111 | | - xx <- center[1] + r * cos(tt) |
112 | | - yy <- center[2] + r * sin(tt) |
113 | | - return(data.frame(x = xx, y = yy)) |
| 139 | + r <- diameter / 2 |
| 140 | + tt <- seq(0, 2 * pi, length.out = npoints) |
| 141 | + xx <- center[1] + r * cos(tt) |
| 142 | + yy <- center[2] + r * sin(tt) |
| 143 | + return(data.frame(x = xx, y = yy)) |
114 | 144 | } |
115 | 145 |
|
116 | 146 | interpolate_cent <- function(cent, x) { |
117 | | - a <- min(cent) |
118 | | - b <- max(cent) |
119 | | - alpha <- 100 / (b - a) |
120 | | - beta <- -100 / (b - a) * a |
121 | | - (x - beta) / alpha |
| 147 | + a <- min(cent) |
| 148 | + b <- max(cent) |
| 149 | + alpha <- 100 / (b - a) |
| 150 | + beta <- -100 / (b - a) * a |
| 151 | + (x - beta) / alpha |
122 | 152 | } |
0 commit comments