Skip to content

Commit 4ba73e6

Browse files
committed
feat: add alpha (transparency) for all types in mf_map() and mf_raster()
fix #78
1 parent 7f6ec34 commit 4ba73e6

29 files changed

+219
-101
lines changed

R/mf_base.R

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
#' @eval my_params(c(
66
#' 'col',
77
#' 'border',
8-
#' 'lwd', 'pch',
8+
#' 'lwd',
9+
#' 'pch',
10+
#' 'alpha',
911
#' 'add'))
1012
#' @param cex point size
1113
#' @param bg background color
@@ -23,6 +25,7 @@
2325
mf_base <- function(x,
2426
col = "grey80",
2527
border = "grey20",
28+
alpha = NULL,
2629
bg = "white",
2730
cex = 1,
2831
pch = 20,
@@ -40,16 +43,54 @@ mf_base <- function(x,
4043
}
4144

4245
xtype <- get_geom_type(x)
43-
if (xtype != "POLYGON" && missing(col)) {
44-
col <- "grey20"
46+
47+
if (xtype == "LINE") {
48+
if (missing(col)) {
49+
col <- "grey20"
50+
}
51+
if (!is.null(alpha)) {
52+
col <- get_hex_pal(col, alpha)
53+
}
54+
plot(
55+
st_geometry(x),
56+
col = col, lwd = lwd, lty = lty,
57+
add = TRUE, ...
58+
)
59+
}
60+
61+
if (xtype == "POLYGON") {
62+
if (!is.null(alpha)) {
63+
col <- get_hex_pal(col, alpha)
64+
}
65+
plot(
66+
st_geometry(x),
67+
col = col, border = border, lwd = lwd, lty = lty,
68+
add = TRUE, ...
69+
)
4570
}
4671

47-
plot(st_geometry(x),
48-
col = col, border = border,
49-
lwd = lwd, add = add, pch = pch,
50-
bg = bg, lty = lty, cex = cex,
51-
...
52-
)
72+
if (xtype == "POINT") {
73+
if (missing(col)) {
74+
col <- "grey20"
75+
}
76+
if (!is.null(alpha)) {
77+
col <- get_hex_pal(col, alpha)
78+
}
79+
if (pch %in% 21:25) {
80+
if (missing(border)) {
81+
border <- "grey80"
82+
}
83+
mycolspt <- border
84+
} else {
85+
mycolspt <- col
86+
}
87+
mycolsptbg <- col
88+
plot(
89+
st_geometry(x),
90+
col = mycolspt, bg = mycolsptbg, cex = cex, pch = pch,
91+
lwd = lwd, add = TRUE, ...
92+
)
93+
}
5394

5495
return(invisible(x))
5596
}

R/mf_choro.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
#' )
5858
mf_choro <- function(x, var,
5959
pal = "Mint",
60-
alpha = 1,
60+
alpha = NULL,
6161
rev = FALSE,
6262
breaks = "quantile",
6363
nbreaks,

R/mf_doc_utils.R

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,10 @@ my_params <- function(x) {
5454
"whether the ordering of the colors should be reversed (TRUE)",
5555
" or not (FALSE)"
5656
),
57-
alpha = paste0(
58-
"alpha if \\code{pal} is a \\link{hcl.colors} palette name, ",
59-
"the alpha-transparency level in the range [0,1]"
60-
),
57+
alpha = "alpha opacity, in the range [0,1]",
6158
col_na = "col_na color for missing values",
62-
cex_na = "cex_na cex (point size) for NA values",
63-
pch_na = "pch_na pch (point type) for NA values",
59+
cex_na = "cex_na point size for NA values",
60+
pch_na = "pch_na point type for NA values",
6461
val_max = "val_max maximum value used for proportional symbols",
6562
breaks = paste0(
6663
"breaks either a numeric vector with the actual breaks, ",

R/mf_get_pal.R

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,17 @@
44
#' Diverging color palettes can be dissymmetric (different number of colors in
55
#' each of the two gradients).
66
#' @name mf_get_pal
7-
#' @param n the number of colors (>= 1) to be in the palette.
8-
#' @param palette a valid palette name (one of hcl.pals()). The name is matched
9-
#' to
10-
#' the list of available palettes, ignoring upper vs. lower case, spaces,
11-
#' dashes,
12-
#' etc. in the matching.
7+
#' @param n the number of colors (>= 1) to be in the palette
8+
#' @param palette a valid palette name. See \link{hcl.pals} to get available
9+
#' palette names. The name is matched
10+
#' to the list of available palettes, ignoring upper vs. lower case, spaces,
11+
#' dashes, etc. in the matching.
1312
#' @param alpha an alpha-transparency level in the range [0,1] (0 means
14-
#' transparent and 1 means opaque), see argument alpha in hsv and hcl,
15-
#' respectively.
13+
#' transparent and 1 means opaque)
1614
#' @param rev logical indicating whether the ordering of the colors should be
17-
#' reversed.
15+
#' reversed
1816
#' @param neutral a color, if two gradients are used, the 'neutral' color can be
19-
#' added between them.
20-
#' @details See \link{hcl.pals} to get available palette names.
21-
#' If two gradients are used, the 'neutral' color can be added between them.
22-
#'
17+
#' added between them
2318
#' @return A vector of colors.
2419
#' @importFrom grDevices hcl.colors
2520
#' @export

R/mf_grad.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#' 'pch',
1010
#' 'add' ,
1111
#' 'col',
12+
#' 'alpha',
1213
#' 'leg_pos',
1314
#' 'leg_title',
1415
#' 'leg_title_cex',
@@ -44,6 +45,7 @@ mf_grad <- function(x,
4445
breaks = "quantile",
4546
nbreaks = 3,
4647
col = "tomato4",
48+
alpha = NULL,
4749
border = getOption("mapsf.fg"),
4850
pch = 21,
4951
cex,
@@ -67,6 +69,10 @@ mf_grad <- function(x,
6769
on.exit(par(op))
6870
xout <- x
6971

72+
if (!is.null(alpha)) {
73+
col <- get_hex_pal(col, alpha)
74+
}
75+
7076
# data prep
7177
x <- x[!is.na(x = x[[var]]), ]
7278
x <- x[order(x[[var]], decreasing = TRUE), ]

R/mf_map.R

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@
2929
#' ## Relevant arguments and default values for each map types:
3030
#' **base**: displays sf objects geometries.
3131
#' \preformatted{
32-
#' mf_map(x, col = "grey80", pch = 20, cex = 1, border = "grey20", lwd = 0.7,
33-
#' expandBB, add = FALSE, ...)
32+
#' mf_map(x, col = "grey80", pch = 20, cex = 1, border = "grey20",
33+
#' lwd = 0.7, alpha = NULL, expandBB, add = FALSE, ...)
3434
#' }
3535
#'
3636
#' **prop**: displays symbols with areas proportional to a quantitative
3737
#' variable (stocks). `inches` is used to set symbols sizes.
3838
#' \preformatted{
3939
#' mf_map(x, var, type = "prop", inches = 0.3, val_max, symbol = "circle",
40-
#' col = "tomato4", lwd_max = 20, border = getOption("mapsf.fg"),
41-
#' lwd = 0.7, expandBB, add = TRUE,
40+
#' col = "tomato4", alpha = NULL, lwd_max = 20,
41+
#' border = getOption("mapsf.fg"), lwd = 0.7, expandBB, add = TRUE,
4242
#' leg_pos = mf_get_leg_pos(x), leg_title = var,
4343
#' leg_title_cex = 0.8, leg_val_cex = 0.6, leg_val_rnd = 0,
4444
#' leg_frame = FALSE, leg_frame_border = getOption("mapsf.fg"),
@@ -54,7 +54,7 @@
5454
#' can use palette names from `hcl.pals()`.
5555
#' \preformatted{
5656
#' mf_map(x, var, type = "choro", breaks = "quantile", nbreaks, pal = "Mint",
57-
#' alpha = 1, rev = FALSE, pch = 21, cex = 1,
57+
#' alpha = NULL, rev = FALSE, pch = 21, cex = 1,
5858
#' border = getOption("mapsf.fg"), lwd = 0.7, col_na = "white",
5959
#' cex_na = 1, pch_na = 4, expandBB, add = FALSE,
6060
#' leg_pos = mf_get_leg_pos(x), leg_title = var, leg_title_cex = 0.8,
@@ -68,7 +68,7 @@
6868
#' **typo**: displays a typology map of a qualitative variable.
6969
#' `val_order` is used to set modalities order in the legend.
7070
#' \preformatted{
71-
#' mf_map(x, var, type = "typo", pal = "Dynamic", alpha = 1, rev = FALSE,
71+
#' mf_map(x, var, type = "typo", pal = "Dynamic", alpha = NULL, rev = FALSE,
7272
#' val_order,border = getOption("mapsf.fg"), pch = 21, cex = 1,
7373
#' lwd = 0.7, cex_na = 1, pch_na = 4, col_na = "white",
7474
#' leg_pos = mf_get_leg_pos(x), leg_title = var, leg_title_cex = 0.8,
@@ -82,7 +82,7 @@
8282
#' **symb**: displays the different modalities of a qualitative variable as
8383
#' symbols.
8484
#' \preformatted{
85-
#' mf_map(x, var, type = "symb", pal = "Dynamic", alpha = 1, rev = FALSE,
85+
#' mf_map(x, var, type = "symb", pal = "Dynamic", alpha = NULL, rev = FALSE,
8686
#' border = getOption("mapsf.fg"), pch, cex = 1, lwd = 0.7,
8787
#' col_na = "grey", pch_na = 4, cex_na = 1, val_order,
8888
#' leg_pos = mf_get_leg_pos(x), leg_title = var, leg_title_cex = 0.8,
@@ -96,7 +96,7 @@
9696
#' `breaks` and `nbreaks`. Symbol sizes are set with `cex`.
9797
#' \preformatted{
9898
#' mf_map(x, var, type = "grad", breaks = "quantile", nbreaks = 3, col = "tomato4",
99-
#' border = getOption("mapsf.fg"), pch = 21, cex, lwd,
99+
#' alpha = NULL, border = getOption("mapsf.fg"), pch = 21, cex, lwd,
100100
#' leg_pos = mf_get_leg_pos(x), leg_title = var, leg_title_cex = 0.8,
101101
#' leg_val_cex = 0.6, leg_val_rnd = 2, leg_frame = FALSE,
102102
#' leg_adj = c(0, 0), leg_size = 1, leg_border = border,
@@ -110,7 +110,7 @@
110110
#' quantitative variable.
111111
#' \preformatted{
112112
#' mf_map(x, var, type = "prop_choro", inches = 0.3, val_max, symbol = "circle",
113-
#' pal = "Mint", alpha = 1, rev = FALSE, breaks = "quantile", nbreaks,
113+
#' pal = "Mint", alpha = NULL, rev = FALSE, breaks = "quantile", nbreaks,
114114
#' border = getOption("mapsf.fg"), lwd = 0.7, col_na = "white",
115115
#' leg_pos = mf_get_leg_pos(x, 1), leg_title = var,
116116
#' leg_title_cex = c(0.8, 0.8), leg_val_cex = c(0.6, 0.6),
@@ -127,7 +127,7 @@
127127
#' variable.
128128
#' \preformatted{
129129
#' mf_map(x, var, type = "prop_typo", inches = 0.3, val_max, symbol = "circle",
130-
#' pal = "Dynamic", alpha = 1, rev = FALSE, val_order,
130+
#' pal = "Dynamic", alpha = NULL, rev = FALSE, val_order,
131131
#' border = getOption("mapsf.fg"), lwd = 0.7, lwd_max = 15,
132132
#' col_na = "white",
133133
#' leg_pos = mf_get_leg_pos(x, 1), leg_title = var,
@@ -144,7 +144,7 @@
144144
#' variable as symbols colored to reflect the classification of a second
145145
#' quantitative variable.
146146
#' \preformatted{
147-
#' mf_map(x, var, type = "symb_choro", pal = "Mint", alpha = 1, rev = FALSE,
147+
#' mf_map(x, var, type = "symb_choro", pal = "Mint", alpha = NULL, rev = FALSE,
148148
#' breaks = "quantile", nbreaks, border = getOption("mapsf.fg"),
149149
#' pch, cex = 1, lwd = 0.7, pch_na = 4, cex_na = 1, col_na = "white",
150150
#' val_order,

R/mf_map_utils.R

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,46 @@
55
#' @param alpha alpha
66
#' @noRd
77
#' @importFrom grDevices hcl.pals hcl.colors
8-
get_the_pal <- function(pal, nbreaks, alpha = 1, rev = TRUE) {
8+
get_the_pal <- function(pal, nbreaks, alpha, rev = TRUE) {
99
if (length(pal) == 1) {
1010
if (pal %in% hcl.pals()) {
11-
cols <- hcl.colors(n = nbreaks, palette = pal, alpha = alpha, rev = rev)
11+
cols <- hcl.colors(n = nbreaks, palette = pal, rev = rev)
1212
} else {
1313
cols <- rep(pal, nbreaks)
1414
}
1515
} else {
1616
cols <- pal[1:nbreaks]
1717
}
18+
if (!is.null(alpha)) {
19+
cols <- get_hex_pal(cols, alpha)
20+
}
21+
1822
return(cols)
1923
}
2024

25+
get_hex_pal <- function(pal, alpha) {
26+
pal <- grDevices::col2rgb(pal, alpha = FALSE)
27+
ffun <- function(x) {
28+
grDevices::rgb(pal[1, x],
29+
pal[2, x],
30+
pal[3, x],
31+
maxColorValue = 255
32+
)
33+
}
34+
paste0(sapply(seq_len(ncol(pal)), ffun), get_alpha(alpha))
35+
}
36+
37+
get_alpha <- function(alpha) {
38+
if (alpha < 0) {
39+
alpha <- 0
40+
}
41+
if (alpha > 1) {
42+
alpha <- 1
43+
}
44+
sprintf("%02X", as.integer(255.999 * alpha))
45+
}
46+
47+
2148
get_col_vec <- function(x, breaks, pal, jen = FALSE) {
2249
if (jen) {
2350
itv <- apply(array(apply(outer(x, breaks, ">"), 1, sum)), 1, max, 1)

R/mf_prop.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#' 'x',
55
#' 'var',
66
#' 'col',
7+
#' 'alpha',
78
#' 'border',
89
#' 'lwd',
910
#' 'add' ,
@@ -48,6 +49,7 @@ mf_prop <- function(x,
4849
lwd_max = 20,
4950
symbol = "circle",
5051
col = "tomato4",
52+
alpha = NULL,
5153
border = getOption("mapsf.fg"),
5254
lwd = .7,
5355
leg_pos = mf_get_leg_pos(x),
@@ -67,6 +69,10 @@ mf_prop <- function(x,
6769
op <- par(mar = getOption("mapsf.mar"), no.readonly = TRUE)
6870
on.exit(par(op))
6971

72+
if (!is.null(alpha)) {
73+
col <- get_hex_pal(col, alpha)
74+
}
75+
7076
xtype <- get_geom_type(x)
7177
# linestring special case
7278
if (xtype == "LINE") {

R/mf_prop_choro.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ mf_prop_choro <- function(x,
6868
val_max,
6969
symbol = "circle",
7070
pal = "Mint",
71-
alpha = 1,
71+
alpha = NULL,
7272
rev = FALSE,
7373
breaks = "quantile",
7474
nbreaks,

R/mf_prop_typo.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ mf_prop_typo <- function(x, var,
5454
val_max,
5555
symbol = "circle",
5656
pal = "Dynamic",
57-
alpha = 1,
57+
alpha = NULL,
5858
rev = FALSE,
5959
val_order,
6060
border = getOption("mapsf.fg"),

0 commit comments

Comments
 (0)