rm(list = ls())
## For information retrieval
library(scholar)
## Warning: package 'scholar' was built under R version 4.0.5
## For data manipulation
library(plyr)
## For plotting
library(ggplot2)
## My ID
kaz_yos <- "xrY7bFYAAAAJ"

## Retrieve data
df <- compare_scholar_careers(kaz_yos)
#View(df)
sum(df$cites)
## [1] 1200
head(df)
##             id year cites career_year        name
## 1 xrY7bFYAAAAJ 2007     5           0 Andi Fugard
## 2 xrY7bFYAAAAJ 2008     3           1 Andi Fugard
## 3 xrY7bFYAAAAJ 2009     3           2 Andi Fugard
## 4 xrY7bFYAAAAJ 2010     3           3 Andi Fugard
## 5 xrY7bFYAAAAJ 2011    10           4 Andi Fugard
## 6 xrY7bFYAAAAJ 2012    19           5 Andi Fugard
## Add cumulative citation
df <- ddply(.data = df,
            .variables = c("id"),
            .fun = transform,
            cumulative_cites = cumsum(cites))
## Plot
ggplot(df,
       mapping = aes(x = career_year, y = cumulative_cites, color = name)) +
  geom_line() +
  theme_bw()

###########################################
andi_id <- "xrY7bFYAAAAJ"
pubs <- get_publications(andi_id)
head(pubs,2)
##                                                                            title
## 1 Supporting thinking on sample sizes for thematic analyses: a quantitative tool
## 2        How people interpret conditionals: Shifts toward the conditional event.
##                                            author
## 1                           AJB Fugard, HWW Potts
## 2 AJB Fugard, N Pfeifer, B Mayerhofer, GD Kleiter
##                                                               journal
## 1                International Journal of Social Research Methodology
## 2 Journal of Experimental Psychology: Learning, Memory, and Cognition
##            number cites year                  cid        pubid
## 1 18 (6), 669-684   517 2015  3882549739737596858 PaBasH6fAo0C
## 2     37 (3), 635   128 2011 12907758729282933491 u-x6o8ySG0sC
pubs_1 <- data.frame(table(pubs$year))
pubs_1$cumpubs <- cumsum(pubs_1$Freq)
colnames(pubs_1)[1:2] <- c("year", "pubs")
############################
data_1 <- merge(pubs_1, df, by = "year")
###################################################plot\
str(data_1)
## 'data.frame':    10 obs. of  8 variables:
##  $ year            : Factor w/ 11 levels "2005","2011",..: 2 3 4 5 6 7 8 9 10 11
##  $ pubs            : int  5 2 7 3 9 2 4 3 2 1
##  $ cumpubs         : int  6 8 15 18 27 29 33 36 38 39
##  $ id              : chr  "xrY7bFYAAAAJ" "xrY7bFYAAAAJ" "xrY7bFYAAAAJ" "xrY7bFYAAAAJ" ...
##  $ cites           : num  10 19 38 54 89 100 118 190 199 221
##  $ career_year     : num  4 5 6 7 8 9 10 11 12 13
##  $ name            : chr  "Andi Fugard" "Andi Fugard" "Andi Fugard" "Andi Fugard" ...
##  $ cumulative_cites: num  24 43 81 135 224 ...
data_1$year <- as.numeric(as.character(data_1$year))
head(data_1)
##   year pubs cumpubs           id cites career_year        name cumulative_cites
## 1 2011    5       6 xrY7bFYAAAAJ    10           4 Andi Fugard               24
## 2 2012    2       8 xrY7bFYAAAAJ    19           5 Andi Fugard               43
## 3 2013    7      15 xrY7bFYAAAAJ    38           6 Andi Fugard               81
## 4 2014    3      18 xrY7bFYAAAAJ    54           7 Andi Fugard              135
## 5 2015    9      27 xrY7bFYAAAAJ    89           8 Andi Fugard              224
## 6 2016    2      29 xrY7bFYAAAAJ   100           9 Andi Fugard              324
p1 <- ggplot(data=data_1) +
  geom_step(aes(x=year, y=cumpubs),color = "#B3E2CD", direction="h", linetype=1,size = 1) +
  geom_point(aes(x=year, y=cumpubs),color= "#FBB4AE", size = 2) +
  scale_x_continuous(expand = c(0.05, 0.05), breaks = 2012:2021) +
  scale_y_continuous(expand = c(0.05, 0.05))+
  theme(legend.key = element_rect(colour = NA, fill = NA),
        #plot.title = element_text(colour="black", size=13, 
        #                         face="plain",family="sans",
        #                         hjust = 0.05),
        #plot.margin=unit(c(0.5,0,0.5,0),"cm"),
        legend.position= "top",
        legend.direction= "horizontal",
        legend.title = element_text(colour="black", size=13, 
                                    face="plain",family="sans"),
        legend.text = element_text(colour="black", size=13, 
                                   face="plain",family="sans"),
        legend.background = element_blank(),
        legend.box.background =  element_blank(),
        #legend.key.size = unit(1, "cm"),
        #legend.key.width = unit(0.4, "cm"),
        #legend.key.height=unit(1, "cm"),
        #axis.line = element_blank(),
        #plot.background = element_blank(),
        panel.background = element_blank(),
        panel.border = element_rect(colour = NA, fill= NA, size=1),
        panel.grid = element_blank(),
        #panel.grid = element_blank(),
        axis.title.y = element_text(color="black", size=13, face="plain",
                                    family="sans",angle = 90,hjust = 0.5),
        #axis.ticks = element_blank(),
        axis.text.x = element_text(color="black", size=13, face="plain",
                                   family="sans",angle = 45,hjust = 1, vjust = 1),
        axis.text.y = element_text(color="black", size=13, face="plain",family="sans",
                                   angle = 0,hjust = 0.5),
        #strip.text = element_blank(),
        axis.ticks =  element_line(size= 0.5),
        axis.ticks.length = unit(3, "pt"),
        axis.line = element_line(size = 0.5, colour = "black"),
        aspect.ratio = 1) +
  labs(y ="Cumulative number of published articles", x = "Year")

