Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 1 | #' Generate viridis Color code for continuous values |
| 2 | #' |
| 3 | #' @inheritParams viridisLite::viridis |
| 4 | #' @param x continuous vectors of values |
| 5 | #' @param na_color color code for NA values |
| 6 | #' @export |
| 7 | spec_color <- function(x, alpha = 1, begin = 0, end = 1, |
| 8 | direction = 1, option = "D", |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 9 | na_color = "#BBBBBB") { |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 10 | x <- round(rescale(x, c(1, 256))) |
| 11 | color_code <- viridisLite::viridis(256, alpha, begin, end, direction, option)[x] |
| 12 | color_code[is.na(color_code)] <- na_color |
| 13 | return(color_code) |
| 14 | } |
| 15 | |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 16 | html_color_ <- function(color) { |
| 17 | if (substr(color, 1, 1) != "#" | nchar(color) != 9) return(color) |
| 18 | rgba_code <- col2rgb(color, alpha = TRUE) |
| 19 | rgba_code[4] <- round(rgba_code[4] / 255, 2) |
| 20 | return(paste0("rgba(", paste(rgba_code, collapse = ", "), ")")) |
| 21 | } |
| 22 | |
| 23 | html_color <- function(colors) { |
| 24 | sapply(colors, html_color_) |
| 25 | } |
| 26 | |
| 27 | latex_color_ <- function(color) { |
| 28 | if (substr(color, 1, 1) != "#") { |
| 29 | return(paste0("{", color, "}")) |
| 30 | } else { |
| 31 | color <- sub("#", "", color) |
| 32 | if (nchar(color) == 8) color <- substr(color, 1, 6) |
| 33 | return(paste0("[HTML]{", color, "}")) |
| 34 | } |
| 35 | } |
| 36 | latex_color <- function(colors) { |
| 37 | sapply(colors, latex_color_) |
| 38 | } |
| 39 | |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 40 | #' Generate common font size for continuous values |
| 41 | #' |
| 42 | #' @param x continuous vectors of values |
| 43 | #' @param begin Smalles font size to be used. Default is 10. |
| 44 | #' @param end Largest font size. Default is 20. |
| 45 | #' @param na_font_size font size for NA values |
| 46 | #' @export |
| 47 | spec_font_size <- function(x, begin = 10, end = 20, na_font_size = "inherit") { |
| 48 | x <- round(rescale(x, c(begin, end))) |
| 49 | x[is.na(x)] <- na_font_size |
| 50 | return(x) |
| 51 | } |
| 52 | |
| 53 | #' Generate rotation angle for continuous values |
| 54 | #' |
| 55 | #' @param x continuous vectors of values |
| 56 | #' @param begin Smallest degree to rotate. Default is 0 |
| 57 | #' @param end Largest degree to rotate. Default is 359. |
| 58 | #' @export |
| 59 | spec_angle <- function(x) { |
| 60 | x <- round(rescale(x, c(0, 359))) |
| 61 | x[is.na(x)] <- 0 |
| 62 | return(x) |
| 63 | } |