2

I am working with the R programming language.

I am following this tutorial over here (https://www.johnmackintosh.net/blog/2017-09-01-easy-maps-with-tmap/) and trying to produce the following maps with a new dataset:

enter image description here

Here is the shapefile I am using:

library(sf)  
library(leaflet)
library(leafgl)
library(colourvalues)
library(leaflet.extras)

# built in shapefile
nc <- st_read(system.file("gpkg/nc.gpkg", package="sf"), quiet = TRUE) %>% 
  st_transform(st_crs(4326)) %>% 
  st_cast('POLYGON')

Next, I added several variables to this shapefile that can for plotting:

nc$var1 = rnorm(length(nc$AREA), 1000, 100)
nc$var2 = rnorm(length(nc$AREA), 1000, 100)
nc$var3 = rnorm(length(nc$AREA), 1000, 100)
nc$var4 = rnorm(length(nc$AREA), 1000, 100)
nc$var5 = rnorm(length(nc$AREA), 1000, 100)
nc$var6 = rnorm(length(nc$AREA), 1000, 100)
nc$var7 = rnorm(length(nc$AREA), 1000, 100)
nc$var8 = rnorm(length(nc$AREA), 1000, 100)

Finally, I tried to plot these maps as instructed within the tutorial:

library(tmap)
library(tmaptools)
library(viridis)

small_mult<- tm_shape(nc) +
  tm_fill(col = c("var1","var2","var3","var4",
                  "var5","var6","var7","var8"),
          palette = viridis(n = 5, direction = -1, option = "C"),
          title=c("title var 1", "title var 2","title var 3","title var 4",
                  "title var 5","title var 6", "title var 7","title var 8"))

When I look at the output of this code - I notice the following things:

enter image description here

  • In the tutorial, they were able to neatly make two rows of four plots each, however mine was made unevenly
  • The legend appears to be intersecting with the map itself
  • The font size is too small
  • It appears that the maps have been "zoomed in"

In general, the maps I made seem to overall have a lower quality resolution compared to the ones from the tutorial.

Is there some options that I have missed? Is there a quick way to fix this?

Thanks!

stats_noob
  • 5,401
  • 4
  • 27
  • 83

1 Answers1

2

This should get you started with some options from tmap to control for the legend position and text sizes.

I'm not sure what you mean by the map "appears zoomed in", the map looks pretty much what is expected (check out other SO examples using this dataset).

The resolution seems fine on my monitor.


# expand the boundary box based on:
# https://stackoverflow.com/questions/60892033/how-do-you-position-the-title-and-legend-in-tmap

bbox_new <- st_bbox(nc) # current bounding box

xrange <- bbox_new$xmax - bbox_new$xmin # range of x values
yrange <- bbox_new$ymax - bbox_new$ymin # range of y values

bbox_new[1] <- bbox_new[1] - (0.25 * xrange) # xmin - left

bbox_new <- bbox_new |> 
  st_as_sfc() 


small_mult <- 
  tm_shape(nc, bbox = bbox_new) +
  tm_fill(col = c("var1","var2","var3","var4",
                  "var5","var6","var7","var8"),
          palette = viridis(n = 5, direction = -1, option = "C"),
          title=c("title var 1", "title var 2","title var 3","title var 4",
                  "title var 5","title var 6", "title var 7","title var 8")) +
  tm_facets(ncol = 2)+
  tm_layout(legend.position = c("left", "top"),
            legend.title.size = 1.5,
            legend.text.size = 0.75)

small_mult

Created on 2022-12-31 with reprex v2.0.2

Peter
  • 11,500
  • 5
  • 21
  • 31