dkro
dkro

Reputation: 213

Can you switch from tmap to leaflet?

I am trying to see if you can create a tmap map, convert it to a leaflet map, and then add leaflet features.

For example:


battle_dat<-read.csv("GeoBattleData_YZ.csv") # from https://doi.org/10.7910/DVN/1KCCX2


battle_dat<-battle_dat %>% select(war,atkr,defr,date,long,lat,dist_att,dist_def,duration) %>% 
  mutate(year=substr(date,1,4))


battle_dat<-battle_dat[!is.na(battle_dat$long),]
battle_dat<-SpatialPointsDataFrame(coords = battle_dat[,c(5:6)],data = battle_dat,proj4string=CRS("+init=EPSG:4326"))
tm<-tm_shape(dat) + tm_dots(col="duration") # make tmap

tm<-tmap_leaflet(tm) # convert to leaflet

tm %>%

addDrawToolbar(

targetGroup='draw',

polylineOptions=FALSE,

markerOptions = FALSE,

circleOptions = T,

polygonOptions=F,

singleFeature=T,

circleMarkerOptions=F)

I get the following error:

"Error in dispatch(map, method, leaflet = { : Invalid map parameter"

So, I am not sure if this an issue on my end or if I simply cannot do what I'm trying to do. It's not that big of a deal, but I find that it is easier to do some things in tmap and other things in leaflet.

Upvotes: 1

Views: 415

Answers (1)

Susan Switzer
Susan Switzer

Reputation: 1922

Using qtm from the tmap package I am able to make a basic plot of the battle sites:

enter image description here

library(tidyverse)
library(tmap)
library(leaflet)
library(leaflet.extras)
library(sp)

GeoBattleData_YZ <- read.csv("GeoBattleData_YZ.csv") # from https://doi.org/10.7910/DVN/1KCCX2


battle_dat<-GeoBattleData_YZ %>% 
  select(war,atkr,defr,date,long,lat,dist_att,dist_def,duration) %>% 
  mutate(year=substr(date,1,4))


battle_dat<-battle_dat[!is.na(battle_dat$long),]

battle_dat<-SpatialPointsDataFrame(coords = battle_dat[,c(5:6)],
                                   data = battle_dat,
                                   proj4string = CRS("+init=EPSG:4326"))

tm <-qtm(battle_dat, 
         symbols.col = 'duration')

tm

map<-tmap_leaflet(tm) %>%

  addDrawToolbar(
    targetGroup='draw',
    polylineOptions=FALSE,
    markerOptions = FALSE,
    circleOptions = T,
    polygonOptions=F,
    singleFeature=T,
    circleMarkerOptions=F)
map

Your example, I think, should have been calling battle_dat rather than dat in your initial tm_shape(dat) line.

Upvotes: 1

Related Questions