blob: f056d583d5cc7cab3f616d7d7a79c76091b087d2 [file] [log] [blame]
Marc Kupietz451980d2019-09-23 23:45:10 +02001#!/usr/bin/Rscript
2library(RKorAPClient)
3library(ggplot2)
4library(raster)
5library(broom)
6
Marc Kupietze457d992019-09-29 18:17:05 +02007devAskNewPage(ask = FALSE)
8mapfile <- "demo/data/cache/map-v2.rds"
Marc Kupietz451980d2019-09-23 23:45:10 +02009
10fetchAndPrepareMap <- function(map, pick) {
11 cat("Downloading GADM map data for ", map, "\n")
12 sp <- readRDS(url(sprintf("https://biogeo.ucdavis.edu/data/gadm3.6/Rsp/gadm36_%s_sp.rds", map)))
13 if (pick > 0) {
14 sp@polygons <- sp@polygons[pick]
15 sp@data <- sp@data[pick,]
16 }
17 sp
18}
19
20fetchMaps <- function(maps, picks) {
21 if (file.exists(mapfile)) {
22 df <- readRDS(mapfile)
23 } else {
24 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")
25 df <- broom::tidy(Reduce(bind, mapply(fetchAndPrepareMap, maps, picks)))
26 dir.create(dirname(mapfile), recursive = TRUE, showWarnings = FALSE)
27 saveRDS(df, mapfile)
28 }
29 df$grp <- floor(as.numeric(as.character(df$group)))
30 df
31}
32
Marc Kupietzb1be8b42019-09-28 17:57:31 +020033map <- 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 +020034
35geoDistrib <- function(query, kco = new("KorAPConnection", verbose=TRUE)) {
Marc Kupietze457d992019-09-29 18:17:05 +020036 regions <- readRDS("demo/data/regions.rds")
Marc Kupietz451980d2019-09-23 23:45:10 +020037 regions$freq <- NA
Marc Kupietz9402dec2019-09-28 22:29:30 +020038 regions$url <- NA
Marc Kupietz451980d2019-09-23 23:45:10 +020039 plot <- NULL
40 vc <- ""
41 for (i in 1:nrow(regions)) {
42 if (!is.na(regions[i,]$query)) {
Marc Kupietzb1be8b42019-09-28 17:57:31 +020043 cat(as.character(regions[i,]$region), "\n")
Marc Kupietz451980d2019-09-23 23:45:10 +020044 regions[i,]$total <- corpusStats(kco, vc=paste0(vc, regions[i,]$query))@tokens
45 if (regions[i,]$total == 0) {
46 regions[i,]$afreq <- 0
47 regions[i,]$freq <- NA
48 } else {
Marc Kupietz9402dec2019-09-28 22:29:30 +020049 kqo <- corpusQuery(kco, query, vc=paste0(vc, regions[i,]$query))
50 regions[i,]$afreq <- kqo@totalResults
Marc Kupietz451980d2019-09-23 23:45:10 +020051 regions[i,]$freq <- regions[i,]$afreq / regions[i,]$total
Marc Kupietz9402dec2019-09-28 22:29:30 +020052 regions[i,]$url <- kqo@webUIRequestUrl
Marc Kupietz451980d2019-09-23 23:45:10 +020053 }
54 cat(regions[i,]$afreq, regions[i,]$total, regions[i,]$freq, "\n")
Marc Kupietz451980d2019-09-23 23:45:10 +020055 cat("\n\n")
56 }
57 }
Marc Kupietz3da02eb2019-10-04 09:15:00 +020058 plot <- updatePlot(query, map, regions)
Marc Kupietz5fb892e2021-03-05 08:18:25 +010059 print(plot)
60 plot
Marc Kupietz451980d2019-09-23 23:45:10 +020061}
62
Marc Kupietz9402dec2019-09-28 22:29:30 +020063updatePlot <- function(query, map, regions) {
64 map$ipm <- sapply(map$grp, function(grp) regions$freq[grp] * 10^6)
65 map$region <- sapply(map$grp, function(grp) regions$region[grp])
66 map$url <- sapply(map$grp, function(grp) regions$url[grp])
Marc Kupietz451980d2019-09-23 23:45:10 +020067 regionsPlot <- ggplot(map) +
Marc Kupietz69cc54a2019-09-30 12:06:54 +020068 geom_polygon(aes(x=long, y=lat, group=group, fill=ipm, hack=region), colour= "black", size=.1) +
Marc Kupietz451980d2019-09-23 23:45:10 +020069 theme(axis.line.x = element_blank(),
70 axis.line.y = element_blank(),
71 panel.grid.major = element_blank(),
72 panel.grid.minor = element_blank(),
73 panel.border = element_blank(),
74 panel.background = element_blank(),
75 axis.line=element_blank(),axis.text.x=element_blank(),
76 axis.text.y=element_blank(),axis.ticks=element_blank(),
77 axis.title.x=element_blank(),
78 axis.title.y=element_blank()) +
79 coord_equal(ratio=1.5) +
Marc Kupietze457d992019-09-29 18:17:05 +020080 labs(title = sprintf("Regional distribution of \u201c%s\u201d", query))
Marc Kupietz451980d2019-09-23 23:45:10 +020081 print(regionsPlot)
82 regionsPlot
83}
84
85#geoDistrib("wegen dem [tt/p=NN]")
86geoDistrib("heuer")
87#geoDistrib("Sonnabend")
88#geoDistrib("eh")