Example 2: Raster Data Analysis
R
# Load required packages library (raster) # Read a raster dataset (elevation data) elevation <- raster ( system.file ( "external/test.grd" , package= "raster" )) # Compute statistics on the elevation data elev_summary <- summary (elevation) # Display the summary statistics print (elev_summary) |
Output:
test
Min. 138.7071
1st Qu. 293.9575
Median 371.9001
3rd Qu. 501.0102
Max. 1736.0580
NA's 6022.0000
In this example, we use the raster package to read a raster dataset containing elevation data. We then compute summary statistics, such as minimum, maximum, and mean elevation, using the summary() function. The result, elev_summary, provides information about the elevation dataset.
Plot the graph for Geospatial Data in R
R
# Install and load necessary packages if you haven't already install.packages ( "ggplot2" ) install.packages ( "maps" ) install.packages ( "plotly" ) library (ggplot2) library (maps) library (plotly) # Load earthquake data from the 'quakes' dataset data (quakes) # Create a basic map of earthquake occurrences world_map <- map_data ( "world" ) ggplot () + geom_polygon (data = world_map, aes (x = long, y = lat, group = group), fill = "white" , color = "black" ) + geom_point (data = quakes, aes (x = long, y = lat, size = mag, color = depth), alpha = 0.7) + scale_size_continuous (range = c (1, 10)) + scale_color_gradient (low = "blue" , high = "red" ) + labs ( title = "Global Earthquake Occurrences" , subtitle = "Magnitude and Depth" , x = "" , y = "" ) + theme_void () + theme (plot.title = element_text (hjust = 0.5, size = 18), plot.subtitle = element_text (hjust = 0.5, size = 14)) # Make the plot interactive using plotly earthquake_plot <- ggplotly () # Display the interactive plot earthquake_plot |
Output:
- ggplot(): We initialize the plot.
- geom_polygon(): We add a layer for drawing the world map with white fill and black borders.
- geom_point(): We add a layer for plotting earthquake occurrences as points. We map the longitude (long) to the x-axis, latitude (lat) to the y-axis, magnitude (mag) to the size of points, and depth (depth) to the color of points. The alpha parameter controls the transparency of points.
- scale_size_continuous(): We customize the size range of the points.
- scale_color_gradient(): We customize the color scale of the points from blue (low depth) to red (high depth).
- labs(): We set the plot title, subtitle, and axis labels.
- theme_void(): We use a minimal theme with no background.
- theme(plot.title): We further customize the appearance of the plot by adjusting the title’s size and position.
Geospatial Data Analysis with R
Geospatial data analysis involves working with data that has a geographic or spatial component. It allows us to analyze and visualize data in the context of its location on the Earth’s surface. R Programming Language is a popular open-source programming language, that offers a wide range of packages and tools for geospatial data analysis.