Load packages: these packages will allow us to create graphs as well as access the data needed to analyze the search records that we need.

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.1.0     ✔ purrr   0.3.0
## ✔ tibble  2.0.1     ✔ dplyr   0.7.8
## ✔ tidyr   0.8.2     ✔ stringr 1.4.0
## ✔ readr   1.3.1     ✔ forcats 0.3.0
## ── Conflicts ────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(DT)
library(gtrendsR)
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
## 
##     date

This code allows us to put all of the search result information into the tag “psych” which makes it easier for us in the long run.

psych <- gtrends("psychologist")
## Warning in system("timedatectl", intern = TRUE): running command
## 'timedatectl' had status 1

Here we want to plot the information we just coded above into a graph. We labeled it Google searches for psychologists over time.

psych$interest_over_time %>% 
  ggplot(aes(x = date, y = hits)) +
  geom_line()+
  theme_minimal() +
  labs(title = "Google searches for 'psychologist' over time")

Here we want to do a graph like the one above, except we want to see by month how the graph looks. This takes the information from the top and puts it all into monthly categories so we can better see how the graph moves.

psych$interest_over_time %>% 
  mutate(month = month(date)) %>%         
  group_by(month) %>%                     
  summarize(hits_per_month = mean(hits)) %>%         
  ggplot(aes(x = month, y = hits_per_month)) +
  geom_line() +
  scale_x_discrete(limits = c(1:12))+
  theme_minimal()+
  labs(title = "Google searches for 'psychologist,' by month")

Here we wanted to see the top 100 search areas. You can also search for a location in the top search bar.

psych$interest_by_dma %>% 
  datatable()

Here we want to get the data comparing US to canadian searches for psychologist. We again make it easier for us in the future and put it under psych_US_CA.

psych_US_CA <- gtrends("psychologist", geo = c("US", "CA"))

Here we are going to graph the information we just recieved above.

psych_US_CA$interest_over_time %>% 
  mutate(month = month(date)) %>% 
  group_by(month, geo) %>% 
  summarize(hits_per_month = mean(hits)) %>% 
  ggplot(aes(x = month, y = hits_per_month, color = geo)) +
  geom_line()+
  scale_x_discrete(limits = c(1:12)) +
  theme_minimal() +
  labs(title = "Comparing US and Canadian searches for 'psychologist,' by month")