metrics_methods.R 2.05 KB
Newer Older
Karthik Ram's avatar
Karthik Ram committed
1
2

#' Print a summary for an altmetric object
Karthik Ram's avatar
Karthik Ram committed
3
#' @export 
Karthik Ram's avatar
Karthik Ram committed
4
5
6
#' @param x An object of class \code{Altmetric}
#' @param ... additional arguments
print.altmetric <- function(x, ...) {
7
    value <- NA
Karthik Ram's avatar
Karthik Ram committed
8

9
	string <- "Altmetrics on: \"%s\" with altmetric_id: %s published in %s."
Karthik Ram's avatar
Karthik Ram committed
10
    vals   <- c(x$title,  x$altmetric_id, x$journal)
Karthik Ram's avatar
Karthik Ram committed
11
12
    cat(do.call(sprintf, as.list(c(string, vals))))
    cat("\n")
Karthik Ram's avatar
Karthik Ram committed
13

Karthik Ram's avatar
Karthik Ram committed
14
    stats <- melt(x[grep("^cited", names(x))])
Karthik Ram's avatar
Karthik Ram committed
15
16
    stats$names <- unname(sapply(stats$L1, return_provider))
    stats$names <- factor(stats$names, levels = stats$names[rev(order(stats$value))])
17
18
    
    print( data.frame(provider = stats$names, count = stats$value))
19
20
21
22
23
24
}



#' Plots metrics for an altmetric object
#' 
Karthik Ram's avatar
Karthik Ram committed
25
#' @export 
Karthik Ram's avatar
Karthik Ram committed
26
#' @param x An object of class \code{Altmetric}
Karthik Ram's avatar
Karthik Ram committed
27
28
29
30
#' @import ggplot2
#' @importFrom reshape2 melt
#' @importFrom png readPNG
#' @importFrom RCurl getURLContent
Karthik Ram's avatar
Karthik Ram committed
31
32
#' @param ... additional arguments
plot.altmetric <- function(x, ...) {
33
34
35

value <- NA
# just to trick check()    
Karthik Ram's avatar
Karthik Ram committed
36
if (!is(x, "altmetric"))   
Karthik Ram's avatar
Karthik Ram committed
37
    stop("Not an altmetric object")
Karthik Ram's avatar
Karthik Ram committed
38

Karthik Ram's avatar
Karthik Ram committed
39
stats <- melt(x[grep("^cited", names(x))])
Karthik Ram's avatar
Karthik Ram committed
40
stats$names <- unname(sapply(stats$L1, return_provider))
41
stats$names <- factor(stats$names, levels = stats$names[rev(order(stats$value))])
42
stats <- stats[-(which(stats$L1=="cited_by_accounts_count")),]
43
# Grab the donut image
Karthik Ram's avatar
Karthik Ram committed
44
donut <- readPNG(getURLContent(x$images[[2]]))
45

Karthik Ram's avatar
Karthik Ram committed
46
# Now return a pretty plot
Karthik Ram's avatar
Karthik Ram committed
47
ggplot(stats, aes(names, value)) + geom_point(size = 4, colour = 'steelblue') + ggtitle(x$title) + xlab("Provider") + ylab("Hits") + theme(panel.background = element_blank(), panel.grid.major = element_blank(),
48
        panel.grid.minor = element_blank(), panel.border = element_blank(),
Karthik Ram's avatar
Karthik Ram committed
49
        axis.line = element_line(colour = "black")) + annotation_raster(donut, xmin = dim(stats)[1]-1, xmax = dim(stats)[1], ymin = max(stats$value)-(.2*max(stats$value)), ymax = max(stats$value), interpolate = T) + theme(title = element_text(family = "Helvetica", colour = "#0680b0", face="bold"), axis.text = element_text(family="Courier", colour = "#3f3f3f"), axis.title = element_text(colour="#3f3f3f")) 
Karthik Ram's avatar
Karthik Ram committed
50
51
}