R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

vietn_impc <-read_csv("Vietnam3_correctlatlong.csv")
## Parsed with column specification:
## cols(
##   DIRECCION = col_character(),
##   SERIE_PROPIEDAD = col_character(),
##   ORDENANZA_PROPIEDAD = col_character(),
##   VALOR_PROPIEDAD_ESTRUCTURA = col_double(),
##   TERRENO = col_character(),
##   SERIE_TERRENO = col_character(),
##   ORDENANZA_TERRENO = col_double(),
##   VALOR_TERRENO = col_character(),
##   Long = col_double(),
##   Lat = col_double(),
##   CATASTTRO_REGISTRO = col_character(),
##   CLASIFICACION = col_character(),
##   ENTRADA = col_character()
## )
viet_cleanc <-vietn_impc %>% 
  clean_names()

In any dataset, the first colum you should see is the Longitud first, and then Latitud. In this one, we have latitutde first. We need to reaarange them and create a new dataframe named viet_clean2

colnames(viet_cleanc)
##  [1] "direccion"                  "serie_propiedad"           
##  [3] "ordenanza_propiedad"        "valor_propiedad_estructura"
##  [5] "terreno"                    "serie_terreno"             
##  [7] "ordenanza_terreno"          "valor_terreno"             
##  [9] "long"                       "lat"                       
## [11] "catasttro_registro"         "clasificacion"             
## [13] "entrada"
viet_clean2c <- viet_cleanc[, c(1,2,3,4,5,6,7,8,10,9,11,12,13)]
colnames(viet_clean2c)
##  [1] "direccion"                  "serie_propiedad"           
##  [3] "ordenanza_propiedad"        "valor_propiedad_estructura"
##  [5] "terreno"                    "serie_terreno"             
##  [7] "ordenanza_terreno"          "valor_terreno"             
##  [9] "lat"                        "long"                      
## [11] "catasttro_registro"         "clasificacion"             
## [13] "entrada"

Ok, now we need to make the data spatial, by assigning it a projection or CRS. The data that we get from la junta de planificación es ESPG: 4269, which is a type of NAD84. However the one which is the most commonly used for mapping purposes is ESPG 4326. The sf package has a very easy way of doing this using the st_as_sf function. Almost all the sf functions begin with st_, which is nice and allows us to more easily search though the library.

Below we need to provide the CRS, which we looked up already, and the columns that have the spatial data. One tip, these should be listed in X then Y order…which happens to be LONG then LAT

df.VTc <- st_as_sf(viet_clean2c, coords = c("long", "lat"), crs = 4437)

After telling R which is the coordinates you have in the dataframe, then you can transform it to the one you need. As said before, 4326 is the most common one and the one that packages such as leaflet recognizes.

df.VTc<-st_transform(x = df.VTc, crs = 4326)

When you see the new dataframe you will see a new colum named (geometry), this one contains the coordinates but one single line. You need to separate both values in different columns in order to be able to map using long and lat. The first number in the column geometry, responds to the longitud. The folowing comands add a new colum named long, with the first[,1] part of the coordinates of the dataframe, later a second column with the second number [,2].

df.VTc$long<-st_coordinates(df.VTc)[,1] # get coordinates
df.VTc$lat<-st_coordinates(df.VTc)[,2] # get coordinates

Ya la data está preparada, entonces se debe mapear usando leaflet. Leaflet (https://leafletjs.com/) es una librería de JavaScript que se utiliza para hacer mapas interactivos. Es la más usada hoy en día. El paquete de R llamado leaflet permite la integración con esta librería.

En lugar de usar +, leaflet utiliza el pipe (%>%) para crear las visualizaciones. Se necesita addTiles() para agregar el mapa, y después algún tipo de puntos geográficos.

df.VTc %>%
  leaflet() %>%
  addTiles() %>%
  addMarkers(~long, ~lat)