Fill color to the map
R
# Load required packages library (ggplot2) library (maps) # Load and plot world map data world <- map_data ( "world" ) ggplot (data = world, aes (x = long, y = lat, group = group)) + geom_polygon (fill = "lightblue" , color = "black" ) + coord_fixed (ratio = 1) + labs (title = "World Map" ) |
Output:
- We create a ggplot object and specify the aesthetics with aes. We use long and lat for longitude and latitude, respectively, and group to group the data for polygons.
- We use geom_polygon to plot the world map polygons with a light blue fill and black borders.
- coord_fixed(ratio = 1) ensures that the aspect ratio of the map is preserved.
- labs(title = “World Map”) sets the title of the plot to “World Map.”
Making Static & Interactive Maps With ggvis
In R Programming Language data visualization, making static and interactive maps is a common activity. Although the R package ggvis allows for interactive data visualizations, map visualizations are not one of its natural capabilities. For static maps and interactive maps, respectively, we commonly utilize tools like ggplot2 and leaflet.
In this article, we will check how to use ggplot2 and leaflet in R to generate both static and interactive maps.