Marc Kupietz | 451980d | 2019-09-23 23:45:10 +0200 | [diff] [blame] | 1 | #!/usr/bin/Rscript |
| 2 | library(RKorAPClient) |
| 3 | library(ggplot2) |
| 4 | library(raster) |
| 5 | library(broom) |
Marc Kupietz | 35eecca | 2022-09-07 10:45:42 +0200 | [diff] [blame] | 6 | # library(R.cache) |
Marc Kupietz | 451980d | 2019-09-23 23:45:10 +0200 | [diff] [blame] | 7 | |
Marc Kupietz | e457d99 | 2019-09-29 18:17:05 +0200 | [diff] [blame] | 8 | devAskNewPage(ask = FALSE) |
Marc Kupietz | 35eecca | 2022-09-07 10:45:42 +0200 | [diff] [blame] | 9 | |
| 10 | mapfile <- file.path(tempdir(), "map-v2.rds") |
| 11 | |
| 12 | # Caching data in the user's home filespace by default |
| 13 | # is not allowed to package demos by CRAN policies ... |
| 14 | # |
| 15 | # mapfile <- file.path(R.cache::getCachePath(), "map-v2.rds") |
Marc Kupietz | 451980d | 2019-09-23 23:45:10 +0200 | [diff] [blame] | 16 | |
| 17 | fetchAndPrepareMap <- function(map, pick) { |
| 18 | cat("Downloading GADM map data for ", map, "\n") |
| 19 | sp <- readRDS(url(sprintf("https://biogeo.ucdavis.edu/data/gadm3.6/Rsp/gadm36_%s_sp.rds", map))) |
| 20 | if (pick > 0) { |
| 21 | sp@polygons <- sp@polygons[pick] |
| 22 | sp@data <- sp@data[pick,] |
| 23 | } |
| 24 | sp |
| 25 | } |
| 26 | |
| 27 | fetchMaps <- function(maps, picks) { |
| 28 | if (file.exists(mapfile)) { |
| 29 | df <- readRDS(mapfile) |
| 30 | } else { |
| 31 | 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") |
| 32 | df <- broom::tidy(Reduce(bind, mapply(fetchAndPrepareMap, maps, picks))) |
| 33 | dir.create(dirname(mapfile), recursive = TRUE, showWarnings = FALSE) |
| 34 | saveRDS(df, mapfile) |
| 35 | } |
| 36 | df$grp <- floor(as.numeric(as.character(df$group))) |
| 37 | df |
| 38 | } |
| 39 | |
Marc Kupietz | b1be8b4 | 2019-09-28 17:57:31 +0200 | [diff] [blame] | 40 | map <- 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 Kupietz | 451980d | 2019-09-23 23:45:10 +0200 | [diff] [blame] | 41 | |
| 42 | geoDistrib <- function(query, kco = new("KorAPConnection", verbose=TRUE)) { |
Marc Kupietz | e457d99 | 2019-09-29 18:17:05 +0200 | [diff] [blame] | 43 | regions <- readRDS("demo/data/regions.rds") |
Marc Kupietz | 451980d | 2019-09-23 23:45:10 +0200 | [diff] [blame] | 44 | regions$freq <- NA |
Marc Kupietz | 9402dec | 2019-09-28 22:29:30 +0200 | [diff] [blame] | 45 | regions$url <- NA |
Marc Kupietz | 451980d | 2019-09-23 23:45:10 +0200 | [diff] [blame] | 46 | plot <- NULL |
| 47 | vc <- "" |
| 48 | for (i in 1:nrow(regions)) { |
| 49 | if (!is.na(regions[i,]$query)) { |
Marc Kupietz | b1be8b4 | 2019-09-28 17:57:31 +0200 | [diff] [blame] | 50 | cat(as.character(regions[i,]$region), "\n") |
Marc Kupietz | 451980d | 2019-09-23 23:45:10 +0200 | [diff] [blame] | 51 | regions[i,]$total <- corpusStats(kco, vc=paste0(vc, regions[i,]$query))@tokens |
| 52 | if (regions[i,]$total == 0) { |
| 53 | regions[i,]$afreq <- 0 |
| 54 | regions[i,]$freq <- NA |
| 55 | } else { |
Marc Kupietz | 9402dec | 2019-09-28 22:29:30 +0200 | [diff] [blame] | 56 | kqo <- corpusQuery(kco, query, vc=paste0(vc, regions[i,]$query)) |
| 57 | regions[i,]$afreq <- kqo@totalResults |
Marc Kupietz | 451980d | 2019-09-23 23:45:10 +0200 | [diff] [blame] | 58 | regions[i,]$freq <- regions[i,]$afreq / regions[i,]$total |
Marc Kupietz | 9402dec | 2019-09-28 22:29:30 +0200 | [diff] [blame] | 59 | regions[i,]$url <- kqo@webUIRequestUrl |
Marc Kupietz | 451980d | 2019-09-23 23:45:10 +0200 | [diff] [blame] | 60 | } |
| 61 | cat(regions[i,]$afreq, regions[i,]$total, regions[i,]$freq, "\n") |
Marc Kupietz | 451980d | 2019-09-23 23:45:10 +0200 | [diff] [blame] | 62 | cat("\n\n") |
| 63 | } |
| 64 | } |
Marc Kupietz | 3da02eb | 2019-10-04 09:15:00 +0200 | [diff] [blame] | 65 | plot <- updatePlot(query, map, regions) |
Marc Kupietz | 5fb892e | 2021-03-05 08:18:25 +0100 | [diff] [blame] | 66 | print(plot) |
| 67 | plot |
Marc Kupietz | 451980d | 2019-09-23 23:45:10 +0200 | [diff] [blame] | 68 | } |
| 69 | |
Marc Kupietz | 9402dec | 2019-09-28 22:29:30 +0200 | [diff] [blame] | 70 | updatePlot <- function(query, map, regions) { |
| 71 | map$ipm <- sapply(map$grp, function(grp) regions$freq[grp] * 10^6) |
| 72 | map$region <- sapply(map$grp, function(grp) regions$region[grp]) |
| 73 | map$url <- sapply(map$grp, function(grp) regions$url[grp]) |
Marc Kupietz | 451980d | 2019-09-23 23:45:10 +0200 | [diff] [blame] | 74 | regionsPlot <- ggplot(map) + |
Marc Kupietz | 5e026d4 | 2023-08-31 14:28:43 +0200 | [diff] [blame^] | 75 | geom_polygon(aes(x=long, y=lat, group=group, fill=ipm, hack=region), colour= "black", linewidth=.1) + |
Marc Kupietz | 451980d | 2019-09-23 23:45:10 +0200 | [diff] [blame] | 76 | theme(axis.line.x = element_blank(), |
| 77 | axis.line.y = element_blank(), |
| 78 | panel.grid.major = element_blank(), |
| 79 | panel.grid.minor = element_blank(), |
| 80 | panel.border = element_blank(), |
| 81 | panel.background = element_blank(), |
| 82 | axis.line=element_blank(),axis.text.x=element_blank(), |
| 83 | axis.text.y=element_blank(),axis.ticks=element_blank(), |
| 84 | axis.title.x=element_blank(), |
| 85 | axis.title.y=element_blank()) + |
| 86 | coord_equal(ratio=1.5) + |
Marc Kupietz | e457d99 | 2019-09-29 18:17:05 +0200 | [diff] [blame] | 87 | labs(title = sprintf("Regional distribution of \u201c%s\u201d", query)) |
Marc Kupietz | 451980d | 2019-09-23 23:45:10 +0200 | [diff] [blame] | 88 | print(regionsPlot) |
| 89 | regionsPlot |
| 90 | } |
| 91 | |
| 92 | #geoDistrib("wegen dem [tt/p=NN]") |
| 93 | geoDistrib("heuer") |
| 94 | #geoDistrib("Sonnabend") |
| 95 | #geoDistrib("eh") |