Skip to content

Commit

Permalink
removed icon borders, fixed icon legend sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
mtennekes committed Jun 26, 2024
1 parent 5c921cd commit 7353784
Show file tree
Hide file tree
Showing 16 changed files with 150 additions and 31 deletions.
7 changes: 7 additions & 0 deletions R/step1_helper_facets.R
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ step1_rearrange_facets = function(tmo, o) {
}
if (length(popup.vars)) add_used_vars(popup.vars)


if (is.na(hover)) {
hover = id
} else if (is.logical(hover)) {
hover = ifelse(hover, id, "")
}

if (hover != "" && !hover %in% smeta$vars) rlang::arg_match0(hover, smeta$vars, "hover label", error_call = NULL)
if (hover != "") add_used_vars(hover)
if (id != "" && !id %in% smeta$vars) rlang::arg_match0(id, smeta$vars, arg_nm = "id", error_call = NULL)
Expand Down
25 changes: 20 additions & 5 deletions R/tmapGridComp_leg_landscape.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ tmapGridCompHeight.tm_legend_standard_landscape = function(comp, o) {

height = get_legend_option(comp$item.height, comp$type)

item_height = if (comp$type == "symbols") max(height, comp$gpar$size / textS) else height
item_height = if (comp$type == "symbols") {
shps = rep(comp$gpar$shape, length.out = nlev)
max(height, rep(comp$gpar$size, length.out = nlev) / textS * ifelse(shps > 999, comp$layer_args$icon.scale, 1))
} else height
itemHsIn = grid::unit(item_height * textS * o$lin, units = "inch")

titleP = comp$title.padding[c(3,1)] * titleS * o$lin
Expand Down Expand Up @@ -76,7 +79,8 @@ tmapGridCompWidth.tm_legend_standard_landscape = function(comp, o) {
# for the latter two, there are 3 ways of stretching the legend: padding (space between items), items (widths of all items), or itemsNNA (widths of non-NA items)

if (comp$type == "symbols") {
item_widths = pmax(width, rep(comp$gpar$size / textS, length.out = nlev), labelW)
shps = rep(comp$gpar$shape, length.out = nlev)
item_widths = pmax(width, rep(comp$gpar$size / textS, length.out = nlev) * ifelse(shps > 999, comp$layer_args$icon.scale, 1), labelW)
comp$stretch = if (!is.na(comp$width)) "padding" else "none"
} else if (comp$type %in% c("rect", "lines")) {
item_widths = pmax(rep(width, nlev), labelW)
Expand Down Expand Up @@ -327,8 +331,19 @@ tmapGridLegPlot.tm_legend_standard_landscape = function(comp, o, fH, fW) {
#grItems = mapply(function(i, gpari) gridCell(i+3, 2, grid::rectGrob(gp = gpari)), 1:nlev, gpars, SIMPLIFY = FALSE)
grItems = mapply(function(id, gpari) gridCell(6, id, grid::linesGrob(x = grid::unit(c(0.5,0.5), "npc"), gp = gpari)), comp$item_ids, gpars, SIMPLIFY = FALSE)
} else if (comp$type == "symbols") {
if (length(gp$size) == 1) gp$size = min(gp$size, min(get_legend_option(comp$item.height, "symbols"),
shps = rep(comp$gpar$shape, length.out = comp$nitems)


# * ifelse(shps > 999, comp$layer_args$icon.scale, 1)


if (length(gp$size) == 1) {
gp$size = min(gp$size, min(get_legend_option(comp$item.height, "symbols"),
get_legend_option(comp$item.width, "symbols")) * comp$textS)
} else {
shps = rep(gp$shape, length.out = nlev)
gp$size = rep(gp$size, length.out = nlev) * ifelse(shps > 999, comp$layer_args$icon.scale, 1)
}
gpars = gp_to_gpar(gp, split_to_n = nlev, o = o, type = comp$type)

# scale down (due to facet use)
Expand Down Expand Up @@ -356,8 +371,8 @@ tmapGridLegPlot.tm_legend_standard_landscape = function(comp, o, fH, fW) {
}
grid::gTree(children=grbs, vp=viewport(x=0.5,
y=0.5,
width=unit(gpari$size*2/3, "lines"),
height=unit(gpari$size*2/3, "lines")))
width=unit(gpari$size*9/10, "lines"),
height=unit(gpari$size*9/10, "lines")))
} else {
if (diffAlpha) {
grid::grobTree(
Expand Down
20 changes: 10 additions & 10 deletions R/tmapGridComp_leg_portrait.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ tmapGridCompHeight.tm_legend_standard_portrait = function(comp, o) {
# for the latter two, there are 3 ways of stretching the legend: padding (space between items), items (heights of all items), or itemsNNA (heights of non-NA items)

if (comp$type == "symbols") {
item_heights = pmax(height, rep(comp$gpar$size / textS, length.out = nlev))
shps = rep(comp$gpar$shape, length.out = nlev)
item_heights = pmax(height, rep(comp$gpar$size / textS, length.out = nlev) * ifelse(shps > 999, comp$layer_args$icon.scale, 1))
comp$stretch = if (!is.na(comp$height)) "padding" else "none"
} else if (comp$type %in% c("rect", "lines", "bivariate")) {
item_heights = rep(height, nlev)
Expand Down Expand Up @@ -150,7 +151,10 @@ tmapGridCompWidth.tm_legend_standard_portrait = function(comp, o) {

width = get_legend_option(comp$item.width, comp$type)

item_widths = if (comp$type == "symbols") pmax(width, rep(comp$gpar$size / textS, length.out = comp$nitems)) else rep(width, length.out = ni)
item_widths = if (comp$type == "symbols") {
shps = rep(comp$gpar$shape, length.out = comp$nitems)
pmax(width, rep(comp$gpar$size / textS, length.out = comp$nitems) * ifelse(shps > 999, comp$layer_args$icon.scale, 1))
} else rep(width, length.out = ni)


if (comp$type == "bivariate") {
Expand Down Expand Up @@ -522,18 +526,14 @@ tmapGridLegPlot.tm_legend_standard_portrait = function(comp, o, fH, fW) {

shapeLib = get("shapeLib", envir = .TMAP)
justLib = get("justLib", envir = .TMAP)

#browser()
grItems = mapply(function(id, gpari, gpari1, gpari2) {
grobs = if (gpari$shape > 999) {
grbs = if (gpari$lwd == 0) {
gList(shapeLib[[gpari$shape-999]])
} else {
gList(shapeLib[[gpari$shape-999]], rectGrob(gp=gpar(fill=NA, col=gpari$col, lwd=gpari$lwd)))
}
grbs = gList(shapeLib[[gpari$shape-999]])
grid::gTree(children=grbs, vp=viewport(x=0.5,
y=0.5,
width=unit(gpari$size*2/3, "lines"),
height=unit(gpari$size*2/3, "lines")))
width=unit(gpari$size*9/10 * comp$layer_args$icon.scale, "lines"),
height=unit(gpari$size*9/10 * comp$layer_args$icon.scale, "lines")))
} else {
if (diffAlpha) {
grid::grobTree(
Expand Down
8 changes: 2 additions & 6 deletions R/tmapGridSymbols.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,9 @@ tmapGridSymbols = function(shpTM, dt, gp, bbx, facet_row, facet_col, facet_page,


if (!is.na(shi) && shi>999) {
size = gp$size[i]*2/3 * args$icon.scale
size = gp$size[i]*9/10 * args$icon.scale
lwdid = if (length(gp$lwd) == 1L) 1 else i
grbs <- if (gp$lwd[lwdid] == 0) {
gList(shapeLib[[shi-999]])
} else {
gList(shapeLib[[shi-999]], rectGrob(gp=gpar(fill=NA, col=gp$col[i], lwd=gp$lwd[lwdid])))
}
grbs <- gList(shapeLib[[shi-999]])
gTree(children=grbs, vp=viewport(x=grid::unit(coords[i,1] + justx * size, "native"),
y=grid::unit(coords[i,2] + justy * size, "native"),
width=unit(size, "lines"),
Expand Down
5 changes: 3 additions & 2 deletions R/tmapLeafletLegend.R
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,15 @@ tmapLeaflet_legend = function(cmp, lf, o, orientation) {
if (length(sid)) {
iconLib <- get("shapeLib", envir = .TMAP)[sn[sid]-999]
symbols_icons <- merge_icons(iconLib)
size = gp2$width[sid] / gp2$baseSize

size = gp2$width[sid] / gp2$baseSize * cmp$layer_args$icon.scale/3

for (i in seq_along(sid)) {
symbols$iconUrl[sid[i]] = symbols_icons$iconUrl[i]
symbols$iconWidth[sid[i]] <- symbols_icons$iconWidth[i] * size[i]
symbols$iconHeight[sid[i]] <- symbols_icons$iconHeight[i] * size[i]
if (all(c("iconAnchorX", "iconAnchorY") %in% names(symbols_icons))) {
symbols$iconAnchorX[sid[i]] <- symbols_icons$iconAnchorX[i] * size[i]
symbols$iconAnchorX[sid[i]] <- symbols_icons$iconAnchorX[i] * size[i]
symbols$iconAnchorY[sid[i]] <- symbols_icons$iconAnchorY[i] * size[i]

}
Expand Down
2 changes: 1 addition & 1 deletion R/tmapLeaflet_layers.R
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ tmapLeafletSymbols = function(shpTM, dt, pdt, popup.format, hdt, idt, gp, bbx, f
symbols_icons <- merge_icons(iconLib)
size = gp2$width[sid] / gp2$baseSize

size[sid] = size[sid] * args$icon.scale/3 # icon.scale is set to 1 in view mode
size[sid] = size[sid] * args$icon.scale/3

for (i in seq_along(sid)) {
symbols$iconUrl[sid[i]] = symbols_icons$iconUrl[i]
Expand Down
2 changes: 2 additions & 0 deletions R/tmapScaleBivariate.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ tmapScaleBivariate = function(x1, x2, scale, legend, chart, o, aes, layer, layer
legend$vvalues = pal
legend$vneutral = pal[4]
legend$na.show = na
legend$layer_args = layer_args



chartFun = paste0("tmapChart", toTitleCase(chart$summary))
Expand Down
1 change: 1 addition & 0 deletions R/tmapScaleCategorical.R
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ tmapScaleCategorical = function(x1, scale, legend, chart, o, aes, layer, layer_a
icon_scale = icon_scale
na.show = get("na.show", envir = parent.env(environment()))
scale = "categorical"
layer_args = layer_args
})


Expand Down
1 change: 1 addition & 0 deletions R/tmapScaleContinuous.R
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ tmapScaleContinuous = function(x1, scale, legend, chart, o, aes, layer, layer_ar
scale = "continuous"
tr = tr
limits = limits
layer_args = layer_args
})
# NOTE: tr and limits are included in the output to facilitate the transformation of the leaflet continuous legend ticks (https://github.com/rstudio/leaflet/issues/665)
#vvalues_mids = sapply(cont_split(vvalues), "[", nvv/2)
Expand Down
1 change: 1 addition & 0 deletions R/tmapScaleDiscrete.R
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ tmapScaleDiscrete = function(x1, scale, legend, chart, o, aes, layer, layer_args
vneutral = value.neutral
na.show = get("na.show", envir = parent.env(environment()))
scale = "discrete"
layer_args = layer_args
})

chartFun = paste0("tmapChart", toTitleCase(chart$summary))
Expand Down
1 change: 1 addition & 0 deletions R/tmapScaleIntervals.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ tmapScaleIntervals = function(x1, scale, legend, chart, o, aes, layer, layer_arg
vneutral = value.neutral
na.show = get("na.show", envir = parent.env(environment()))
scale = "intervals"
layer_args = layer_args
})

chartFun = paste0("tmapChart", toTitleCase(chart$summary))
Expand Down
2 changes: 1 addition & 1 deletion man/tm_cartogram.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/tm_lines.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/tm_polygons.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/tm_symbols.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 94 additions & 0 deletions man/tmap_internal.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7353784

Please sign in to comment.