This is test report, aimed to demonstrate usage of leaflet R package. As an illustration, I’ve chosen the map of Roman victories, with geodata and color describing the year.
Firstly, google the nessesary data and get source easy-to-grab. Retrieve the raw data from the repository of user @rharriso and conduct preliminiary formatting:
battles<-read.csv("https://raw.githubusercontent.com/rharriso/Roman-Battles-Droid/master/assets/RomanBattles.csv")
battles$roman_victory<-sapply(battles$Result, function(res){
grepl("roman victory",res,ignore.case = T)
} )
victories<-subset(battles,roman_victory==T)
victories$lat<-sapply(as.character(victories$Coordinates), function(res){
as.numeric(strsplit(res,";",fixed=T)[[1]][1])
} )
victories$long<-sapply(as.character(victories$Coordinates), function(res){
as.numeric(strsplit(res,";",fixed=T)[[1]][2])
} )
victories$yearnum<-sapply(as.character(victories$Year),function(yr){
if(grepl("BC",yr,ignore.case = T)>0)
{-as.numeric(gsub("BC","",yr,fixed=T))
}else{as.numeric(yr)}
})
victories<-subset(victories, !is.na(lat) & !is.na(long) & !is.na(yearnum))
Now let us construct leaflet object, by : * adding the tiles and selecting the tiles provider, * adding the lng/lat coordintes of victories_datatvdataset * setting up the color based on battle’s year, * adding the circles on it’s coordinates, popups to show the battle name.
require(leaflet)
## Loading required package: leaflet
victories_data<-victories[,c("lat","long","yearnum","BattleName",'Year')]
victories_data$col<-(victories_data$yearnum-min(victories_data$yearnum))/
(max(victories_data$yearnum)-min(victories_data$yearnum))
victories_data$col<-sapply(victories_data$col,colorNumeric("RdYlBu", victories_data$col))
ll<-leaflet() %>%
addProviderTiles("Stamen.Watercolor",
options = providerTileOptions(noWrap = TRUE)
) %>%
#addMarkers(data = victories[,c("lat","long")]) %>%
addCircles(radius = 100000, lng = victories_data$long,lat=victories_data$lat, data = victories_data,fillOpacity=0.5,fill = T, weight = 1, color = "grey",fillColor=victories_data$col,
popup=paste(victories_data$BattleName," , ",victories_data$Year)
)
ll
Eventually, this map shows the geography of expansion in the rise epoch and that Rome in the early years was really striving for survival: e.g. battles in the very heartland.