p1

ggsave(paste0(Sys.Date(),"-Cumulative published articles.tiff"), 
       plot = last_plot(), device = NULL,
       scale = 1, width = 11, height = 11, units ="cm",dpi = 300, 
       limitsize = TRUE,compression = "lzw")
################################
head(data_1)
##   year pubs cumpubs           id cites career_year        name cumulative_cites
## 1 2011    5       6 xrY7bFYAAAAJ    10           4 Andi Fugard               24
## 2 2012    2       8 xrY7bFYAAAAJ    19           5 Andi Fugard               43
## 3 2013    7      15 xrY7bFYAAAAJ    38           6 Andi Fugard               81
## 4 2014    3      18 xrY7bFYAAAAJ    54           7 Andi Fugard              135
## 5 2015    9      27 xrY7bFYAAAAJ    89           8 Andi Fugard              224
## 6 2016    2      29 xrY7bFYAAAAJ   100           9 Andi Fugard              324
p2 <- ggplot(data=data_1) +
  geom_step(aes(x=year, y=cumulative_cites),color = "#B3CDE3", direction="h", linetype=1,size = 1) +
  geom_point(aes(x=year, y=cumulative_cites),color= "#FED9A6" , size = 2) +
  scale_x_continuous(expand = c(0.05, 0.05), breaks = 2012:2021) +
  scale_y_continuous(expand = c(0.05, 0.05))+
  theme(legend.key = element_rect(colour = NA, fill = NA),
        #plot.title = element_text(colour="black", size=13, 
        #                         face="plain",family="sans",
        #                         hjust = 0.05),
        #plot.margin=unit(c(0.5,0,0.5,0),"cm"),
        legend.position= "top",
        legend.direction= "horizontal",
        legend.title = element_text(colour="black", size=13, 
                                    face="plain",family="sans"),
        legend.text = element_text(colour="black", size=13, 
                                   face="plain",family="sans"),
        legend.background = element_blank(),
        legend.box.background =  element_blank(),
        #legend.key.size = unit(1, "cm"),
        #legend.key.width = unit(0.4, "cm"),
        #legend.key.height=unit(1, "cm"),
        #axis.line = element_blank(),
        #plot.background = element_blank(),
        panel.background = element_blank(),
        panel.border = element_rect(colour = NA, fill= NA, size=1),
        panel.grid = element_blank(),
        #panel.grid = element_blank(),
        axis.title.y = element_text(color="black", size=13, face="plain",
                                    family="sans",angle = 90,hjust = 0.5),
        #axis.ticks = element_blank(),
        axis.text.x = element_text(color="black", size=13, face="plain",
                                   family="sans",angle = 45,hjust = 1, vjust = 1),
        axis.text.y = element_text(color="black", size=13, face="plain",family="sans",
                                   angle = 0,hjust = 0.5),
        #strip.text = element_blank(),
        axis.ticks =  element_line(size= 0.5),
        axis.ticks.length = unit(3, "pt"),
        axis.line = element_line(size = 0.5, colour = "black"),
        aspect.ratio = 1) +
  labs(y ="Cumulative number of citations", x = "Year")

p2

ggsave(paste0(Sys.Date(),"-Cumulative number of citations-2.tiff"), 
       plot = last_plot(), device = NULL,
       scale = 1, width = 11, height = 11, units ="cm",dpi = 300, 
       limitsize = TRUE,compression = "lzw")
###########################
library(cowplot)

plot_grid(p1, p2, labels = "AUTO", rel_widths = c(1, 1))

ggsave(paste0(Sys.Date(),"-merge_2.tiff"), 
       plot = last_plot(), device = NULL,
       scale = 1, width = 22, height = 11, units ="cm",dpi = 300, 
       limitsize = TRUE,compression = "lzw")