In this assignment I created a map which shows several locations on a map. The locations have different color and sizes based on the size of the location.
I want to use this map at work with a bigger dataset. For this assignment, I only included a few locations and removed some privacy sensitive data. The general use of this map is therefore not very interesting. Nonetheless, it gives an impression of how data can be visualised on with Leaflet and how to use coordinates in “Rijksdriehoekscoordinate”-format in Leaflet (this a frequently used coordinate system in the Netherlands).
I loaded a dataframe (df) with 50 locations and location size attributes. The coordinats are in the Dutch Rijksdriehoekscoordinate format (http://epsg.io/28992#). This format cannot be shown in Leaflet with default settings. The center of this coordinate system is in the spire of the ‘O.L. Vrouwe’-church in Amersfoort, instead of in Greenwich (UK).
The coordinates can be converted to normal longitude and latitude coordinates with the following code (note that there are many other projections that could be converted in a similar way):
df_sp <- SpatialPointsDataFrame(coords = df[2:3],
data = df[1:1], proj4string = CRS("+init=epsg:28992")) #make coordinate point from dataframe
df.sp.4326 = spTransform(df_sp, CRS("+init=epsg:4326")) #convert coordinates from Amersfoort to Lat/Long projection (EPSG=4326).
After the conversions, I also did some calculations to be able to set the marker color/size based on size of the location.
Finaly show the locations in open streetmap with a legend. You can zoom into the map to get a better look.
##show the map
my_map <- leaflet()%>%addTiles()%>%
addCircleMarkers(lat=df.sp.4326$y, lng=df.sp.4326$x,
radius=df.sp.4326$sizeNumber, color=df.sp.4326$sizeColor)%>%
addLegend(labels=c("Unknown","Small","Medium","Large"),
color=c("yellow","gold","chocolate","red"))
my_map