ArTu
ArTu

Reputation: 483

how can I add point from a database to a tmap?

I would like to add cases points on a map. In particular, my points data are in this form:

cita$lat
  [1] 41.88333 41.88333 44.95971 45.16110 45.16110 45.16110 45.16110
  [8] 45.16110 45.16110 45.16110 45.16110 45.16110 45.16110 45.16110
 [15] 45.16110 45.16110 45.16110 45.29775 45.29775 45.13333 45.16110
 [22] 45.29775 45.29775 45.29775 45.29775 45.29775 45.18885 45.19092

cita$lng
  [1] 12.500000 12.500000 10.689220  9.701432  9.701432  9.701432
  [7]  9.701432  9.701432  9.701432  9.701432  9.701432  9.701432
 [13]  9.701432  9.701432  9.701432  9.701432  9.701432 11.658382
 [19] 11.658382 10.033333  9.701432 11.658382 11.658382 11.658382
 [25] 11.658382 11.658382  9.690454  9.726434 

I created a map in this way:

library(rnaturalearth)
ita = ne_countries(country = "Italy") # United States borders
class(ita)
ita_sf = st_as_sf(ita)

tm_shape(ita_sf) +
  tm_fill() +
  tm_borders() 

How can I do this?

thank you!

Upvotes: 3

Views: 1361

Answers (1)

Ben
Ben

Reputation: 30474

library(rnaturalearth)
library(sf)
library(tmap)

ita = ne_countries(country = "Italy") # United States borders
class(ita)
ita_sf = st_as_sf(ita)

cita_sf = st_as_sf(cita, coords = c('long', 'lat'), crs = st_crs(ita_sf)$proj4string)

tm_shape(ita_sf) +
  tm_fill() +
  tm_borders() +
tm_shape(cita_sf) +
  tm_dots()

Map

italy map with points/dots added

Data

cita <- data.frame(
  lat = c(41.88333, 41.88333, 44.95971, 45.16110, 45.16110, 45.16110, 45.16110,
          45.16110, 45.16110, 45.16110, 45.16110, 45.16110, 45.16110, 45.16110,
          45.16110, 45.16110, 45.16110, 45.29775, 45.29775, 45.13333, 45.16110,
          45.29775, 45.29775, 45.29775, 45.29775, 45.29775, 45.18885, 45.19092),
  long = c(12.500000, 12.500000, 10.689220,  9.701432,  9.701432,  9.701432,
            9.701432,  9.701432,  9.701432,  9.701432,  9.701432,  9.701432,
            9.701432,  9.701432,  9.701432,  9.701432,  9.701432, 11.658382,
            11.658382, 10.033333,  9.701432, 11.658382, 11.658382, 11.658382,
            11.658382, 11.658382,  9.690454,  9.726434)
)

Upvotes: 3

Related Questions