The example at hand shows how quickly and neatly maps can be created with Leaflet in R.
library(leaflet) ## loading the package
I created a file that contains longitudes, latitudes and popup text for some of the major cities I have visited.
# load the file and convert to numeric
citiestravel <-read.table("C:/users/johan/Documents/travel.txt", header = TRUE)
citiestravel$long <- as.numeric(paste(citiestravel$long))
citiestravel$lat <- as.numeric(paste(citiestravel$lat))
# check the file before mapping
str(citiestravel)
## 'data.frame': 32 obs. of 3 variables:
## $ long : num 16.4 11.6 13.4 -74 -80.2 ...
## $ lat : num 48.2 48.1 52.5 40.7 25.8 ...
## $ label: Factor w/ 31 levels "amazingly beautiful â<U+0080>¦ unfortunately never in summer and 'just' for business",..: 24 24 23 16 17 7 14 2 22 5 ...
head(citiestravel)
## long lat label
## 1 16.37382 48.20817 various times (business and leisure)
## 2 11.58198 48.13513 various times (business and leisure)
## 3 13.40495 52.52001 unfortunately only once (yet!)
## 4 -74.00594 40.71278 leaves you speechless
## 5 -80.19179 25.76168 loved it
## 6 151.20930 -33.86882 could imagine living there
We finally draw the map. I decided not to use the label as I found it “overdone” (maps show the names of places). People should get curious and zoom in :-)
leaflet(data = citiestravel) %>% addTiles() %>%
addCircleMarkers(citiestravel$long, citiestravel$lat, popup = ~as.character(citiestravel$label), color = "#e6550d")