I am plotting the boundary of several polygons using the tmap
package. The following code is a basic example.
library(sf)
library(tmap)
nc <- st_read(system.file("shape/nc.shp", package = "sf"))
tm_shape(nc) +
tm_borders()
This looks good. However, if I changed the style of the boundary lines, the boundaries between polygons look different than the outline. Below is one example. I changed the line type to dotted. Some of the line segments look solid or with lots of dots.
tm_shape(nc) +
tm_borders(lwd = 1, lty = "dotted")
Here is another example. I changed the line width to 2
and transparency to 0.6
. We can see that many of the inner boundaries look stronger than the outline.
tm_shape(nc) +
tm_borders(lwd = 2, alpha = 0.6)
I would like to learn why this happens and how can I make the line style universal for all boundaries and outline. I would be grateful for any hints or ideas.
Updates: Other Plotting Options
Here I tried other options to mimic the map with dotted boundary. The geom_sf
and ggspatial
can generate the boundary plot with fairly similar doted lines. However, if I changed the sf
object and plotted it using base R
or spplot
from the sp
package, the issue remains.
geom_sf
library(ggplot2)
library(sf)
nc <- st_read(system.file("shape/nc.shp", package = "sf"))
ggplot() +
geom_sf(data = nc, linetype = "dotted", fill = "white") +
theme_bw() +
theme(panel.grid = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank())
ggspatial
library(ggspatial)
library(ggplot2)
library(sf)
nc <- st_read(system.file("shape/nc.shp", package = "sf"))
ggplot() +
layer_spatial(nc, linetype = "dotted", fill = "white") +
theme_bw() +
theme(panel.grid = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank())
Base R with SP object
library(sf)
library(sp)
nc <- st_read(system.file("shape/nc.shp", package = "sf"))
nc_sp <- as(nc, "Spatial")
plot(nc_sp, col = "white", lty = "dotted")
spplot with SP object
library(sf)
library(sp)
nc <- st_read(system.file("shape/nc.shp", package = "sf"))
nc_sp <- as(nc, "Spatial")
nc_sp$Z <- 1
spplot(nc_sp, zcol = "Z", col.regions = "white", lty = 3,
colorkey = FALSE,
par.settings = list(axis.line = list(col = 'transparent')))