Utilizing the mapview() function in R to produce maps

Introduction:

Mapview() in R is a versatile tool for creating interactive and informative maps. This function empowers users to visualize geographic data dynamically, making it invaluable for GIS professionals, data scientists, and anyone interested in spatial data exploration. In this blog, we’ll explore mapview()’s capabilities, enabling you to harness its potential for compelling map visualizations that enhance your data-driven narratives.

Understanding mapview():

The mapview() function is a powerful R package that simplifies the process of visualizing spatial data by creating interactive maps. It’s particularly useful for working with geographic information systems (GIS) data, allowing users to explore and analyze data in a dynamic and engaging way. With mapview(), you can easily plot spatial datasets, overlay multiple layers, customize map attributes, and add interactive features such as zooming and panning.

Getting Started:

To get started, ensure you have the mapview package installed by using the following command for installation:
install.packages("mapview")
Once the package is successfully installed, proceed to import any additional necessary modules and begin the map creation process.

Importing data and libraries

library(tidyverse)
library(sf)
library(mapview)
df<-read.csv("C:/Users/SANKHYA/Downloads/data.csv",check.names = FALSE)
head(df)
        Date        States Regions latitude longitude Usage
1 02-01-2019        Punjab      NR 31.51997  75.98000 119.9
2 02-01-2019       Haryana      NR 28.45001  77.01999 130.3
3 02-01-2019     Rajasthan      NR 26.45000  74.63998 234.1
4 02-01-2019         Delhi      NR 28.66999  77.23000  85.8
5 02-01-2019 Uttar Pradesh      NR 27.59998  78.05001 313.9
6 02-01-2019   Uttarakhand      NR 30.32041  78.05001  40.7

 

#State wise Total consumption
Total_Usage<- df%>% group_by(States)%>% 
  summarise(Total_Usage=sum(Usage)) %>% arrange(desc(Total_Usage))
#Getting longitude & latitude for each State
State_LL<- df %>% distinct(States,longitude,latitude)
#Merging the 2 datasets
mapdata<-merge(State_LL,Total_Usage,by="States",all.x = TRUE)
#Convert the data frame to a spatial points data frame
`States Of India` = st_as_sf(mapdata,coords = c("longitude", "latitude"), crs = 4326)
#Plot the points on a map
mapview(`States Of India`, zcol = "States", label = paste0(`States Of India`$States,`States Of India`$Total_Usage))

 

In this example, we first aggregate the data to get the total usage, and then perform the following steps:
st_as_sf() converts a standard data frame (mapdata) into a spatial points data frame (States Of India), interpreting “longitude” and “latitude” as coordinates in the CRS (coordinate reference system) 4326.
mapview() then displays these spatial points on a map and labels them interactively with state names and total usage information.

Features of Mapview:

Interactive map viewing, Customizable map layers, Popups, Map saving, Augmenting Interactivity and Export Capabilities.

Conclusion:

The mapview() function in R empowers users to create interactive and multi-layered maps, revolutionizing the way spatial data is visualized and analyzed. With its versatile features and ease of use, it enhances the accessibility and impact of geographic data, making it an indispensable tool for professionals and researchers in various fields. This function not only simplifies complex spatial visualizations but also offers a dynamic platform for conveying insights effectively to diverse audiences.