Marc Kupietz | 9d57d4b | 2019-09-25 20:02:37 +0200 | [diff] [blame] | 1 | #!/usr/bin/env Rscript |
| 2 | # |
| 3 | # Plot frequency of an expressions under multiple conditions over time |
| 4 | # |
| 5 | #library(devtools) |
| 6 | #install_git("https://korap.ids-mannheim.de/gerrit/KorAP/RKorAPClient", upgrade="never") |
| 7 | library(RKorAPClient) |
| 8 | library(ggplot2) |
| 9 | library(reshape2) |
| 10 | #library(plotly) |
| 11 | |
| 12 | conditionsOverTime <- function(query, conditions, years, kco = new("KorAPConnection", verbose = TRUE)) { |
Marc Kupietz | 296e493 | 2019-10-04 22:51:11 +0200 | [diff] [blame] | 13 | g <- expand_grid(condition = conditions, year = years) %>% |
| 14 | cbind(frequencyQuery(kco, "[tt/l=Heuschrecke]", sprintf("%s & pubDate in %d", .$condition, .$year))) %>% |
| 15 | ggplot(aes(x = year, y = f, fill=condition, color=condition)) + |
Marc Kupietz | 9d57d4b | 2019-09-25 20:02:37 +0200 | [diff] [blame] | 16 | geom_point() + |
| 17 | geom_line() + |
Marc Kupietz | 296e493 | 2019-10-04 22:51:11 +0200 | [diff] [blame] | 18 | geom_ribbon(aes(ymin=conf.low, ymax=conf.high, fill=condition, color=condition), alpha=.3, linetype=0) + |
Marc Kupietz | 9d57d4b | 2019-09-25 20:02:37 +0200 | [diff] [blame] | 19 | xlab("TIME") + |
| 20 | labs(color="Virtual Corpus", fill="Virtual Corpus") + |
Marc Kupietz | e457d99 | 2019-09-29 18:17:05 +0200 | [diff] [blame] | 21 | ylab(sprintf("Observed frequency of \u201c%s\u201d", query)) + |
Marc Kupietz | 9d57d4b | 2019-09-25 20:02:37 +0200 | [diff] [blame] | 22 | theme(axis.text.x = element_text(angle = 45, hjust = 1)) + scale_x_continuous(breaks=unique(df$year)) |
| 23 | print(g) |
| 24 | # print(ggplotly(g, tooltip = c("x", "y"))) |
Marc Kupietz | 9d57d4b | 2019-09-25 20:02:37 +0200 | [diff] [blame] | 25 | } |
Marc Kupietz | 9d57d4b | 2019-09-25 20:02:37 +0200 | [diff] [blame] | 26 | #df <- conditionsOverTime("wegen dem [tt/p=NN]", c("textClass = /sport.*/", "textClass=/politik.*/", "textClass=/kultur.*/"), (1995:2005)) |
Marc Kupietz | 296e493 | 2019-10-04 22:51:11 +0200 | [diff] [blame] | 27 | |
| 28 | conditionsOverTime("[tt/l=Heuschrecke]", c("textClass = /natur.*/", "textClass=/politik.*/", "textClass=/wirtschaft.*/"), (2002:2018)) |