Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

The boundary lines between two polygons seems to be different than the outline when plotted using the tmap package

$
0
0

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()

enter image description here

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")

enter image description here

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)

enter image description here

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())

enter image description here

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())

enter image description here

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")

enter image description here

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')))

enter image description here


Viewing all articles
Browse latest Browse all 201894

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>