Skip to content

Instantly share code, notes, and snippets.

@erzk
Created November 21, 2015 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erzk/3a24c10be67c640d2d6a to your computer and use it in GitHub Desktop.
Save erzk/3a24c10be67c640d2d6a to your computer and use it in GitHub Desktop.
Query Google Trends and Google Ngrams using R
# install necessary packages
devtools::install_github("dvanclev/GTrendsR")
devtools::install_github("trinker/gtrend")
# load the packages
library(curl)
library(dplyr)
library(gtrend)
library(ggplot2)
library(ngramr)
# query Google Ngrams and create a plot
ngramData <- ngrami(c("Sigmund Freud", "Carl Jung"), year_start = 1900)
ggplot(ngramData,
aes(Year, Frequency, colour = Phrase)) +
geom_line(lwd = 1) +
stat_smooth()
# query Google Trends
gtrendQueries <- c("Sigmund Freud", "Carl Jung")
# add your Google account details
outputGT <- gtrend_scraper("YOUR EMAIL", "YOUR PASSWORD", gtrendQueries)
# create a plot
outputGT %>%
trend2long() %>%
plot() +
stat_smooth()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment