Skip to content

Instantly share code, notes, and snippets.

@cavedave
Last active January 1, 2020 09:22
Show Gist options
  • Save cavedave/ed66f1961e144adb14c9898e58b42ff7 to your computer and use it in GitHub Desktop.
Save cavedave/ed66f1961e144adb14c9898e58b42ff7 to your computer and use it in GitHub Desktop.
European Centers of population Data from

Find out median center of population for European countries. Data is from eurostat https://ec.europa.eu/eurostat/web/gisco/geodata/reference-data/population-distribution-demography/geostat where they give number of people in each square kilometer in Europe

Which I discovered from this cool r package graph of european population density https://gist.github.com/cpsievert/7dd28a478b4c051180d802321353259d

GRD_ID contains north and east co-ordinates.

conversion done on https://epsg.io/transform#s_srs=3035&t_srs=4326&x=5214000.0000000&y=2224000.0000000 from Input coordinate system EPSG:3035 ETRS89 / LAEA Europe this input system needs to have 000 added to it to move form square km in the data to square m in the output mapping to Output coordinate system EPSG:4326 WGS 84

eur<-read_csv('GEOSTAT_grid_POP_1K_2011_V2_0_1.csv')
#two ways to extract east and north
eur$North <-str_match(eur$GRD_ID, "1kmN(\\d\\d\\d\\d)E(\\d\\d\\d\\d)")[,2]
eur$East <-str_match(eur$GRD_ID, "1kmN(\\d\\d\\d\\d)E(\\d\\d\\d\\d)")[,3]
eur<-eur%>%
mutate(
lat = as.numeric(gsub('.*N([0-9]+)[EW].*', '\\1', GRD_ID))/100,
lng = as.numeric(gsub('.*[EW]([0-9]+)', '\\1', GRD_ID)) * ifelse(gsub('.*([EW]).*', '\\1', GRD_ID) == 'W', -1, 1) / 100
)
head(eur)
#get each countries population total
popu<-aggregate(eur$TOT_P, by=list(Category=eur$CNTR_CODE), FUN=sum)
for (row in 1:nrow(popu)) {
country <- popu[row, "Category"]
TOT_P <- popu[row, "x"]
}
#now loop through each contry and find the North and east km with half the people either side
for (row in 1:nrow(popu)) {
country <- popu[row, "Category"]
TOT_P <- popu[row, "x"]
Coun<-filter(eur, DATA_SRC == country)
#stop when you have half the population
thresholdValue <- (TOT_P/2)
#head east until you have counted half the people
Coun<-Coun[order(Coun$East),]
ix <- length(which(cumsum(Coun$TOT_P) <= thresholdValue))
first<-Coun[ix,]$East
#head south until you have counted half the people
Coun<-Coun[order(Coun$North),]
iy <- length(which(cumsum(Coun$TOT_P) <= thresholdValue))
cat(country, first ,strtoi(Coun[iy,]$North))
cat("\n")
}
library("ggplot2")
theme_set(theme_bw())
library("sf")
library(tmap)
library("rnaturalearth")
library("rnaturalearthdata")
world <- ne_countries(scale = "medium", returnclass = "sf")
class(world)
ggplot(data = world) +
geom_sf() +
coord_sf(xlim = c(-15, 35), ylim = c(35, 72), expand = FALSE)+
#al 19.80 41.28
annotate("point", x = 19.80, y =41.28, colour = "red", size = 1.1)+
#AT 4717 2777 15.30 47.97
annotate("point", x = 15.30, y =47.97, colour = "red", size = 1.1)+
#BE 3927 3099 4.39
annotate("point", x = 4.39, y =50.86, colour = "red", size = 1.1)+
#BG 5537 2285 24.87 42.58
annotate("point", x = 24.87, y =42.58, colour = "red", size = 1.1)+
#CH 4174 2673
annotate("point", x = 8.06, y =47.15, colour = "red", size = 1.1)+
#CZ 4698 2997
annotate("point", x = 15.25, y =49.96, colour = "red", size = 1.1)+
#DE 4274 3103
annotate("point", x = 9.329, y =51.036, colour = "red", size = 1.1)+
#DK 4349 3625
annotate("point", x = 10.445, y =55.728, colour = "red", size = 1.1)+
#EE 5166 4117
annotate("point", x = 24.949, y =59.349, colour = "red", size = 1.1)+
#EL 5513 1773
annotate("point", x = 23.569, y =38.082, colour = "red", size = 1.1)+
#ES 3169 2025
annotate("point", x = -3.583, y =40.389, colour = "red", size = 1.1)+
#FI 5128 4313
annotate("point", x = 25.064, y =61.139, colour = "red", size = 1.1)+
#FR 3765 2775
annotate("point", x = 2.562, y =47.836, colour = "red", size = 1.1)+
#HR 4803 2515
annotate("point", x = 16.173, y =45.567, colour = "red", size = 1.1)+
#HU 5007 2747
annotate("point", x = 19.117, y =47.455, colour = "red", size = 1.1)+
#ireland x=-7.063484&y=53.208182
annotate("point", x = -7.063, y =53.208, colour = "red", size = 1.1)+
#IT 4493 2288
annotate("point", x = 12.128, y =43.679, colour = "red", size = 1.1)+
#LI 4284 2672
annotate("point", x = 9.512, y =47.16, colour = "red", size = .6)+
# LT 5210 3632
annotate("point", x = 23.993, y =55.013, colour = "red", size = 1.1)+
#LV 5178 3843
annotate("point", x = 24.166, y =56.923, colour = "red", size = 1.1)+
#MT 4728 1437
annotate("point", x = 14.467, y =35.886, colour = "red", size = .6)+
#NL 3991 3230
annotate("point", x = 5.182, y =52.081, colour = "red", size = 1.1)+
# NO 4343 4094
annotate("point", x = 10.392, y =59.946, colour = "red", size = 1.1)+
#PL 4968 3213
annotate("point", x = 19.378, y =51.652, colour = "red", size = 1.1)+
#PT 2755 2066
annotate("point", x = -8.436, y =39.964, colour = "red", size = 1.1)+
#RO 5531 2616
annotate("point", x = 25.591, y =45.510, colour = "red", size = 1.1)+
#SE 4640 4013
annotate("point", x = 15.562, y =59.105, colour = "red", size = 1.1)+
#SI 4694 2571
annotate("point", x = 14.825, y =46.143, colour = "red", size = 1.1)+
#SK 4965 2889
annotate("point", x = 18.781, y =48.768, colour = "red", size = 1.1)+
#UK 3555 3319
annotate("point", x = -1.317, y =52.442, colour = "red", size = 1.1)+
#XK* 5214 2224 kosovo
annotate("point", x = 20.878, y =42.539, colour = "red", size = 1.1)+
#all countries 4224000 2891
annotate("point", x = 8.671, y =49.124, colour = "blue", size = 1.3)+
ggtitle("Center of Population of European Countries",
subtitle = "Half population is North and half East")+
theme(
axis.title.x = element_blank(),plot.title = element_text(hjust = 0.5),plot.subtitle = element_text(hjust = 0.5),
axis.title.y = element_blank(), axis.text.x = element_blank(),
axis.text.y = element_blank(),axis.ticks = element_blank())
ggsave("europe.png", width = 15, height = 20, units = "cm")
@cavedave
Copy link
Author

europe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment