Skip to content

Instantly share code, notes, and snippets.

@stijnvanhoey
Created March 7, 2018 12:24
Show Gist options
  • Save stijnvanhoey/6b6fe4ebeaa080a712be410ba729ccb0 to your computer and use it in GitHub Desktop.
Save stijnvanhoey/6b6fe4ebeaa080a712be410ba729ccb0 to your computer and use it in GitHub Desktop.
Loop plot for multiple species from userflora
library(RODBC)
library(ggplot2)
library(nlme)
library(mgcv)
library(lme4)
library(MASS)
library(splines)
# additional library to install for parameterized queries
library(RODBCext)
# A function that can be reused for the data acquisition. It assumes that the
# QryTrend view contains all species to work with
#' Trend plot of species
#'
#' @param species_name name of the species to query the data
#' @param dbase name of the access database to connect with
#' @param rel_path path on the computer where to store the figures
#'
#' @return saves ggplot figure as jpg
#' @export
#'
#' @examples plot_trend_species("korenbloem", "UserFlorabank.mdb", "./images")
plot_trend_species <- function(species_name, dbase, rel_path = '.') {
# dbase connection
connection <- odbcConnectAccess(dbase)
sqlCode <- "SELECT QryTrend.* from QryTrend WHERE species = ?;"
sqlPrepare(connection, sqlCode)
trend_data <- sqlExecute(channel = connection, species_name,
fetch = TRUE)
ggplot(trend_data, aes(x = BeginJaar, y = Percentage)) +
geom_point() +
geom_smooth() +
ylab(paste0("Percentage van onderzochte kmhokken met ", species_name)) +
xlab("jaar") +
scale_y_continuous(limits = c(0, 100)) +
theme(axis.text.x = element_text(size = 15),
axis.title.x = element_text(size = 15, face = "bold"),
axis.text.y = element_text(size = 10),
axis.title.y = element_text(size = 15, face = "bold"))
# save the figure
ggsave(file.path(rel_path, paste0(species_name, ".jpg")))
}
# Load the individual species names (CSV-fiel, just a list, maybe with a query?)
species_list <- TODO #!
# apply the function to each of the species names:
lapply(species_list, plot_trend_species, "UserFlorabank.mdb", "./images")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment