Spheric visualization map using Tmap
I am using a map to create a graphic of the world. I have used different functions to modify the world's appearance to spheroid. Typically, the result is a map in a rectangular view. I have tried different methods but found it impossible to modify it to a spherical display. Could you please tell me if creating this display using Tmap is impossible?
I have provided you with a dummy code to help you find the solution that works for you.
countries <- world(resolution = 5, path = "maps") countries_df <- data.frame( Countries = c("Argentina", "Brazil", "Canada", "Mexico", "Chile", "Colombia", "Peru", "France", "Germany", "Australia"), Count = sample(1:10, 10, replace = TRUE) # Add dummy numbers in the "count" column )
countries2 <- merge(countries, countries_df, by.x = "NAME_0", by.y = "Countries", all.x = TRUE) countries2 <- countries2[countries2$NAME_0 %in% countries_df$Countries,] centroids <- terra::centroids(countries2)
f1 <- tm_shape(countries) +
tm_polygons(col = "#a3a3a3") +
tm_compass(position = c("left", "top"), size = 3) +
tm_scalebar(position = c("right", "bottom", size=10), breaks = c(0, 2000, 4000)) +
tm_shape(centroids) +
tm_symbols(
size = "Count",
fill = "#ff8000",
size.scale = tm_scale_intervals(
#breaks = c(1, 2, 4, 6, 8),
values.range = c(0.2,1.2)),
size.legend = tm_legend("Frecuency"),
#legend.show = TRUE,
#legend.size.show = TRUE
)+
tm_layout(legend.bg.color="#ffffff", legend.position = c(0.09, 0.45))
f1 + tm_grid(alpha = 0.6, col = '#a7a7a7', labels.size = 1, n.y = 9, n.x = 6)
I appreciate your support
Cristian
Hi @crcruzr two approaches:
- Use an orthograpic projection. See https://r-tmap.github.io/tmap/articles/foundations_crs for an example. Note that this does not always work out of the box due to clipping issues. E.g. in that example the USA doesn't seen to render well.
- Use our new extension library tmap.mapgl. See https://github.com/r-tmap/tmap.mapgl/issues/1 for a working example. This is work-in-progress, but I expect to publish an experimental version to CRAN within a few weeks.
Thanks for the ideas. I will use the first one but will keep an eye out for the second option. It seems much better
Hi @mtennekes
I'm working on aligning the Y-axis labels to the edge of the World, but it's proving to be a challenge! Could I wait for the integration of the tmap? mapgl library to make it happen?
The following code is a dummy example.
''' data(World)
tm_shape(World) + tm_graticules( ) + tm_polygons() + tm_crs("+proj=eck4") + tm_layout(earth_boundary = TRUE, frame=FALSE ) '''
Best,
Cristian
library(tmap)
data(World)
tm_shape(World) +
tm_graticules( ) +
tm_polygons() +
tm_crs("+proj=eck4") +
tm_layout(earth_boundary = TRUE,
frame=FALSE
)

@crcruzr -- just to clarify: you would like to place 50N and 50S closer to the earth boundary?
@mtennekes -- is this something that could be done/you consider adding?
@Nowosad @crcruzr It is something that can be added as new feature, but has very low (if not 0) priority, also because it is very tricky to implement.
However, I've found a nice but hacky work-around:
library(sf)
x = st_as_sf(as.data.frame(matrix(c(rep(-175, 3), 50, 0, -50), ncol = 2, byrow = FALSE, dimnames = list(NULL, c("x", "y")))), coords = c("x", "y"), crs = 4326)
x$labels = c("50° N", "0", "50° S")
tm_shape(World) +
tm_graticules(labels.show = c(T, F)) +
tm_polygons() +
tm_shape(x) +
tm_text("labels", size = 0.6, xmod = -2, options = opt_tm_text(just = "right")) +
tm_crs("+proj=eck4") +
tm_layout(earth_boundary = TRUE,
frame=FALSE)
@mtennekes looks good! @crcruzr I hope that this hacky solution is fine by you.
Great. It was exactly what I expected. With these code lines, I can add my layer and adjust the y-axis as needed.
Thanks so much for the idea.