This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sentiment_timeline_likert_module.R
126 lines (107 loc) · 4.08 KB
/
sentiment_timeline_likert_module.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
sentimentTimelineLikertUI <- function(id){
ns <- NS(id)
tagList(
description = div(
"This chart uses a Likert scale, rotated 90°, to show the spread of sentiment for each dataset by month. Positive and Negative sentiment are indicated by blue and red respectively. The height of each bar indicates the percentage of responses that fell into the respective sentiment category."
),
plot = div(
div(
style = "max-width: 1200px;",
shiny::uiOutput(ns("sentiment_timeline_likert_plot_ui")) %>%
shinycssloaders::withSpinner()
),
div(
id = ns("sentiment_legend"),
style = "max-width: 1200px;",
align="center",
sentiment_legend()
)
)
)
}
sentimentTimelineLikert <- function(input, output, session,
data_set_info,
query_info) {
plot_data_likert <- reactive({
req(query_info()$num_hits > 0)
# arrange by order to make sure colours show up in correct order in chart
colour_map <- get_sentiment_colourmap() %>%
arrange(order)
aggregations = query_text_depot(query_info = query_info(),
aggregates_json = sentimentBucketsTimelineQuery(colour_map))
aggregations = parse_aggregates(es_results = aggregations)
plotting_data <- aggregations$month_counts.buckets %>%
select(index_name = index, key_as_string, key, doc_count, ends_with(".doc_count")) %>%
mutate(Date = as.Date(stringr::str_sub(key_as_string, 1, 10))) %>%
select(-key_as_string) %>%
wrangle_likert_data(colour_map = colour_map, data_set_info = data_set_info())
return(plotting_data)
})
output$sentiment_timeline_likert_plotly <- plotly::renderPlotly({
req(plot_data_likert)
p <- plot_data_likert() %>%
mutate(hover_label = paste0("<b>", display_name, '</b>\n',
"Sentiment Category: ", sentiment_label, '\n',
"Document Count: ", display_count, '\n',
"Document Percent: ", sigfig(display_frac * 100, n = 2), '%', '\n',
"Date: ", format(Date, format = "%b-%Y"))) %>%
tidyr::replace_na(list(frac_mod=0)) %>% # this enables proper scaling in the plotly conversion of ggplot (plotly ignores NAs)
plot_likert(colour_map = get_sentiment_colourmap(),
x_var = Date,
fractions_var = frac_mod,
fill_name = Sentiment,
group_var = "display_name",
hover_label = hover_label)
p1 <- plotly::ggplotly(p, tooltip = c("text"),dynamicTicks = "x") %>%
layout(margin = list(l = 75, r = 75),
xaxis = list(
type = 'date',
tickformat = "%b<br>%Y"
))
for (x in names(p1$x$layout)[grepl("yaxis", names(p1$x$layout))]) {
p1[["x"]][["layout"]][[x]][["fixedrange"]] <- TRUE
}
p1
})
output$sentiment_colour_scale <- renderPlot({
colour_map <- get_sentiment_colourmap()
sentimentLegendPlot(colour_mapping = colour_map)
})
output$sentiment_timeline_likert_plot_ui <- renderUI({
n_facets <- n_distinct(plot_data_likert()$display_name)
plotly::plotlyOutput(session$ns("sentiment_timeline_likert_plotly"), height = 200 + (150 * n_facets))
})
}
sentimentBucketsTimelineQuery <- function(mapping) {
bucket_str = build_likert_bins(mapping)
glue::glue(
'
"aggs": {
"group_by_index": {
"terms": {
"field": "_index"
},
"aggs" : {
"month_counts": {
"date_histogram" : {
"field" : "date",
"calendar_interval" : "month"
},
"aggs" : {
"sentiment" : {
"range" : {
"field" : "sentiment_polarity",
"keyed" : true,
"ranges" : [
<{bucket_str}>
]
}
}
}
}
}
}
}
',
.open = "<{", .close = "}>")
}