Automatic Google Scholar Citations Generator Plot for your Website (in OSX)

3 minute read

Published:

In this post I explain how I generated my Google Scholar citations automatic plot generator. After following these steps, you are going to be able to generate automatically your own Google Scholar citations plot in a daily basis, which your personal website will load (automatically too) every time someone visits it.

What you need: OSX, crontab, R, DropBox

  • You should use a Mac.
  • crontab should already be installed by default in your machine. So, there is no need to install that.
  • You should have both a DropBox and a Google Scholar account.
  • R should be updated.

Steps

  1. Copy the script below into R to extract data from your Google Scholar profile. Save the script as gscholar.R.
# load packages
if (!require("pacman")) install.packages("pacman"); library(pacman) # conditional installation of the packages below
p_load(scholar,ggplot2,scales)

# 
cit <- get_citation_history('3FHDoL0AAAAJ&hl')

p = ggplot(cit,aes(x=year,y=cites))+
  geom_bar(stat='identity')+
  theme_bw()+
  xlab('Year')+
  ylab('Citations') +
  scale_y_continuous(labels = number_format(accuracy = 1), breaks = round(seq(min(cit$cites), max(cit$cites), by = 1),1)) +
  scale_x_continuous(breaks = round(seq(min(cit$year), max(cit$year), by = 1),1)) +
  annotate('text',label=format(Sys.time(), "%Y-%m-%d"),x=-Inf,y=Inf,vjust=1.5,hjust=-0.05,size=3,colour='gray')  + 
  theme_classic() +
  theme(#axis.text.y   = element_text(size=14),
        #axis.text.x   = element_text(size=14),
        #axis.title.y  = element_text(size=14),
        #axis.title.x  = element_text(size=14),
        #panel.background = element_blank(),
        #panel.grid.major = element_blank(), 
        #panel.grid.minor = element_blank(),
        #axis.line = element_line(colour = "black"),
        panel.border = element_rect(colour = "black", fill=NA, size=1)) +
  ggtitle("Google Scholar Citations (updated daily)")
  
png("/Users/hectorbahamonde/Dropbox/scholar_citations.png",width=2200,height=1000,res=250)

print(p)

dev.off()
  • Make sure you replace 3FHDoL0AAAAJ&hl with your Google Scholar user number. You can find yours in your Google Scholar profile URL. In my case that is,
https://scholar.google.com/citations?user=3FHDoL0AAAAJ&hl=en

…that is, your user name is whatever is right after "user=".

  • This script is going to send the plot scholar_citations.png to your DropBox folder (make sure your DropBox folder is “/Users/HERE/…”).
  1. Go to DropBox, and get a shareable link of the plot scholar_citations.png (like this).

  2. Now the crontab part. Open terminal and type the following in this exact same order:

  • env EDITOR=nano crontab -e.

  • Next:

MAILTO=""
0 9 * * * /usr/local/bin/Rscript -e 'source("/Users/hectorbahamonde/hbahamonde.github.io/gscholar.R")'

This will automatically execute the R script gscholar.R every day at 9AM (make sure your computer is turned on by that time otherwise, crontab won’t be able to execute this task). If you need more specific times, see this link. Make sure the path of the script gscholar.R is correct in your case. That is, in your case it should be something like this: 'source("/Users/YOUR_USER/YOUR_FOLDER_WHERE_YOU_KEEP_THE_SCRIPT_DESCRIBED_IN_STEP_1/gscholar.R")'. Do not hit enter at this point. And make sure you copy and paste this as one line code per task (first task suppresses email alters every time the task is executed, and the second line is the R script task itself).

  • CTRL+O. This saves the crontab task.
  • Hit enter. This one just accepts the task name.
  • CTRL+X. And this one closes down crontab. Mac might ask you if you want to let Terminal to modify your files. Accept that.
  1. In your website, and using the shareable link of your scholar_citations.png plot (as described in step 2), insert the following HTML code to embed your daily-generated Google Scholar citations plot into your website. The DropBox link should go after src=. Notice that you have to replace whatever is after the ? symbol to raw=1.
<img src="https://www.dropbox.com/s/hlroz3qjeyrwmta/scholar_citations.png?raw=1" alt="gscholar"  style="width:800px;height:600;;border:0;">

I borrowed this from Ole Michelsen. I also got help from here. And from here. This one was important because my terminal window was very narrow, and I think it was indenting the crontab task described in 3.b in two lines, causing the task not to save. Thanks guys!