Skip to content

Commit 3643b2a

Browse files
committed
.
1 parent 8500f6c commit 3643b2a

14 files changed

+285
-136
lines changed

ChangeLog

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Version 0.13.0 (2024-09-024)
2+
* Add 'mode' parameter to 'relx' and 'rely'.
3+
* Suggest 'qs2' instead of 'qs'
4+
* Add 'light_cool_colors' theme
5+
Version 0.12.0 (2023-04-06)
6+
* Initial tracking

DESCRIPTION

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: glow
22
Title: Make Plots that Glow
3-
Version: 0.12.0
4-
Date: 2023-4-6
3+
Version: 0.13.0
4+
Date: 2024-09-24
55
Authors@R: c(
66
person("Travers", "Ching", email = "[email protected]", role = c("aut", "cre", "cph"))
77
)
@@ -17,8 +17,8 @@ Imports:
1717
Depends:
1818
ggplot2
1919
Suggests:
20-
knitr, rmarkdown, viridisLite, magick, EBImage, qs
20+
knitr, rmarkdown, viridisLite, magick, EBImage, qs2
2121
VignetteBuilder: knitr
22-
RoxygenNote: 7.1.2
22+
RoxygenNote: 7.3.2
2323
URL: https://github.com/traversc/glow
2424
BugReports: https://github.com/traversc/glow/issues

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ importFrom(grDevices, col2rgb, rgb, colorRampPalette, rainbow)
77
useDynLib(glow, .registration=TRUE)
88
export("theme_night",
99
"light_heat_colors",
10+
"light_cool_colors",
1011
"additive_alpha",
1112
"GlowMapper",
1213
"GlowMapper4",

R/glow_functions.r R/glow_pkg_functions.R

+40-45
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ light_heat_colors <- function(...) {
1818
colorRampPalette(c("red", "darkorange2", "darkgoldenrod1", "gold1", "yellow2"))(...)
1919
}
2020

21+
light_cool_colors <- function(...) {
22+
colorRampPalette(c("#1133AA", "#CCFFFF"))(...)
23+
}
24+
2125
additive_alpha <- function(colors) {
2226
s <- seq(2, length(colors))
2327
x <- t(col2rgb(colors, alpha=F))/255
@@ -44,14 +48,42 @@ map_colors <- function(colors, x, min_limit=NULL, max_limit=NULL) {
4448
colors[findInterval(x, seq(min_limit, max_limit, length.out = length(colors) + 1), all.inside = TRUE)]
4549
}
4650

47-
relx <- function(r) {
48-
structure(r, class = "relx")
51+
relx <- function(r, mode = "data") {
52+
structure(r, class = "relx", mode = mode)
4953
}
5054

51-
rely <- function(r) {
52-
structure(r, class = "rely")
55+
rely <- function(r, mode = "data") {
56+
structure(r, class = "rely", mode = mode)
5357
}
5458

59+
# internal helper function
60+
calculate_radius <- function(radius, xdiff, ydiff, self) {
61+
plot_x_diff <- self$xmax - self$xmin
62+
plot_y_diff <- self$ymax - self$ymin
63+
if(inherits(radius, "relx")) {
64+
rel_mode <- attr(radius, "mode")
65+
attributes(radius) <- NULL
66+
if(rel_mode == "data") {
67+
radius <- xdiff * radius / self$x_aspect_ratio
68+
} else if(rel_mode == "plot") {
69+
radius <- plot_x_diff * radius / self$x_aspect_ratio
70+
} else {
71+
stop("relx mode must be 'data' or 'plot'")
72+
}
73+
} else if(inherits(radius, "rely")) {
74+
rel_mode <- attr(radius, "mode")
75+
attributes(radius) <- NULL
76+
if(rel_mode == "data") {
77+
radius <- ydiff * radius / self$y_aspect_ratio
78+
} else if(rel_mode == "plot") {
79+
radius <- plot_y_diff * radius / self$y_aspect_ratio
80+
} else {
81+
stop("rely mode must be 'data' or 'plot'")
82+
}
83+
} else {
84+
radius
85+
}
86+
}
5587

5688
# GlowMapper ###################################
5789
GlowMapper <- R6Class("GlowMapper", list(
@@ -115,16 +147,7 @@ GlowMapper <- R6Class("GlowMapper", list(
115147
self$x_aspect_ratio <- max(xincrement / yincrement,1)
116148
self$y_aspect_ratio <- max(yincrement / xincrement,1)
117149

118-
if(inherits(radius, "relx")) {
119-
class(radius) <- NULL
120-
radius <- xdiff * radius / self$x_aspect_ratio
121-
} else if(inherits(radius, "rely")) {
122-
class(radius) <- NULL
123-
radius <- ydiff * radius / self$y_aspect_ratio
124-
} else {
125-
# nothing
126-
}
127-
class(radius) <- NULL
150+
radius <- calculate_radius(radius, xdiff, ydiff, self)
128151

129152
self$plot_data <- data.frame(x, y, intensity, radius, distance_exponent)
130153

@@ -275,17 +298,7 @@ GlowMapper4 <- R6Class("GlowMapper4", list(
275298
self$x_aspect_ratio <- max(xincrement / yincrement,1)
276299
self$y_aspect_ratio <- max(yincrement / xincrement,1)
277300

278-
279-
if(inherits(radius, "relx")) {
280-
class(radius) <- NULL
281-
radius <- xdiff * radius / self$x_aspect_ratio
282-
} else if(inherits(radius, "rely")) {
283-
class(radius) <- NULL
284-
radius <- ydiff * radius / self$y_aspect_ratio
285-
} else {
286-
# nothing
287-
}
288-
class(radius) <- NULL
301+
radius <- calculate_radius(radius, xdiff, ydiff, self)
289302

290303
self$plot_data <- data.frame(x, y, r,g,b, radius, distance_exponent)
291304

@@ -460,16 +473,7 @@ LightMapper <- R6Class("LightMapper", list(
460473
self$x_aspect_ratio <- max(xincrement / yincrement,1)
461474
self$y_aspect_ratio <- max(yincrement / xincrement,1)
462475

463-
if(inherits(radius, "relx")) {
464-
class(radius) <- NULL
465-
radius <- xdiff * radius / self$x_aspect_ratio
466-
} else if(inherits(radius, "rely")) {
467-
class(radius) <- NULL
468-
radius <- ydiff * radius / self$y_aspect_ratio
469-
} else {
470-
# nothing
471-
}
472-
class(radius) <- NULL
476+
radius <- calculate_radius(radius, xdiff, ydiff, self)
473477

474478
self$plot_data <- data.frame(x, y, intensity, radius, falloff_exponent, distance_exponent)
475479

@@ -615,16 +619,7 @@ LightMapper4 <- R6Class("GlowMapper4", list(
615619
self$x_aspect_ratio <- max(xincrement / yincrement,1)
616620
self$y_aspect_ratio <- max(yincrement / xincrement,1)
617621

618-
if(inherits(radius, "relx")) {
619-
class(radius) <- NULL
620-
radius <- xdiff * radius / self$x_aspect_ratio
621-
} else if(inherits(radius, "rely")) {
622-
class(radius) <- NULL
623-
radius <- ydiff * radius / self$y_aspect_ratio
624-
} else {
625-
# nothing
626-
}
627-
class(radius) <- NULL
622+
radius <- calculate_radius(radius, xdiff, ydiff, self)
628623

629624
self$plot_data <- data.frame(x, y, r,g,b, radius, distance_exponent, falloff_exponent)
630625

R/zz_help_files.R

+28-4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,25 @@ NULL
3535
#' @name light_heat_colors
3636
NULL
3737

38+
# light_cool_colors ###################################################
39+
40+
#' light_cool_colors
41+
#'
42+
#' A light color palette.
43+
#'
44+
#' @usage light_cool_colors(...)
45+
#' @param ... Arguments passed to the function returned by `colorRampPalette`
46+
#' @return A light color palette function
47+
#' @details A simple light color palette gradient from dark blue to light blue
48+
#' intended for a heatmap with a white or light color background.
49+
#'
50+
#' Equivalent to `colorRampPalette(c("#1133AA", "#CCFFFF"))(...)`.
51+
#' @examples
52+
#' light_colors <- light_cool_colors(144)
53+
#' plot(1:144, 1:144, col = light_colors, pch = 19)
54+
#' @name light_cool_colors
55+
NULL
56+
3857
# circular_palette ###################################################
3958

4059
#' circular_palette
@@ -94,9 +113,11 @@ NULL
94113
#'
95114
#' Helper functions for specifying the `radius` parameter in `GlowMapper$map` and similar.
96115
#'
97-
#' @usage relx(r)
98-
#' @aliases relx
99-
#' @param r Radius of point data relative to X or Y range of the plot, a value between 0 and 1.
116+
#' @usage relx(r, mode = "data")
117+
#' @usage rely(r, mode = "data")
118+
#' @param r Radius of point data relative to X or Y range of the plot, a value between 0 and 1.
119+
#' @param mode One of "data" (default) or "plot". Whether to use a radius relative to the extent
120+
#' of the data range or the plot range (which could have been adjusted manually).
100121
#' @return A class structure for input to `GlowMapper$map`
101122
#' @details
102123
#' Helper functions for specifying the `radius` parameter relative to the range of the plot as a proportion.
@@ -126,7 +147,10 @@ NULL
126147
#' @name relxy
127148
NULL
128149

129-
#' @usage rely(r)
150+
#' @aliases relx
151+
#' @name relxy
152+
NULL
153+
130154
#' @aliases rely
131155
#' @name relxy
132156
NULL

README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ ggplot() +
9595
![](vignettes/diamonds_vignette_dark.png "diamonds_vignette_dark.png")
9696

9797
``` r
98-
# light color theme
98+
# light "heat" color theme
9999
light_colors <- light_heat_colors(144)
100100
ggplot() +
101101
geom_raster(data = pd, aes(x = pd$x, y = pd$y, fill = pd$value), show.legend = FALSE) +
@@ -107,6 +107,19 @@ ggplot() +
107107

108108
![](vignettes/diamonds_vignette_light.png "diamonds_vignette_light.png")
109109

110+
``` r
111+
# light "cool" color theme
112+
light_colors <- light_cool_colors(144)
113+
ggplot() +
114+
geom_raster(data = pd, aes(x = pd$x, y = pd$y, fill = pd$value), show.legend = FALSE) +
115+
scale_fill_gradientn(colors = additive_alpha(light_colors)) +
116+
coord_fixed(gm$aspect(), xlim = gm$xlim(), ylim = gm$ylim()) +
117+
labs(x = "carat", y = "price") +
118+
theme_bw(base_size = 14)
119+
```
120+
121+
![](vignettes/diamonds_vignette_cool.png "diamonds_vignette_cool.png")
122+
110123
### Writing a raster image directly
111124

112125
Instead of using ggplot, you can also output a raster image directly

inst/examples/examples.R

+19-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
library(dplyr)
1515
library(data.table)
16-
library(qs)
16+
library(qs2)
1717

1818
library(glow)
1919
library(ggplot2)
@@ -59,7 +59,7 @@ output_width = 1920*4
5959
output_height = 1080*4
6060
outfile <- "plots/GAIA_galaxy_pseudocolor.png"
6161

62-
stars <- qread("plot_data/gaia_stars.qs")
62+
stars <- qs_read("plot_data/gaia_stars.qs2")
6363

6464
# Transform to galactic coordinates
6565
# https://gea.esac.esa.int/archive/documentation/GDR2/Data_processing/chap_cu3ast/sec_cu3ast_intro/ssec_cu3ast_intro_tansforms.html
@@ -116,7 +116,7 @@ writeImage(img, "plots/GAIA_galaxy_pseudocolor.png")
116116

117117
outfile <- "plots/US_coronavirus_2021.png"
118118

119-
cov_cases <- qread("plot_data/covid_confirmed_usafacts.qs")
119+
cov_cases <- qs_read("plot_data/covid_confirmed_usafacts.qs2")
120120
centroids <- cov_cases$centroids
121121
state <- cov_cases$state
122122
county <- cov_cases$county
@@ -280,9 +280,23 @@ outfile <- "plots/diamonds_vignette_light.png"
280280
ggsave(g, file=outfile, width=10, height=4, dpi=96)
281281
trim_image(outfile, "white")
282282

283+
# light color theme with cool colors
284+
light_colors <- colorRampPalette(c("#1133AA", "#CCFFFF"))(144)
285+
g <- ggplot() +
286+
geom_raster(data = pd, aes(x = pd$x, y = pd$y, fill = pd$value), show.legend = F) +
287+
scale_fill_gradientn(colors = additive_alpha(light_colors)) +
288+
coord_fixed(gm$aspect(), xlim = gm$xlim(), ylim = gm$ylim()) +
289+
labs(x = "carat", y = "price") +
290+
theme_bw(base_size = 14)
291+
292+
outfile <- "plots/diamonds_vignette_cool.png"
293+
ggsave(g, file=outfile, width=10, height=4, dpi=96)
294+
trim_image(outfile, "white")
295+
296+
283297
# Volcano ##########################################
284298

285-
DMPs <- qread("plot_data/methylation_data.qs")
299+
DMPs <- qs_read("plot_data/methylation_data.qs2")
286300

287301
adj_pval_threshold <- DMPs %>% filter(adj.P.Val < 0.05) %>%
288302
pull(P.Value) %>% max
@@ -308,7 +322,7 @@ trim_image(outfile, "white")
308322
# https://www.r-bloggers.com/visualize-large-data-sets-with-the-bigvis-package/
309323
## wget https://packages.revolutionanalytics.com/datasets/AirOnTime87to12/AirOnTimeCSV.zip .
310324

311-
air <- qread("plot_data/AirOnTime.qs", nthreads=nt)
325+
air <- qs_read("plot_data/AirOnTime.qs2", nthreads=nt)
312326

313327
temp <- rbindlist(air)
314328
qlo1 <- temp$ARR_DELAY %>% quantile(0.0025)

inst/examples/examples_parse_data.R

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ library(glow)
44
library(dplyr)
55
library(data.table)
66
library(arrow)
7-
library(qs)
7+
library(qs2)
88
library(stringr)
99

1010
# COVID example
@@ -41,7 +41,7 @@ stars <- lapply(files, function(f) {
4141
stars2 <- stars[complete.cases(stars),]
4242
stars2 <- stars2 %>% arrange_all
4343

44-
qsave(stars2, "plot_data/gaia_stars.qs", preset = "custom", algorithm = "zstd", compress_level = 22, nthreads = 8)
44+
qs_save(stars2, "plot_data/gaia_stars.qs2", preset = "custom", algorithm = "zstd", compress_level = 22, nthreads = 8)
4545

4646

4747
# COVID #######################################################################
@@ -61,7 +61,7 @@ centroids <- left_join(centroids, cov_cases, by = c("name", "state_abbr"))
6161
centroids$total[is.na(centroids$total)] <- 0
6262
centroids <- filter(centroids, total > 0)
6363

64-
qsave(list(centroids=centroids, county=county, state=state), "plot_data/covid_confirmed_usafacts.qs", preset = "custom", algorithm = "zstd", compress_level = 22)
64+
qs_save(list(centroids=centroids, county=county, state=state), "plot_data/covid_confirmed_usafacts.qs2", preset = "custom", algorithm = "zstd", compress_level = 22)
6565

6666

6767
# Volcano ##########################################
@@ -102,7 +102,7 @@ DMPs <- topTable(fit2, num=Inf, coef=1, genelist=ann450kSub)
102102
DMPs <- DMPs %>% dplyr::select(logFC, P.Value, adj.P.Val)
103103
rownames(DMPs) <- NULL
104104

105-
qsave(DMPs, file = "plot_data/methylation_data.qs", preset = "custom", algorithm = "zstd", compress_level = 22)
105+
qs_save(DMPs, file = "plot_data/methylation_data.qs2", preset = "custom", algorithm = "zstd", compress_level = 22)
106106

107107
# Airline ######################################################################
108108
# https://www.r-bloggers.com/visualize-large-data-sets-with-the-bigvis-package/
@@ -119,7 +119,7 @@ air <- lapply(1:length(files), function(i) {
119119
filter(complete.cases(.))
120120
return(z)
121121
})
122-
qsave(air, file="plot_data/AirOnTime.qs", preset = "custom", algorithm = "zstd", compress_level = 22, nthreads=nt)
122+
qs_save(air, file="plot_data/AirOnTime.qs2", preset = "custom", algorithm = "zstd", compress_level = 22, nthreads=nt)
123123

124124
# GPS traces ######################################################################
125125
# https://planet.osm.org/gps/
@@ -130,6 +130,6 @@ system(sprintf("unxz %s/simple-gps-points-120312.txt.xz", tempdir))
130130
gps <- data.table::fread(sprintf("%s/simple-gps-points-120312.txt", tempdir), header=FALSE, data.table=FALSE)
131131
gps <- list(latitude = gps[[1]], longitude = gps[[2]]) # Long dataframes not supported in R as of 4.2.3
132132

133-
qsave(gps, file="plot_data/simple-gps-points.qs", preset = "custom", algorithm = "zstd", compress_level = 22, nthreads=nt)
133+
qs_save(gps, file="plot_data/simple-gps-points.qs2", preset = "custom", algorithm = "zstd", compress_level = 22, nthreads=nt)
134134

135135

inst/examples/gps_example.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
library(qs)
1+
library(qs2)
22
library(glow)
33
library(EBImage)
44

@@ -10,7 +10,7 @@ output_height = 1080*4
1010
radius <- 0.2 # Increase this value if plotting fewer points
1111
intensity <- 0.6 # Increase this value if plotting fewer points
1212

13-
gps <- qread("plot_data/simple-gps-points.qs", nthreads=32)
13+
gps <- qs_read("plot_data/simple-gps-points.qs2", nthreads=32)
1414

1515
length(gps$latitude) # 2770233904
1616

0 commit comments

Comments
 (0)