knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
L'utilisateur sélectionne la liste des mots-clé relatifs à des formats de diffusion de données.
gtrends_lst <- c("csv", "json", "rdf")
Ensuite, on utilise le package gtrendsR
pour requêter via l'API Google Trends
. On visualise les premières lignes.
library(gtrendsR) library(dplyr) library(purrr) gtrends_api <- gtrends_lst %>% gtrends(geo = "FR", time = "today+5-y",onlyInterest = TRUE)%>% pluck("interest_over_time") %>% dplyr::mutate(hits = as.numeric(hits)) %>% as_tibble() # head(gtrends_api)
On construit le premier graphique (sur les 5 dernières années).
library(tidyquant) library(plotly) g1 <- gtrends_api %>% ggplot(aes(date, hits, color = keyword)) + geom_line() + geom_smooth(span = 0.3, se = FALSE) + theme_tq() + scale_color_tq() + labs(title = "Part des requêtes contenant le mot clé - évolution sur cinq ans - France") ggplotly(g1)
On construit la table qui servira pour le second graphique (depuis 2004)
library(gtrendsR) library(dplyr) library(purrr) gtrends_api_all <- gtrends_lst %>% gtrends(geo = "FR", time = "all",onlyInterest = TRUE)%>% pluck("interest_over_time") %>% dplyr::mutate(hits = as.numeric(hits)) %>% as_tibble() head(gtrends_api_all)
On construit le second graphique
library(tidyquant) library(plotly) g2 <- gtrends_api_all %>% ggplot(aes(date, hits, color = keyword)) + geom_line() + geom_smooth(span = 0.3, se = FALSE) + theme_tq() + scale_color_tq() + labs(title = "Part des requêtes par mot clé - évolution depuis 2004 - France") ggplotly(g2)
library(purrr) library(tidyquant) library(plotly) library(dplyr) library(forcats) n_terms <- 10 top_n_related_searches_tbl <- gtrends_lst %>% gtrends(geo = "FR", time = "today+5-y") %>% pluck("related_queries") %>% as_tibble() %>% filter(related_queries == "top") %>% dplyr::mutate(interest = as.numeric(subject)) %>% select(keyword, value, interest) %>% group_by(keyword) %>% arrange(desc(interest)) %>% slice(1:n_terms) %>% ungroup() %>% mutate(value = as_factor(value) %>% fct_reorder(interest)) g <- top_n_related_searches_tbl %>% ggplot(aes(value, interest, color = keyword)) + geom_segment(aes(xend = value, yend = 0)) + geom_point() + coord_flip() + facet_wrap(~ keyword, ncol = 1, scales = "free_y") + theme_tq() + scale_color_tq()+ labs(y = " ") ggplotly(g)
Enfin, on effectue une analyse graphique des mots clés les plus liés (un seul mot-clé d'intérêt) - depuis 2004
library(purrr) library(tidyquant) library(plotly) library(forcats) terme_gtrends_all <- c("rdf") %>%gtrends(geo = "FR", time = "all") top_n_related_topics_tbl <- terme_gtrends_all %>% pluck("related_topics") %>% as_tibble() %>% filter(related_topics == "top") %>% dplyr::mutate(interest = as.numeric(subject)) %>% select(keyword, value, interest) %>% group_by(keyword) %>% arrange(desc(interest)) %>% slice(1:n_terms) %>% ungroup() %>% mutate(value = as_factor(value) %>% fct_reorder(interest)) g_top <- top_n_related_topics_tbl %>% ggplot(aes(value, interest, color = keyword)) + geom_segment(aes(xend = value, yend = 0)) + geom_point() + coord_flip() + facet_wrap(~ keyword, ncol = 1, scales = "free_y") + theme_tq() + labs(x = " ",y = " ")+ scale_color_tq() ggplotly(g_top)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.