Skip to content

Commit

Permalink
improved compass and scaling in general in plot mode #861
Browse files Browse the repository at this point in the history
  • Loading branch information
mtennekes committed Apr 10, 2024
1 parent 5ed338f commit e54cf3d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion R/step1_helper_meta.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ preprocess_meta_step1 = function(o) {
for (nm in names(o)[grep("fontface", names(o), fixed = TRUE)]) if (is.null(get(nm))) assign(nm, text.fontface)
for (nm in names(o)[grep("fontfamily", names(o), fixed = TRUE)]) if (is.null(get(nm))) assign(nm, text.fontfamily)

for (nm in names(o)[c(grep("lwd", names(o), fixed = TRUE), grep("size", names(o), fixed = TRUE))]) assign(nm, get(nm) * scale)
#for (nm in names(o)[c(grep("lwd", names(o), fixed = TRUE), grep("size", names(o), fixed = TRUE))]) assign(nm, get(nm) * scale)

# if (is.null(legend.title.fontface)) legend.title.fontface = text.fontface
# if (is.null(legend.title.fontfamily)) legend.title.fontfamily = text.fontfamily
Expand Down
16 changes: 10 additions & 6 deletions R/tmapGridComp.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ tmapGridLegPlot.tm_credits = function(comp, o, fH, fW) {
tmapGridCompPrepare.tm_compass = function(comp, o) {
o$attr.color.light = is_light(o$attr.color)
within(comp, {
text.size = text.size * o$scale

if (is.na(text.color)) text.color = o$attr.color
text.color = do.call("process_color", c(list(col=text.color), o$pc))

Expand All @@ -76,8 +78,7 @@ tmapGridCompPrepare.tm_compass = function(comp, o) {
color.dark = do.call("process_color", c(list(col=color.dark), o$pc))
color.light = do.call("process_color", c(list(col=color.light), o$pc))

text.size = text.size * o$scale
lwd = lwd * o$scale
asp = if (type == "arrow") 0.5 else 1

show = TRUE
if (is.na(type)) type = o$type
Expand All @@ -93,7 +94,6 @@ tmapGridCompHeight.tm_compass = function(comp, o) {
textS = comp$text.size #* o$scale
#textP = comp$padding[c(3,1)] * textS * o$lin


marH = comp$margins[c(3,1)] * textS * o$lin
hs = c(marH[1], comp$nlines * textS * o$lin, marH[2])

Expand All @@ -116,8 +116,9 @@ tmapGridCompWidth.tm_compass = function(comp, o) {
textS = comp$text.size #* o$scale
#textP = comp$padding[c(3,1)] * textS * o$lin


marW = comp$margins[c(2,4)] * textS * o$lin
ws = c(marW[1], comp$nlines * textS * o$lin, marW[2])
ws = c(marW[1], comp$nlines * textS * o$lin * comp$asp, marW[2])

sides = switch(comp$position$align.h, left = "second", right = "first", "both")
wsu = set_unit_with_stretch(ws, sides = sides)
Expand Down Expand Up @@ -161,7 +162,7 @@ tmapGridLegPlot.tm_compass = function(comp, o, fH, fW) {
id = rep(1:16, each=3)
fill = c(dark, light, dark, light, light, dark, light, dark)
} else if (comp$type=="arrow") {
x = list(c(.5, .7, .5, .5, .3, .5))
x = list(c(.5, .9, .5, .5, .1, .5))
y = list(c(1, 0, .2, 1, 0, .2))
id = rep(1:2, each=3)
fill = c(dark, light)
Expand All @@ -187,7 +188,7 @@ tmapGridLegPlot.tm_compass = function(comp, o, fH, fW) {

} else if (comp$type=="rose") {
cr = c(.45, .42, .2, .17, .1)
LWD = convertWidth(unit(.01, "npc"), "points", valueOnly=TRUE) * comp$lwd
LWD = (o$lineH * 24) * comp$lwd
cd = seq(1/8, 15/8, by=.25) * pi
cd2 = seq(1/4, 7/4, by=.5) * pi
cd3 = seq(0, 1.75, by=.25) * pi
Expand Down Expand Up @@ -306,6 +307,8 @@ tmapGridLegPlot.tm_compass = function(comp, o, fH, fW) {
grobLabels),
name="compass")
})
po(comp$text.size)

grid::grobTree(compass, vp = vp)
}

Expand Down Expand Up @@ -504,6 +507,7 @@ tmapGridLegPlot.tm_scalebar = function(comp, o, fH, fW) {

grobBG = if (getOption("tmap.design.mode")) rectGrob(gp=gpar(fill="orange")) else NULL


# other grid cells are aligns (1 and 5) and margins (2 and 4)
scalebar = gridCell(3,3, {
gTree(children=gList(
Expand Down
4 changes: 2 additions & 2 deletions R/tmapGridLegend.R
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ tmapGridLegend = function(comp, o, facet_row = NULL, facet_col = NULL, facet_pag
component.offset.v = get_option_class(o$component.offset, class = CASE.v(paste0(class, "side")), spatial_class = FALSE)
}

offsetIn.h = component.offset.h * o$lin * o$scale + (o$frame.lwd * o$scale / 144) # 1 line = 1/72 inch, frame lines are centered (so /2)
offsetIn.v = component.offset.v * o$lin * o$scale + (o$frame.lwd * o$scale / 144)
offsetIn.h = component.offset.h * o$lin + (o$frame.lwd * o$scale / 144) # 1 line = 1/72 inch, frame lines are centered (so /2)
offsetIn.v = component.offset.v * o$lin + (o$frame.lwd * o$scale / 144)
marginIn = o$component.stack.margin * o$lin

marginInTot = (n - 1L) * marginIn
Expand Down
10 changes: 5 additions & 5 deletions R/tmap_options.R
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@
credits.frame.lwd = 1,
credits.frame.r = 2,
credits.stack = "vertical",
credits.position = tm_pos_in(pos.h = "right", pos.v = "bottom", align.h = "left", align.v = "top", just.h = "left", just.v = "bottom"),
credits.position = tm_pos_in(pos.h = "right", pos.v = "bottom", align.h = "right", align.v = "top", just.h = "left", just.v = "bottom"),
credits.width = NA,
credits.heigth = NA,
credits.group.frame = TRUE,
Expand All @@ -452,12 +452,12 @@
compass.lwd=1,
compass.bg.color=NA,
compass.bg.alpha=NA,
compass.margins = c(0.4, 0.4, 0.4, 0.4),
compass.margins = c(0.25, 0.25, 0.25, 0.25),

# standard arguments:
compass.show = FALSE,
compass.stack = "vertical",
compass.position = tm_pos_in(pos.h = "right", pos.v = "bottom", align.h = "left", align.v = "top", just.h = "left", just.v = "bottom"),
compass.position = tm_pos_in(pos.h = "right", pos.v = "bottom", align.h = "right", align.v = "top", just.h = "left", just.v = "bottom"),
compass.frame = FALSE,
compass.frame.lwd = 1,
compass.frame.r = 2,
Expand All @@ -479,7 +479,7 @@

# standard arguments:
scalebar.stack = "vertical",
scalebar.position = tm_pos_in(pos.h = "right", pos.v = "bottom", align.h = "left", align.v = "top", just.h = "left", just.v = "bottom"),
scalebar.position = tm_pos_in(pos.h = "right", pos.v = "bottom", align.h = "right", align.v = "top", just.h = "left", just.v = "bottom"),
scalebar.frame = FALSE,
scalebar.frame.lwd = 1,
scalebar.frame.r = 2,
Expand Down Expand Up @@ -523,7 +523,7 @@
#

mouse_coordinates.stack = "vertical",
mouse_coordinates.position = tm_pos_in(pos.h = "right", pos.v = "bottom", align.h = "left", align.v = "top", just.h = "left", just.v = "bottom"),
mouse_coordinates.position = tm_pos_in(pos.h = "right", pos.v = "bottom", align.h = "right", align.v = "top", just.h = "left", just.v = "bottom"),
mouse_coordinates.show = FALSE,

panel.show = TRUE,
Expand Down

0 comments on commit e54cf3d

Please sign in to comment.