Adrian
Adrian

Reputation: 843

Measure Country's Distance from the Equator using a Shapefile

I want to measure the distance of countries position relative to the equator, and I got the idea to do this using a shape-file with R. I do not know how feasible this is, or whether it is possible at all, but I do know that it is possible to measure distance.

Using the shape-files from the EU, I managed to import the map into R:

library(sf)
library(tidyverse)

map <- read_sf(dsn = "C:/Users/Adrian/Desktop/Data/ne_50m_admin_0_countries", 
               layer = "ne_50m_admin_0_countries")

ggplot(data = map_world) +
  geom_sf()

I would next draw a line through the 0° latitude and then measure the vertical distance between country and equator line. Something like this for each country (apologies for my crude drawing):

enter image description here

Next the measurement of distance between country and equator line. Ideally, the calculation would yield a panel data with country and distance.

Would this be possible?

Upvotes: 3

Views: 327

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 173793

Let us obtain a world map shapefile using the rnaturalearth package:

library(sf)
library(rnaturalearth)
library(tidyverse)

map_world <- ne_countries(returnclass = 'sf')

To simplify the example, let's choose a few different countries:

countries <- c("United States of America",
               "United Kingdom",
               "Australia",
               "Argentina",
               "Chad")

Now we filter out these countries and find their centroids. We then generate an extra sfc column that is the projection of the centroids onto the equator. Finally, we measure the distance between the centroids and the equator points:

country_points <- map_world %>% 
  filter(admin %in% countries) %>% 
  st_centroid() %>%
  bind_cols(st_coordinates(.)) %>%
  select(admin, X, Y) %>%
  rowwise() %>%
  mutate(equator = st_sfc(lapply(X, function(x) st_point(c(x, 0))),
                          crs = st_crs(map_world))) %>%
  mutate(distance_equator = st_distance(geometry, equator)[,1]) %>%
  select(-equator)

This results in the distance measurements of all the countries' centroids from the equator:

country_points
#> Simple feature collection with 5 features and 4 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: -103.5729 ymin: -34.73683 xmax: 134.3143 ymax: 53.8083
#> CRS:           +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
#> # A tibble: 5 x 5
#> # Rowwise: 
#>   admin                          X     Y              geometry distance_equator
#>   <chr>                      <dbl> <dbl>           <POINT [°]>              [m]
#> 1 Argentina                 -64.8  -34.7 (-64.75442 -34.73683)         3862566.
#> 2 Australia                 134.   -25.8   (134.3143 -25.7631)         2864730.
#> 3 United Kingdom             -2.76  53.8   (-2.759269 53.8083)         5983219.
#> 4 Chad                       18.6   15.3   (18.57333 15.27628)         1698647.
#> 5 United States of America -104.    44.8  (-103.5729 44.75598)         4976646.

Furthermore, we can plot the result like this:

ggplot(data = map_world) +
  geom_sf() +
  geom_hline(yintercept = 0, color = 'red', size = 1) +
  geom_segment(data = country_points, size = 1,
               aes(X, Y, xend = X, yend = 0), color = 'red')

Created on 2022-10-27 with reprex v2.0.2

Upvotes: 2

Related Questions