Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 1 | #' Experimental: Plot interactive frequency by year graphs with confidence intervals using highcharter |
| 2 | #' |
| 3 | #' Experimental convenience function for plotting typical frequency by year graphs with confidence intervals using highcharter. |
| 4 | #' \bold{Warning:} This function may be moved to a new package. |
| 5 | #' |
| 6 | #' @import highcharter |
Marc Kupietz | b7e7f72 | 2020-06-02 12:29:18 +0200 | [diff] [blame] | 7 | #' @importFrom tibble add_column |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 8 | #' @export |
| 9 | #' |
| 10 | #' @param df data frame like the value of a \code{\link{frequencyQuery}} |
Marc Kupietz | 43a6ade | 2020-02-18 17:01:44 +0100 | [diff] [blame] | 11 | #' @param as.alternatives boolean decides whether queries should be treated as mutually exclusive and exhaustive wrt. to some meaningful class (e.g. spelling variants of a certain word form). |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 12 | #' @param ylabel defaults to \% if \code{as.alternatives} is \code{true} and to "ipm" otherwise. |
Marc Kupietz | ab0b071 | 2020-05-04 16:24:57 +0200 | [diff] [blame] | 13 | #' @param smooth boolean decides whether the graph is smoothed using the highcharts plot types spline and areasplinerange. |
Marc Kupietz | 5b503f4 | 2020-04-09 15:26:00 +0200 | [diff] [blame] | 14 | #' @param ... additional arguments passed to \code{\link{hc_add_series}} |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 15 | #' |
| 16 | #' @examples |
Marc Kupietz | 657d8e7 | 2020-02-25 18:31:50 +0100 | [diff] [blame] | 17 | #' \donttest{year <- c(1990:2018)}\dontshow{year <- c(2013:2013)} |
| 18 | #' \donttest{alternatives <- c("macht []{0,3} Sinn", "ergibt []{0,3} Sinn")}\dontshow{alternatives <- c("macht []{0,3} Sinn")} |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 19 | #' new("KorAPConnection", verbose = TRUE) %>% |
Marc Kupietz | 657d8e7 | 2020-02-25 18:31:50 +0100 | [diff] [blame] | 20 | #' frequencyQuery(query = alternatives, |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame] | 21 | #' vc = paste("textType = /Zeit.*/ & pubDate in", year), |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 22 | #' as.alternatives = TRUE) %>% |
| 23 | #' hc_freq_by_year_ci(as.alternatives = TRUE) |
| 24 | #' |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame] | 25 | #' \donttest{ |
Marc Kupietz | 10f65c4 | 2020-01-31 15:18:24 +0100 | [diff] [blame] | 26 | #' kco <- new("KorAPConnection", verbose = TRUE) |
| 27 | #' expand_grid( |
| 28 | #' condition = c("textDomain = /Wirtschaft.*/", "textDomain != /Wirtschaft.*/"), |
| 29 | #' year = (2005:2011) |
| 30 | #' ) %>% |
| 31 | #' cbind(frequencyQuery( |
| 32 | #' kco, |
| 33 | #' "[tt/l=Heuschrecke]", |
| 34 | #' paste0(.$condition, " & pubDate in ", .$year) |
| 35 | #' )) %>% |
| 36 | #' hc_freq_by_year_ci() |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame] | 37 | #' } |
Marc Kupietz | 10f65c4 | 2020-01-31 15:18:24 +0100 | [diff] [blame] | 38 | #' |
Marc Kupietz | ab0b071 | 2020-05-04 16:24:57 +0200 | [diff] [blame] | 39 | hc_freq_by_year_ci <- function(df, as.alternatives = FALSE, |
| 40 | ylabel = if(as.alternatives) "%" else "ipm", |
| 41 | smooth = FALSE, |
| 42 | ...) { |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 43 | title <- "" |
| 44 | df <- df %>% |
| 45 | { if(! as.alternatives) ipm(.) else RKorAPClient::percent(.) } |
| 46 | |
| 47 | if (!"year" %in% colnames(df)) { |
Marc Kupietz | b7e7f72 | 2020-06-02 12:29:18 +0200 | [diff] [blame] | 48 | df <- df %>% add_column(year = as.integer(queryStringToLabel(df$vc, pubDateOnly = TRUE))) |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 49 | } |
| 50 | if (!"condition" %in% colnames(df)) { |
| 51 | if (length(base::unique(df$query)) > 1) { |
| 52 | df <- df %>% mutate(condition = query) |
Marc Kupietz | cf1771d | 2020-03-04 16:03:04 +0100 | [diff] [blame] | 53 | if(length(base::unique(queryStringToLabel(df$vc, excludePubDate = TRUE ))) > 1) { |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 54 | df <- df %>% mutate(condition = paste(condition, " & ", |
Marc Kupietz | cf1771d | 2020-03-04 16:03:04 +0100 | [diff] [blame] | 55 | queryStringToLabel(vc, excludePubDate = TRUE ))) |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 56 | } |
| 57 | } else { |
Marc Kupietz | 5d70ffe | 2020-03-12 11:16:43 +0100 | [diff] [blame] | 58 | if (length(base::unique(queryStringToLabel(df$vc, excludePubDate = TRUE ))) > 1) { |
| 59 | title <- base::unique(df$query) |
Marc Kupietz | b7e7f72 | 2020-06-02 12:29:18 +0200 | [diff] [blame] | 60 | df <- df %>% add_column(condition = queryStringToLabel(vc, excludePubDate = TRUE )) |
Marc Kupietz | 5d70ffe | 2020-03-12 11:16:43 +0100 | [diff] [blame] | 61 | } else { |
| 62 | df <- df %>% mutate(condition = query) |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | } |
| 66 | # use the D3 palette which provides 20 attractive and distinguishable colours |
| 67 | palette <- c("#1F77B4", "#FF7F0E", "#2CA02C", "#D62728", "#9467BD", "#8C564B", "#E377C2", "#7F7F7F", "#BCBD22", "#17BECF", "#AEC7E8", "#FFBB78", "#98DF8A", "#FF9896", "#C5B0D5", "#C49C94", "#F7B6D2", "#C7C7C7", "#DBDB8D", "#9EDAE5") |
| 68 | highcharter::highchart() %>% |
| 69 | hc_title(text=title) %>% |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 70 | hc_yAxis( |
| 71 | title = list(text = if (as.alternatives) "" else ylabel), |
| 72 | ceiling = if (as.alternatives) 100 else NULL, |
| 73 | floor = 0, |
| 74 | labels = if(as.alternatives) list(format = paste0("{value}\U2009", ylabel)) else NULL |
| 75 | ) %>% |
Marc Kupietz | cf1771d | 2020-03-04 16:03:04 +0100 | [diff] [blame] | 76 | hc_xAxis(allowDecimals=FALSE) %>% |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 77 | hc_add_theme(hc_theme_google(colors=palette)) %>% |
| 78 | hc_plotOptions( |
Marc Kupietz | cf1771d | 2020-03-04 16:03:04 +0100 | [diff] [blame] | 79 | series = list(enabled = TRUE), |
Marc Kupietz | ab0b071 | 2020-05-04 16:24:57 +0200 | [diff] [blame] | 80 | spline = list(cursor = 'pointer', point = list(events = list( |
| 81 | click = JS("function() { window.open(this.click, 'korap'); }") |
| 82 | ))), |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 83 | line = list(cursor = 'pointer', point = list(events = list( |
| 84 | click = JS("function() { window.open(this.click, 'korap'); }") |
| 85 | )))) %>% |
Marc Kupietz | cf1771d | 2020-03-04 16:03:04 +0100 | [diff] [blame] | 86 | hc_credits(enabled = TRUE, |
Marc Kupietz | 900d052 | 2020-03-19 13:36:45 +0100 | [diff] [blame] | 87 | text = "KorAP R Client Package", |
| 88 | href = "https://github.com/KorAP/RKorAPClient/") %>% |
Marc Kupietz | cf1771d | 2020-03-04 16:03:04 +0100 | [diff] [blame] | 89 | hc_exporting(enabled = TRUE) %>% |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 90 | hc_tooltip( |
Marc Kupietz | ab0b071 | 2020-05-04 16:24:57 +0200 | [diff] [blame] | 91 | headerFormat = '<span style="font-size: 10pt">{point.key}</span><br/>', |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 92 | formatter = JS(paste0("function (tooltip) { |
| 93 | var str = tooltip.defaultFormatter.call(this, tooltip); |
| 94 | if(Array.isArray(str)) { |
| 95 | str = str.join(''); |
| 96 | } |
| 97 | for (var i = 0; i < this.points.length; i++) { |
| 98 | str = str.replace(/([0-9.,]+.?)", ylabel, "/, this.points[i].point.count+' ($1@)'); |
| 99 | } |
| 100 | return str.replace(/@/g, '", ylabel, "') |
| 101 | } ")), |
Marc Kupietz | cf1771d | 2020-03-04 16:03:04 +0100 | [diff] [blame] | 102 | crosshairs = TRUE, |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 103 | valueDecimals = 2, |
Marc Kupietz | cf1771d | 2020-03-04 16:03:04 +0100 | [diff] [blame] | 104 | shared = TRUE, |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 105 | valueSuffix = paste0('\U2009', ylabel) |
| 106 | ) %>% |
Marc Kupietz | ab0b071 | 2020-05-04 16:24:57 +0200 | [diff] [blame] | 107 | hc_add_series_korap_frequencies(df, smooth, as.alternatives, ...) |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Marc Kupietz | a4f3653 | 2020-02-03 22:50:08 +0100 | [diff] [blame] | 110 | ## Mute notes: "no visible binding for global variable:" |
| 111 | globalVariables(c("value", "query", "condition", "vc")) |
| 112 | |
Marc Kupietz | ab0b071 | 2020-05-04 16:24:57 +0200 | [diff] [blame] | 113 | hc_add_series_korap_frequencies <- function(hc, df, smooth = FALSE, |
| 114 | as.alternatives = FALSE, |
| 115 | ...) { |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 116 | index <- 0 |
Marc Kupietz | ab0b071 | 2020-05-04 16:24:57 +0200 | [diff] [blame] | 117 | type <- ifelse(smooth, "spline", "line") |
| 118 | areatype <- ifelse(smooth, "areasplinerange", "arearange") |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 119 | for(q in unique(df$condition)) { |
| 120 | dat <- df[df$condition==q,] |
| 121 | hc <- hc %>% hc_add_series( |
| 122 | marker = list(radius = 2), |
| 123 | name = q, |
| 124 | data = data.frame( |
| 125 | year = dat$year, |
| 126 | value = if (as.alternatives) dat$f else dat$ipm, |
| 127 | count = dat$totalResults, |
| 128 | click = dat$webUIRequestUrl |
| 129 | ), |
| 130 | hcaes(year, value), |
Marc Kupietz | ab0b071 | 2020-05-04 16:24:57 +0200 | [diff] [blame] | 131 | type = type, |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 132 | colorIndex = index, |
Marc Kupietz | 5b503f4 | 2020-04-09 15:26:00 +0200 | [diff] [blame] | 133 | zIndex = 1, |
| 134 | ... |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 135 | ) %>% |
| 136 | hc_add_series( |
| 137 | name = "ci", |
| 138 | data = dat[,c('year', 'conf.low', 'conf.high')], |
| 139 | hcaes(x = year, low = conf.low, high = conf.high), |
Marc Kupietz | ab0b071 | 2020-05-04 16:24:57 +0200 | [diff] [blame] | 140 | type = areatype, |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 141 | fillOpacity = 0.3, |
| 142 | lineWidth = 0, |
Marc Kupietz | cf1771d | 2020-03-04 16:03:04 +0100 | [diff] [blame] | 143 | marker = list(enabled = FALSE), |
| 144 | enableMouseTracking = FALSE, |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 145 | linkedTo= ':previous', |
| 146 | colorIndex = index, |
| 147 | zIndex = 0 |
| 148 | ) |
| 149 | index <- index+1 |
| 150 | } |
| 151 | hc |
| 152 | } |
Marc Kupietz | 882f08c | 2020-03-18 13:30:31 +0100 | [diff] [blame] | 153 | |
| 154 | .onAttach <- function(libname = find.package("RKorAPClient"), |
| 155 | pkgname = "RKorAPClient") { |
| 156 | packageStartupMessage( |
| 157 | "If you intend to use the Highcharts plot options, please note that Highcharts (www.highcharts.com) is a Highsoft software product which is not free for commercial and governmental use." |
| 158 | ) |
| 159 | } |
| 160 | |