Skip to content

Commit 2b31be2

Browse files
committed
lty legend fixed
1 parent 61b6923 commit 2b31be2

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

R/tmapLeafletComp.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ gp_to_lpar = function(gp, mfun, shape = 20, pick_middle = TRUE, size_factor = 20
2121
fill_alpha = {if (!all(is.na(gp$fill_alpha))) gp$fill_alpha else 0},
2222
col_alpha = {if (!all(is.na(gp$col_alpha))) gp$col_alpha else 0},
2323
lwd = {if (!all(is.na(gp$lwd))) gp$lwd else 0},
24-
lty = {if (!all(is.na(gp$lty))) lty2dash(gp$lty) else "none"},
24+
lty = {if (!all(is.na(gp$lty))) lty2dash(gp$lty, gp$lwd) else "none"},
2525
size = {if (!all(is.na(gp$size))) gp$size else 1},
2626
shape = {if (!all(is.na(gp$shape))) gp$shape else shape}))
2727

@@ -290,6 +290,7 @@ tmapLeaflet_legend = function(cmp, lf, o, orientation) {
290290
}
291291

292292
names(gp2)[names(gp2) == 'stroke-width'] = "strokeWidth"
293+
names(gp2)[names(gp2) == 'stroke-dash'] = "dashArray"
293294
gp2$baseSize = 20
294295
#symbols = do.call(leaflegend::makeSymbolIcons, gp2)#$iconUrl
295296

R/tmapLeafletDataPlot_lines.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ tmapLeafletDataPlot.tm_data_lines = function(a, shpTM, dt, pdt, popup.format, hd
5353
} else {
5454

5555
lf %>%
56-
leaflet::addPolylines(data = shp, layerId = idt, label = hdt, color = gp$col, opacity = gp$col_alpha, weight = gp$lwd, group = group, options = opt, dashArray = lty2dash(gp$lty), popup = popups) %>%
56+
leaflet::addPolylines(data = shp, layerId = idt, label = hdt, color = gp$col, opacity = gp$col_alpha, weight = gp$lwd, group = group, options = opt, dashArray = lty2dash(gp$lty, gp$lwd), popup = popups) %>%
5757
assign_lf(facet_row, facet_col, facet_page)
5858

5959
}

R/tmapLeaflet_layers.R

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,20 @@ tmapLeafletDataPlot.default = function(a, shpTM, dt, pdt, popup.format, hdt, idt
119119
NULL
120120
}
121121

122+
#preset = lapply(list(dashed = c(4, 4), dotted = c(1,3), dotdash = c(1, 3, 4, 3), longdash = c(7, 3), twodash = c(2, 2, 6, 2)), function(x) {
123+
#x * scale
124+
#})
125+
lty2dash = function(lty, lwd) {
126+
po(lty,lwd)
127+
k = max(length(lty), length(lwd))
128+
lty = rep(lty, length.out = k)
129+
lwd = rep(lwd, length.out = k)
130+
131+
122132

123-
lty2dash = function(lty) {
124133
tab = c(solid = "", dashed = "4 4", dotted = "1 3", dotdash = "1 3 4 3", longdash = "7 3", twodash = "2 2 6 2")
125134
are_words = (lty %in% names(tab))
126-
if (all(are_words)) {
135+
x = if (all(are_words)) {
127136
unname(tab[lty])
128137
} else {
129138
are_letters = (suppressWarnings(!is.na(as.numeric(lty))))
@@ -137,14 +146,25 @@ lty2dash = function(lty) {
137146
lty
138147
}
139148

149+
# multiply by lwd
150+
split_x <- strsplit(x, " ")
151+
152+
z = mapply(
153+
function(vals, mult) paste(as.numeric(vals) * mult, collapse = " "),
154+
split_x, lwd, USE.NAMES = FALSE
155+
)
156+
157+
z[x == ""] = "" # restore empties if needed
158+
z
140159
}
141160

142161
makeSymbolIcons2 = function (shape, color, fillColor = color, opacity, fillOpacity = opacity,
143-
strokeWidth = 1, width, height = width, ...)
162+
strokeWidth = 1, dashArray = "none", width, height = width, ...)
144163
{
145164
symbols <- Map(leaflegend::makeSymbol, shape = shape, width = width,
146165
height = height, color = color, fillColor = fillColor,
147166
opacity = opacity, fillOpacity = fillOpacity, `stroke-width` = strokeWidth,
167+
`stroke-dasharray` = dashArray,
148168
...)
149169
leaflet::icons(iconUrl = unname(symbols), iconAnchorX = width/2 + strokeWidth,
150170
iconAnchorY = height/2 + strokeWidth)

R/tmapScaleAsIs.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ tmapScaleAsIs = function(x1, scale, legend, chart, o, aes, layer, layer_args, so
3232
values = do.call(cfun, list(x = x2, pc = o$pc))
3333

3434
vneutral = if (is.na(scale$value.neutral)) {
35-
getAesOption("value.neutral", o, aes, layer)
35+
vna = getAesOption("value.neutral", o, aes, layer)
36+
if (is.na(vna)) {
37+
x1[1]
38+
} else vna
3639
} else {
3740
scale$value.neutral
3841
}

0 commit comments

Comments
 (0)