Dear tmap-developers,
thanks for providing and maintaining this great package.
I am sorry in advance in case this is a newbie question. I was wondering if it is possible to add axis ticks to a map. So far I came only up with "pseudo"-ticks inside a plot produced by gridlines underlying a raster plot:
library(tmap)
library(raster)
data("dem", package = "RQGIS")
# create hillshade
hs = hillShade(slope = terrain(dem, "slope"), aspect = terrain(dem, "aspect"))
# create contour
cn = rasterToContour(dem)
tm_shape(hs) +
tm_grid(col = "black", n.x = 2, n.y = 2, labels.inside.frame = FALSE,
labels.rot = c(0, 90)) +
tm_raster(palette = gray(0:100 / 100), n = 100, legend.show = FALSE) +
tm_shape(dem) +
tm_raster(alpha = 0.5, palette = terrain.colors(25),
auto.palette.mapping = FALSE, legend.show = FALSE) +
tm_shape(cn) +
tm_lines(col = "white") +
tm_text("level", col = "white") +
tm_layout(outer.margins = c(0.04, 0.04, 0.02, 0.02))
Hi, could you post an example of what you want to achieve?
Hi,
I was referring to x- and y-axis ticks. Here is one example:
library("rasterVis")
library("latticeExtra")
data("dem", package = "RQGIS")
# create hillshade
hs = hillShade(slope = terrain(dem, "slope"), aspect = terrain(dem, "aspect"))
# create contour
cn = rasterToContour(dem)
# create contourplot
cont = rasterVis::contourplot(dem, margin = FALSE, col = "white",
labels = list(col = "white", cex = 0.6))
# overlay hillshade with topo DEM and contourlines
spplot(dem, col.regions = terrain.colors(25), alpha.regions = 0.5,
scales = list(tck = c(1, 0)),
colorkey = FALSE) +
# add hillshade
as.layer(spplot(hs, col.regions = gray(0:100 / 100)), under = TRUE) +
# add contour plot
cont
Unfortunately, tmap does not plot ticks. The reason is that maps usually do not have ticks when there are no grid lines. I like your creative solution. You can even tweak it a little bit more:
rect <- tmaptools::bb_sp(hs)
bbx <- tmaptools::bb(hs, xlim = c(-.02, 1), ylim = c(-.02, 1), relative = TRUE)
tm_shape(hs, bbox = bbx) +
tm_grid(col = "black", n.x = 2, n.y = 2, labels.inside.frame = FALSE,
labels.rot = c(0, 90)) +
tm_raster(palette = gray(0:100 / 100), n = 100, legend.show = FALSE) +
tm_shape(dem) +
tm_raster(alpha = 0.5, palette = terrain.colors(25),
auto.palette.mapping = FALSE, legend.show = FALSE) +
tm_shape(cn) +
tm_lines(col = "white") +
tm_text("level", col = "white") +
qtm(rect, fill = NULL) +
tm_layout(outer.margins = c(0.04, 0.04, 0.02, 0.02), frame = FALSE)
Thanks for the tweaked version! However, I think there are a lot of maps that use axis ticks without grid lines. I even think that this is the more usual case (but maybe that is just my biased opinion). Maybe when plotting large regions, continents or the world it is more common to use grid lines. Would it be hard to add axis ticks to tmap?
https://github.com/mtennekes/tmap/issues/315
library(tmap)
library(raster)
#> Loading required package: sp
data("dem", package = "RQGIS")
# create hillshade
hs = hillShade(slope = terrain(dem, "slope"), aspect = terrain(dem, "aspect"))
# create contour
cn = rasterToContour(dem)
tm_shape(hs) +
tm_grid(col = "black", n.x = 4, n.y = 4, lines = FALSE,
labels.rot = c(0, 90)) +
tm_raster(palette = gray(0:100 / 100), n = 100, legend.show = FALSE) +
tm_shape(dem) +
tm_raster(alpha = 0.5, palette = terrain.colors(25), legend.show = FALSE) +
tm_shape(cn) +
tm_lines(col = "white") +
tm_text("level", col = "white")

Created on 2019-06-19 by the reprex package (v0.3.0)
Most helpful comment
Thanks for the tweaked version! However, I think there are a lot of maps that use axis ticks without grid lines. I even think that this is the more usual case (but maybe that is just my biased opinion). Maybe when plotting large regions, continents or the world it is more common to use grid lines. Would it be hard to add axis ticks to tmap?