0

I made the following map using tmap:

enter image description here

My data source: lic_bed_sf.csv, and in .gpkg format .gpkg

My code is as follows:

  tm_shape(lic_bed_sf) +
  tm_polygons("density", 
              id = "county_name", 
              palette = "Blues", 
              border.col = "#555555") +
  tm_layout(fontfamily = "Athelas", 
            legend.position = c("left", "bottom"),
            legend.text.size = 0.8,
            legend.text.fontface = "bold",
            frame = F,
            inner.margins = c(0.12, 0.05, 0.10, 0.05)) + 
  tm_text("county_name", 
          size = 0.50,
          fontfamily = "Kefa", 
          fontface = "bold") + 
  tm_scale_bar(text.size = 0.60) 

My questions are:

  1. As you can see, some of the county names overlap, for example Alamance, Orange and Durham. Is there any way I can select these counties and adjust the text angle to the ones in the following map?
  2. How to add a thick state border like the one showing in the second map?

enter image description here

user438383
  • 5,716
  • 8
  • 28
  • 43
celilati
  • 153
  • 7
  • Hi and welcome to SO! Can you share the original data to reproduce your plot? – agila Apr 10 '20 at 09:56
  • Hi there, thanks for the reply! I updated my question to include a link to my data source. Let me know if you cannot access it. – celilati Apr 10 '20 at 17:51
  • Hi! Can you share (via dropbox or whatever) the output of `st_write(lic_bed_sf, "lic_bed_sf.gpkg")`? Because it's unnecessary difficult and annoying reading a sf file saved as .csv – agila Apr 11 '20 at 12:17
  • It's now updated and includes a .gpkg file through a Dropbox link. – celilati Apr 11 '20 at 16:36
  • Ok, thanks! The second point is quite easy and you should add `tm_shape(st_boundary(st_union(lic_bed_sf))) + tm_lines(lwd = 2))` to your `tmap` code. I'm not sure about the first question. – agila Apr 11 '20 at 17:10
  • Woohoo, it works! Thank you! – celilati Apr 11 '20 at 18:00

1 Answers1

0

Edit: This is extremely hacky but shows it can be done in principle. I manually added a linestring based on where the Carteret centroid was. Centroid grabbing technique inspired by this answer.

The text won't plot at the rotated angle unless the line is also plotted (I made the line invisible with alpha = 0).

lic_bed_sf <- st_read('lic_bed_sf.gpkg')

lic_bed_sf$centroids <-
  st_transform(lic_bed_sf, 29101) %>% 
  st_centroid() %>% 
  st_transform(., '+proj=longlat +ellps=WGS84 +no_defs')

carteretTest <- filter(lic_bed_sf, county_name == "Carteret")

carteretTest$geom <- st_sfc(sf::st_linestring(matrix(c(-76.2, -76.8, 35, 34.7), , 2)),crs = 4326)

tm_shape(lic_bed_sf) +
  tm_polygons("density", 
              id = "county_name", 
              palette = "Blues", 
              border.col = "#555555") +
  tm_layout(fontfamily = "Athelas", 
            legend.position = c("left", "bottom"),
            legend.text.size = 0.8,
            legend.text.fontface = "bold",
            frame = F,
            inner.margins = c(0.12, 0.05, 0.10, 0.05)) + 
  tm_scale_bar(text.size = 0.60) +
  tm_shape(carteretTest) + 
  tm_lines(alpha = 0) +
  tm_text("county_name", 
          size = 0.50,
          fontfamily = "Kefa", 
          fontface = "bold",
          along.lines = T)

enter image description here

legatrix
  • 101
  • 2
  • 1
    Hi there! Thank you for getting back. I did try auto.placement = F within tm_text(), and unfortunately it did not alter any overlap. Could you elaborate a bit more on the second method you proposed? – celilati Apr 18 '20 at 01:08
  • @celilati see edit. By the way, if it was me, i would add the rotated labels later in Illustrator (or a similar program)! – legatrix Apr 18 '20 at 22:27