1

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.

lovalery
  • 4,524
  • 3
  • 14
  • 28
dkro
  • 193
  • 1
  • 8

1 Answers1

1

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.

Susan Switzer
  • 1,531
  • 8
  • 34
  • This works! Do you know why it works with ```qtm``` and not in my example? (Disregarding my typo with dat and battle_dat) – dkro Jun 24 '20 at 15:15
  • I am not sure, So sorry. A guess: Perhaps more arguments passed to tm_shape would get you to same spot. I always start with a qtm, then build layers from there within tmap or as I pass to leaflet. – Susan Switzer Jun 24 '20 at 19:14