-
Notifications
You must be signed in to change notification settings - Fork 1
/
Trends Analysis.R
85 lines (66 loc) · 2.15 KB
/
Trends Analysis.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
# Libraries
# Google Trends API
library(gtrendsR)
# Data Tools
library(tidyverse)
library(lubridate)
library(tidyquant)
# Setting up search terms
search_terms <- c(
"SQL",
"python"
)
# Read Search Terms ----
gtrends_lst <- search_terms %>%
gtrends(geo = "US", time = "all")
#Search Interest Over Time ----
gtrends_lst[["interest_over_time"]] %>%
mutate(hits = as.numeric(hits)) %>%
as_tibble() %>%
ggplot(aes(date, hits, color = keyword)) +
geom_line() +
geom_smooth(span = 0.3, se = FALSE) +
theme_tq() +
scale_color_tq() +
labs(title = paste("Keyword Trends - ", as.character((gtrends_lst[[1]]$geo[1])), "- Over Time"))
# 3.2 Trends by Geography ----
gtrends_lst[["interest_by_region"]] %>%
as_tibble()
states_tbl <- map_data("state") %>%
as_tibble() %>%
mutate(region = str_to_title(region))
states_tbl
state_trends_tbl <- gtrends_lst[["interest_by_region"]] %>%
left_join(states_tbl, by = c("location" = "region")) %>%
as_tibble()
state_trends_tbl %>%
ggplot(aes(long, lat)) +
geom_polygon(aes(group = group, fill = hits)) +
coord_map("albers", at0 = 45.5, lat1 = 29.5) +
scale_fill_viridis_c() +
theme_tq() +
facet_wrap(~ keyword, nrow = 1) +
labs(title = "Keyword Trends - US")
gtrends_lst %>% names()
gtrends_lst %>% pluck("interest_by_dma") %>% as_tibble() %>% View()
gtrends_lst %>% pluck("related_queries") %>% as_tibble() %>% View()
# 3.3 Related Queries ----
gtrends_lst[["related_queries"]] %>% DataExplorer::plot_bar()
top_10_related_searches_tbl <- gtrends_lst[["related_queries"]] %>%
as_tibble() %>%
filter(related_queries == "top") %>%
mutate(interest = as.numeric(subject)) %>%
select(keyword, value, interest) %>%
group_by(keyword) %>%
arrange(desc(interest)) %>%
slice(1:10) %>%
ungroup() %>%
mutate(value = as_factor(value) %>% fct_reorder(interest))
top_10_related_searches_tbl %>%
ggplot(aes(value, interest, color = keyword)) +
geom_segment(aes(xend = value, yend = 0)) +
geom_point() +
coord_flip() +
facet_wrap(~ keyword, nrow = 1, scales = "free_y") +
theme_tq() +
scale_color_tq()