---
title: "Cerques a Google"
author: "Partits: Vox, Aliança Catalana, CUP, Esquerra Republicana de Catalunya - Candidats: L.Estrada, A.Fernández, I.Garriga, S.Orriols, C.Puigdemont"
date: "`r format(strptime(as.character(lubridate::today()), '%Y-%m-%d'), '%d/%m/%Y')`"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
logo: banda_provaPetit.png
theme: yeti
source_code: embed
---
<script>
$('.navbar-logo').wrap('<a href="https://gaiacooperacion.net" target=_blank>');
</script>
<style>
.navbar{
background: linear-gradient(#b77b7f,#e5d0d6);
}
.nav-tabs-custom > .nav-tabs > li.active a { /* ================ Format inside: tabs titles-- */
font-weight: bold;
color: #b77b7f;
border-top-color: none;
font-size: 14pt;
}
.nav-tabs-custom > .nav-tabs > li:not(.active) a {
color:#b77b7f;
font-size: 14pt;
}
</style>
```{r include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
knitr::opts_chunk$set(message = FALSE)
knitr::opts_chunk$set(warning = FALSE)
```
```{r}
library(gtrendsR)
library(dplyr)
library(ggplot2)
library(plotly)
#library(crosstalk)
#install.packages("colorspace")
#install.packages("vctrs")
#install.packages("gtrendsR")
```
```{r eval=FALSE}
# Dades
#setHandleParameters(user = 'gaiacooperacion',
# password = '1agiovanna1b')
res1 <- gtrends(keyword = c("VOX","aliança catalana", 'cup', 'esquerra republicana',"psc"),
geo="ES-CT", time="today 12-m")
Sys.sleep(10) # Pero obviar el problema 429
iot <- res1$interest_over_time
writexl::write_xlsx(iot, "trendsGoogle.xlsx")
res1b <- gtrends(keyword = c("ciutadans", "junts per catalunya"),
geo="ES-CT", time="today 12-m")
Sys.sleep(10) # Pero obviar el problema 429
iotb <- res1b$interest_over_time
writexl::write_xlsx(iotb, "trendsGoogleb.xlsx")
res2 <- gtrends(keyword = c("silvia orriols", "ignacio garriga", "alejandro fernandez", "laia estrada", "carles puigdemont"),geo="ES-CT", time="today 12-m")
Sys.sleep(10) # Pero obviar el problema 429
res2b <- gtrends(keyword = c("jessica albiach", "clara ponsati", "carlos carrizosa"),geo="ES-CT", time="today 12-m")
Sys.sleep(10) # Pero obviar el problema 429
iot2b <- res2b$interest_over_time
writexl::write_xlsx(iot2b, "trendsGoogle2b.xlsx")
```
## Column{.tabset}
```{r}
iot <- readxl::read_xlsx("trendsGoogle.xlsx") %>%
mutate(tipus= "Partit")
iot2 <- readxl::read_xlsx("trendsGoogle2.xlsx") %>%
mutate(tipus="Candidat")
```
### Partits
```{r}
tr <- highlight_key(iot, ~keyword)
p <- tr %>%
ggplot(aes(x=date,
y=hits,
color=keyword,
group= keyword)) +
geom_line() +
theme_minimal() +
theme(plot.title = element_text(size = 12)) +
ylab("Clics") +
xlab("Data")+
guides(color=guide_legend(title="Paraules de cerca"))
ggplotly(p, height = 500) %>%
layout(title= list(text= paste0("Cerques de noms de partits - Catalunya - Últims 12 mesos",
"<br>",
"<sup>",
"Per aillar una línia, passeu-hi el ratolí. Per deixar de seleccionar-la, feu doble clic amb el ratolí","</sup>"),
font= list(size=16)),
annotations = list(
x = 1,
y = -0.09,
text = "<i>Data: Google Trends. Courtesy: gtrendsR package</i>",
showarrow = F, xref='paper', yref='paper',
xanchor='right', yanchor='auto', xshift=0, yshift=0,
font=list(size=12))
) %>%
highlight(on = "plotly_hover", off= 'plotly_doubleclick')
```
### Candidats
```{r}
tx <- highlight_key(iot2, ~keyword)
p1 <- tx %>%
ggplot(aes(x=date,
y=hits,
color=keyword,
group= keyword)) +
geom_line() +
theme_minimal() +
theme(plot.title = element_text(size = 12)) +
ylab("Clics") +
xlab("Data")+
guides(color=guide_legend(title="Paraules de cerca"))
ggplotly(p1, height = 500) %>%
layout(title= list(text= paste0("Cerques de noms de candidats - Catalunya - Últims 12 mesos",
"<br>",
"<sup>",
"Per aillar una línia, passeu-hi el ratolí. Per deixar de seleccionar-la, feu doble clic amb el ratolí","</sup>"),
font= list(size=16)),
annotations = list(
x = 1,
y = -0.09,
text = "<i>Data: Google Trends. Courtesy: gtrendsR package</i>",
showarrow = F, xref='paper', yref='paper',
xanchor='right', yanchor='auto', xshift=0, yshift=0,
font=list(size=12))
) %>%
highlight(on = "plotly_hover", off= 'plotly_doubleclick')
```