blob: 5d62d78c02593fc0a1983059d813e933959b0e2c [file] [log] [blame]
Marc Kupietz451980d2019-09-23 23:45:10 +02001#!/usr/bin/Rscript
2library(RKorAPClient)
3library(ggplot2)
4library(raster)
5library(broom)
Marc Kupietzca6c4f62022-09-06 10:40:50 +02006library(R.cache)
Marc Kupietz451980d2019-09-23 23:45:10 +02007
Marc Kupietze457d992019-09-29 18:17:05 +02008devAskNewPage(ask = FALSE)
Marc Kupietzca6c4f62022-09-06 10:40:50 +02009mapfile <- file.path(R.cache::getCachePath(), "map-v2.rds")
Marc Kupietz451980d2019-09-23 23:45:10 +020010
11fetchAndPrepareMap <- function(map, pick) {
12 cat("Downloading GADM map data for ", map, "\n")
13 sp <- readRDS(url(sprintf("https://biogeo.ucdavis.edu/data/gadm3.6/Rsp/gadm36_%s_sp.rds", map)))
14 if (pick > 0) {
15 sp@polygons <- sp@polygons[pick]
16 sp@data <- sp@data[pick,]
17 }
18 sp
19}
20
21fetchMaps <- function(maps, picks) {
22 if (file.exists(mapfile)) {
23 df <- readRDS(mapfile)
24 } else {
25 cat("Downloading and caching GADM map data.\nPlease note that the GADM map data is licensed for academic use and other non-commercial use, only.\nSee https://gadm.org/license.html\n")
26 df <- broom::tidy(Reduce(bind, mapply(fetchAndPrepareMap, maps, picks)))
27 dir.create(dirname(mapfile), recursive = TRUE, showWarnings = FALSE)
28 saveRDS(df, mapfile)
29 }
30 df$grp <- floor(as.numeric(as.character(df$group)))
31 df
32}
33
Marc Kupietzb1be8b42019-09-28 17:57:31 +020034map <- fetchMaps(c("DEU_1", "AUT_0", "CHE_0", "LUX_0", "BEL_3", "ITA_1", "LIE_0"), c(0, 0, 0, 0, 34, 17, 0))
Marc Kupietz451980d2019-09-23 23:45:10 +020035
36geoDistrib <- function(query, kco = new("KorAPConnection", verbose=TRUE)) {
Marc Kupietze457d992019-09-29 18:17:05 +020037 regions <- readRDS("demo/data/regions.rds")
Marc Kupietz451980d2019-09-23 23:45:10 +020038 regions$freq <- NA
Marc Kupietz9402dec2019-09-28 22:29:30 +020039 regions$url <- NA
Marc Kupietz451980d2019-09-23 23:45:10 +020040 plot <- NULL
41 vc <- ""
42 for (i in 1:nrow(regions)) {
43 if (!is.na(regions[i,]$query)) {
Marc Kupietzb1be8b42019-09-28 17:57:31 +020044 cat(as.character(regions[i,]$region), "\n")
Marc Kupietz451980d2019-09-23 23:45:10 +020045 regions[i,]$total <- corpusStats(kco, vc=paste0(vc, regions[i,]$query))@tokens
46 if (regions[i,]$total == 0) {
47 regions[i,]$afreq <- 0
48 regions[i,]$freq <- NA
49 } else {
Marc Kupietz9402dec2019-09-28 22:29:30 +020050 kqo <- corpusQuery(kco, query, vc=paste0(vc, regions[i,]$query))
51 regions[i,]$afreq <- kqo@totalResults
Marc Kupietz451980d2019-09-23 23:45:10 +020052 regions[i,]$freq <- regions[i,]$afreq / regions[i,]$total
Marc Kupietz9402dec2019-09-28 22:29:30 +020053 regions[i,]$url <- kqo@webUIRequestUrl
Marc Kupietz451980d2019-09-23 23:45:10 +020054 }
55 cat(regions[i,]$afreq, regions[i,]$total, regions[i,]$freq, "\n")
Marc Kupietz451980d2019-09-23 23:45:10 +020056 cat("\n\n")
57 }
58 }
Marc Kupietz3da02eb2019-10-04 09:15:00 +020059 plot <- updatePlot(query, map, regions)
Marc Kupietz5fb892e2021-03-05 08:18:25 +010060 print(plot)
61 plot
Marc Kupietz451980d2019-09-23 23:45:10 +020062}
63
Marc Kupietz9402dec2019-09-28 22:29:30 +020064updatePlot <- function(query, map, regions) {
65 map$ipm <- sapply(map$grp, function(grp) regions$freq[grp] * 10^6)
66 map$region <- sapply(map$grp, function(grp) regions$region[grp])
67 map$url <- sapply(map$grp, function(grp) regions$url[grp])
Marc Kupietz451980d2019-09-23 23:45:10 +020068 regionsPlot <- ggplot(map) +
Marc Kupietz69cc54a2019-09-30 12:06:54 +020069 geom_polygon(aes(x=long, y=lat, group=group, fill=ipm, hack=region), colour= "black", size=.1) +
Marc Kupietz451980d2019-09-23 23:45:10 +020070 theme(axis.line.x = element_blank(),
71 axis.line.y = element_blank(),
72 panel.grid.major = element_blank(),
73 panel.grid.minor = element_blank(),
74 panel.border = element_blank(),
75 panel.background = element_blank(),
76 axis.line=element_blank(),axis.text.x=element_blank(),
77 axis.text.y=element_blank(),axis.ticks=element_blank(),
78 axis.title.x=element_blank(),
79 axis.title.y=element_blank()) +
80 coord_equal(ratio=1.5) +
Marc Kupietze457d992019-09-29 18:17:05 +020081 labs(title = sprintf("Regional distribution of \u201c%s\u201d", query))
Marc Kupietz451980d2019-09-23 23:45:10 +020082 print(regionsPlot)
83 regionsPlot
84}
85
86#geoDistrib("wegen dem [tt/p=NN]")
87geoDistrib("heuer")
88#geoDistrib("Sonnabend")
89#geoDistrib("eh")