Created
November 12, 2012 14:49
-
-
Save tts/4059808 to your computer and use it in GitHub Desktop.
Querying SPARQL endpoint with R and plotting results with R graphics package
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
###################################################### | |
# | |
# Example of querying Linked Open Aalto Data Service | |
# SPARQL endpoint with the SPARQL R client, and | |
# plotting data with the R graphics package. | |
# | |
# http://data.aalto.fi/ | |
# | |
# Tuija Sonkkila, 12.11.2012 | |
# | |
###################################################### | |
# http://cran.r-project.org/web/packages/SPARQL/index.html | |
library(SPARQL) | |
endpoint <- "http://data.aalto.fi/sparql" | |
# Prepare the query | |
# | |
# Example is from http://data.aalto.fi/vizplay | |
# adapted with namespace handling introduced at | |
# http://linkedscience.org/tools/sparql-package-for-r/tutorial-on-sparql-package-for-r/ | |
q <- "SELECT ?date (COUNT(?member) AS ?contributors )(COUNT(DISTINCT ?publication) AS ?publications ) | |
WHERE { | |
?publication <http://purl.org/ontology/bibo/authorList> ?list ; | |
<http://purl.org/dc/terms/date> ?date. | |
?list <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> */ | |
<http://www.w3.org/1999/02/22-rdf-syntax-ns#first> ?member . | |
} | |
GROUP BY ?date | |
ORDER BY ?date" | |
# Query the endpoint, and store results in a data frame | |
res <- SPARQL(url=endpoint, q)$results | |
# Find out about e.g. min and max values | |
summary(res) | |
# Reduce font size | |
par(cex = 0.8) | |
# Plot data about publications in blue | |
plot(res$date, | |
res$publications, | |
main = "Aalto University publication statistics", | |
type = "b", # join symbols with lines | |
pch = 2, # plot symbol | |
lty = 2, # plot line type | |
col = "blue", | |
ylim = c(0, 21000), # Y axis limits | |
xaxt = "n", yaxt = "n", # but don't draw axes yet | |
xlab = "", ylab = "") # nor axis labels | |
# In the same plot, add data about contributors | |
lines(res$date, | |
res$contributors, | |
type = "b", | |
pch = 2, | |
lty = 2, | |
col = "red") | |
# Add legend | |
legend("topleft", | |
inset = c(.05,.05), | |
c("Publications", "Contributors"), | |
pch = c(2, 2), | |
col = c("blue","red")) | |
# Draw X axis tick marks without labels | |
axis(1, | |
at = seq(1975, 2012, by = 2), | |
labels = FALSE, | |
col.axis = "black", | |
lty = 1) | |
# Character vector of X axis labels | |
xlablist <- as.vector(seq(1975, 2012, by = 2)) | |
# Draw X axis labels in 45 degree angle | |
# http://jnlnet.wordpress.com/2009/05/20/x-axis-labels-on-a-45-degree-angle-using-r/ | |
text(seq(1975, 2012, by = 2), | |
par("usr")[3], | |
adj = c(1.5,1.5), | |
labels = xlablist, | |
srt = 45, | |
xpd = TRUE) | |
# Draw the Y axis | |
axis(2) | |
# Add horizontal and vertical lines at tick points of both axes | |
abline(h = c(seq(1, 21000, by = 1000)), lty = 3, col = "gray") | |
abline(v = c(seq(1975, 2012, by = 2)), lty = 3, col = "gray") | |
# Add some explanatory text in italics without a box | |
par(font = 3) | |
legend("bottomleft", | |
inset = c(.18,.08), | |
bty = "n", | |
"Data officially since 1992") | |
# Restore the default font size and type | |
par(cex = 1, font = 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment