hello and welcome back
in this last video
of the series
we will go over
map
for two states we will start
by preparing
the data and then create the map
we'll also use different colors for
two different states and then create toggle
option to switch between the two states
and finally we will add an option
to cluster the counties
let's save within USA
so we use this 'c' where we have entire data
and then filter country
United states
province
massachusetts
this coma here means united states and
massachusetts and then I am going to use this vertical line
to indicate
we massachusetts or New York
we can further modify USA
we can group by
city, province
longitude, latitude
and then summarise
so we are going to count all the cases using
'n'
we can also arrange everything in descending order
we have only New York and massachusetts states
and all the counties so this is a
subset of the entire data and we have
only 62 rows which means there are
62 counties
so we can use USA data now
leaflet
this time let's use 'openstreet' map
and then we can add circle markers
we can color everything let's say 'red'
and let's say popup is city
we have all counties from massachusetts and
New York we can also save this in 'map'
and we are going to reuse this map several times
for example if we want to add markers
to this map and run this
so for each county it's going to add a marker
or if we want to remove this markers and also
circles
we can again use map and say
clear markers
so we get a clean map
so let's say we want to have a different co;or marker
for massachusetts and for New York
so state is a factor variable
so let's define my color
and we can have palette
so let's say first one will be red
second one will be blue
we can also specify levels
so let's say red one is for massachusetts
and blue one is for New York
so this should be one equal
make use of map that we use earlier
so it's the same map and that's where we are going to plot
red and blue circles
and let's also have lables
paste0
so we will have city or county and within parenthesis
we can have states
then province
and then close the parenthesis
so we have all the data points for massachusetts and
Newyork
actually this should be a parenthesis and not c
if I give a space here
it will do better
so now you can see
if you do leaflet
add tiles and then we can add
searchOSM
and another thing we can add is
reverse search OSM
it will allow us to search for anything
so
suppose I look for Boston
in massachusetts
it can locate Boston and
if I click here
it can give me the latitude and longitude
values
Let's store within MA massachusetts data
so we can use filter and our data
in in USA
where for province we say massachusetts
let's store within 'm' a map
so I am going to use cartoDB provider
and then circle marker
where data is massachusetts
so let's have a radius of 5
for label values htmlEscape
and city
we'll use blue color for massachusetts
so we can save this thing in 'm'
so let's run this and then if you run 'm'
so you can see plot for massachusetts counties
we can repeat
this for New york I am going to copy this whole thing
and say this is for New york
because we have this map information
for massachusetts stores in m
instead of this we'll just use 'm'
and the output will be stored in 'm'
so we are going to modify m to contain
New york information
so the color for New york we are going to use will be red
I am going to add one more line
so it is add layers control
and we'll say overlayGroups equal
massachusetts and New york
you can see top right corner
you have the feature to toggle between this two states
so for example this is only for New york
this counties are only for massachusetts
let's say in 'm1' we store leaflet
and we add tiles
let say the first one is open railway map
and then we can add provider tiles
so let's go for "HERE"
for the second map
where we can say group equals HERE
this we connect to
so I am going to connect
add circle markers part
and copy it here
similarly we can copy the New york part
for add layer control I am going to first use
base groups
let's bring this down
so we had open railway map
so let's copy this here
and HERE
and for overlay groups we already
have massachusetts and New york
so all this will be saved in 'm1'
so let's run this so we need
coma here
we get this map and you can see the toggle
gives us more options so we can
toggle between two different types of base maps
and then it also allows us to choose massachusetts
or New york
so when we zoom in and zoom out
sometimes it looks odd because
everything is over crowded here
so we can make use of cluster
features to take care of that
so let's use radius, label
so we can use this cluster options
and say marker
cluster options
when you zoom in and zoom out you'll see that
they get clustered and
present a much better picture
to summarize in this four videos series
we covered geographic plots in R
we started by looking at very basic world and US map
and then created US map
with COVID data that was followed by
a US map that included each county
in the country and finally in this last video
we focused on creating maps involving two states
massachusetts and New York
and we looked at various features and options
that we can use