Skip to content

Instantly share code, notes, and snippets.

@tts
Created June 14, 2017 09:58
Show Gist options
  • Save tts/d41a93dfaeb31bb764b8b0e3e814a49b to your computer and use it in GitHub Desktop.
Save tts/d41a93dfaeb31bb764b8b0e3e814a49b to your computer and use it in GitHub Desktop.
library(dplyr)
library(tidyr)
library(ggplot2)
library(gridExtra)
library(grid)
data20162017 <- read.csv("toshiny_altm_2016_2017.csv", stringsAsFactors = FALSE)
data20162017 <- data20162017 %>%
mutate(gap_cites = Citations2017 - Citations,
gap_tweets = Twitter2017 - Twitter,
gap_mend = Mendeley2017 - Mendeley)
data20162017_gathered <- gather(data20162017, metrics, value, Citations, Citations2017, Mendeley, Mendeley2017, Twitter, Twitter2017)
data20162017_gathered <- data20162017_gathered %>%
mutate(Date = ifelse(grepl('2017', metrics),
format(as.Date("2017-05-30", format="%Y-%m-%d"),"%Y-%m-%d"),
format(as.Date("2016-05-13", format="%Y-%m-%d"),"%Y-%m-%d")))
data20162017_gathered$metrics <- gsub("([^0-9]*)[0-9]+","\\1",data20162017_gathered$metrics)
data20162017_gathered$Date <- as.Date(data20162017_gathered$Date, origin="1970-01-01")
data20162017_gathered$value <- as.numeric(data20162017_gathered$value)
data20162017_gathered$DOI <- gsub("/", "/\n", data20162017_gathered$DOI)
data20162017_gathered$value[is.na(data20162017_gathered$value)] <- 0
# https://blog.dominodatalab.com/visualizing-homeownership-in-the-us-using-small-multiples-and-r/
opts <- theme(
panel.background = element_rect(fill="white"),
panel.border = element_rect(colour="black", fill=NA),
axis.line = element_blank(),
axis.ticks = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text = element_text(colour="gray25", size=15, angle=90, hjust=1),
axis.title = element_blank(),
text = element_text(size=12),
legend.key = element_blank(),
legend.position = "none",
legend.background = element_blank(),
plot.title = element_text(size = 45))
vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y)
doschool <- function(data = NULL, sc = NULL, m = NULL) {
m_name <- if(m=="gap_cites") "citations" else if(m=="gap_tweets") "tweets" else "Mendeley readers"
message("Now printing ", sc, " sorted by diff in ", m_name)
flush.console()
s <- data %>%
filter(School == sc) %>%
arrange_(.dots = paste0("desc(", m, ")"))
dois <- unique(s$DOI)
if (length(dois)<=50) {
dois <- dois
} else {
dois <- dois[1:100]
}
grid.newpage()
if ( s[1, "School"]=="ARTS" ) {
png(file=paste0("altm_", m, "_", sc, ".png"), height = 1500, width = 3000)
pushViewport(viewport(layout = grid.layout(7, 10, heights=c(.3,.1,rep(1,5)), widths=c(1.1, rep(1,9)))))
} else {
png(file=paste0("altm_", m, "_", sc, ".png"), height = 3500, width = 4000)
pushViewport(viewport(layout = grid.layout(12, 10, heights=c(.3,.1,rep(1,10)), widths=c(1.1, rep(1,9)))))
}
grid.text("WoS citations, Mendeley readers, and tweets 2016-2017",
gp=gpar(fontsize=50, col="gray40"),
vp = viewport(layout.pos.row = 1,
layout.pos.col = 1:10))
grid.text(paste0("Aalto University, School of ", sc, ". Max 100 items published 2013-2015, sorted by changes in ", m_name, " between 2016-05-13 and 2017-06-01."),
gp=gpar(fontsize=30, col="gray40"),
vp = viewport(layout.pos.row = 2,
layout.pos.col = 1:10))
for (i in 1:length(dois))
{
datap <- subset(s, DOI==dois[i])
p <- ggplot(datap, aes(Date, value))+
geom_rect(data = datap,
aes(fill = factor(metrics)),
xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, alpha = NA)+
scale_fill_brewer(palette="Set2")+
geom_line(size=1.5)+
facet_wrap(DOI ~ metrics)+
{if ( datap[1,"School"]=="ARTS" ) { scale_y_continuous(expand = c(0, 0), limits = c(0,190)) } else { scale_y_log10(limits = c(1,1e3)) }}+
scale_x_date(date_breaks = "1 year", date_labels = "%Y")+
opts
if (i%%10!=1) p <- p+theme(axis.text.y = element_blank())
if (datap[1,"School"]=="ARTS") {
if (i<=40) p <- p+theme(axis.text.x = element_blank())
} else {
if (i<=90) p <- p+theme(axis.text.x = element_blank())
}
print(p, vp = vplayout(floor((i-1)/10)+3, i%%10+(i%%10==0)*10))
}
dev.off()
}
metrics <- c("gap_cites", "gap_tweets", "gap_mend")
schools <- sort(unique(data_gathered$School))
for (i in 1:length(schools)) {
for (j in 1:length(metrics)) {
doschool(data20162017_gathered, schools[i], metrics[j])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment