Wednesday, December 03, 2014

Creating your own Google Trends plots in R

With this post, I want to demonstrate how you can use the R functions I've built to create your own Google Trends graphs.



First, install the functions by pasting this code into R.

You now have the following functions at your disposal:
  • URL_GT(keywords)
  • downoloadGT(url, downloadDir)
  • readGT(file path)
By running the following code, you will get chart above.

#downloadDir=Where you save the csv from Google Trends
downloadDir="C:/downloads"
setwd(downloadDir)
keywords=c("Samsung", "Apple", "Nokia")
url=URL_GT(keywords)
filePath=downloadGT(url, downloadDir)
smartphones=readGT(filePath)

library(ggplot2)

ggplot(smartphones, aes(x=Date, y=SVI, color=Keyword))+geom_line()

3 comments:

Unknown said...

Thanks for the interesting post. Unfortunately, when I tried to use with my R version 3.0.2 it gave me the following error:

"Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file 'report.csv': No such file or directory"

It is somewhat strange since the report.csv is correctly downloaded in the specified "downloadDir" directory, but it is unable to open it.

Thanks for the help.

Best regards, Dean

ErikJJ said...

Hi Dean,

You need to set your download directory as your working directory by entering the following:

setwd(downloadDir)

Then it should work. Let me know if that doesn't help!

Rebecca Michaelson said...

HELLO VIEWERS

TESTIMONY ON HOW I GOT MY LOAN €300,000.00EURO FROM A FINANCE COMPANY LAST WEEK Email for immediate response: drbenjaminfinance@gmail.com

Do you need a loan to start a business or pay your debts {Dr.Benjamin Scarlett Owen} can also help you with a legit loan offer. He Has also helped some other colleagues of mine with a loan finance. Get your Blank ATM card or CREDIT CARD deliver to your doorstep that works in all ATM machines all over the world with the help of BENJAMIN LOAN FINANCE the ATM cards can be used to withdraw at the ATM Machines or swipe, at stores and POS. they give out this cards to all interested clients worldwide, If you need a loan without cost/stress he his the right loan lender to wipe away your financial problems and crisis today. BENJAMIN LOAN FINANCE holds all of the information about how to obtain money quickly and painlessly via Email drbenjaminfinance@gmail.com

Entertaining Blogs - BlogCatalog Blog Directory
Bloggtoppen.se