# Load required packages from local library into the R session. library(tidyverse) # ggplot2, tibble, tidyr, readr, purr, dplyr library(lubridate) # Dates and time. library(magrittr) # Pipe operator %>% %<>% %T>% equals(). library(rattle) # comcat(). library(WDI) # World Bank Data library(countrycode) library(gridExtra) # Name of the dataset. dsname <- "stroke" # Identify the source location of the dataset. dsloc <- "data" # Construct the path to the dataset and display some if it. dsname %>% paste0(".csv") %>% file.path(dsloc, .) %T>% cat("Dataset:", ., "\n\n") %T>% { paste("head", .) %>% system(intern=TRUE) %>% sub("\r", "\n", .) %>% print() } -> dspath dspath %>% read_csv2(na=".") %T>% glimpse() %>% assign(dsname, ., .GlobalEnv) dspath %>% read_delim(delim=";", na=".") %T>% glimpse() %>% assign(dsname, ., .GlobalEnv) # Prepare the dataset for usage with our template. ds <- get(dsname) # Review the variables to optionally normalise their names. names(ds) # Normalise the variable names. names(ds) %<>% normVarNames() %T>% print() # Confirm the results are as expected. glimpse(ds) ds$died %<>% dmy() %T>% {class(.) %>% print()} ds$dstr %<>% dmy() %T>% {class(.) %>% print()} glimpse(ds) (ct <- Sys.time()) class(ct) str(ct) unclass(ct) (ct <- as.POSIXlt(ct)) class(ct) str(ct) unclass(ct) format(Sys.time(), "%a %d %b %Y %H:%M:%S %Z") ds$lived <- ds$died - ds$dstr head(ds$lived) class(ds$lived) ds$lived <- ds$died - ds$dstr head(ds$lived) class(ds$lived) as.integer(ds$lived[1])/60/60/24 units(ds$lived) units(ds$lived) <- "days" units(ds$lived) head(ds$lived) ds$interval <- with(ds, interval(dstr, died)) head(ds$interval) class(ds$interval) max(ds$interval, na.rm=TRUE) min(ds$interval, na.rm=TRUE) head(as.duration(ds$interval)) ds %>% mutate(weekday=wday(dstr, label=TRUE, abbr=FALSE)) %>% ggplot(aes(weekday)) + geom_bar(colour="white", fill="lightblue") + labs(title="Day of Week of Incidence of Stroke", x="Weekday", y="Count") ds %>% mutate(mday=mday(dstr)) %>% ggplot(aes(mday)) + geom_bar(colour="white", fill="orange") + labs(title="Day of Month of Incidence of Stroke", x="Day of Month") data.frame(L1=100+c(0, cumsum(runif(99, -20, 20))), L2=150+c(0, cumsum(runif(99, -10, 10))), Date=seq.Date(as.Date("2000-01-01"), by="1 month", length.out=100)) %>% melt(id="Date") %>% ggplot(aes(x=Date, y=value, colour=variable)) + geom_line() + ylab("Observation") + labs(colour="Location") (meta.data <- WDIsearch("Fertility rate", field="name", short=FALSE)) (indicators <- meta.data[1:2, 1]) countries <- c("United States", "Britain", "India", "China", "Australia") (iso2char <- countrycode(countries, "country.name", "iso2c")) (wdids <- WDI(iso2char, meta.data[1:2,1], start=2001, end=2011)) plots <- lapply(indicators, function(nm) { p <- ggplot(wdids, aes(x=year, y=wdids[,nm], group=country, color=country), environment=environment()) p <- p + geom_line(size=1) p <- p + scale_x_continuous(name="Year", breaks=c(unique(wdids[,"year"]))) p <- p + scale_y_continuous(name=nm) p <- p + scale_linetype_discrete(name="Country") p <- p + theme(legend.title=element_blank()) p <- p + labs(title=paste(meta.data[meta.data[,1]==nm, "name"], "\n")) }) do.call(grid.arrange, plots) vars <- c("Date", "MinTemp", "MaxTemp", "Sunshine", "Rainfall", "Evaporation") ds <- weather[vars] ds$Sunshine <- ds$Sunshine * 60 ds$CumRainfall <- cumsum(ds$Rainfall) ds$CumEvaporation <- cumsum(ds$Evaporation) dsm <- melt(ds, id="Date") g <- ggplot(dsm, aes(x=Date, y=value, colour=variable)) g <- g + geom_point() print(g) g <- ggplot(dsm, aes(x=Date, y=value, colour=variable)) g <- g + geom_point() g <- g + scale_y_log10() print(g) asinh_trans <- function() trans_new(name="asinh", transform=asinh, inverse=sinh) g <- ggplot(dsm, aes(x=Date, y=value, colour=variable)) g <- g + geom_point() g <- g + scale_y_continuous(trans="asinh") print(g) g <- ggplot(dsm, aes(x=Date, y=value, colour=variable)) g <- g + geom_point() g <- g + scale_y_continuous(trans="asinh", limits=c(-1e4, 1e4)) print(g) g <- ggplot(dsm, aes(x=Date, y=value, colour=variable)) g <- g + geom_point() g <- g + scale_y_continuous(trans="asinh", breaks=c(-10, 0, 10, 1e2, 1e3)) print(g) g <- ggplot(dsm, aes(x=Date, y=value, colour=variable)) g <- g + geom_point() g <- g + scale_y_continuous(trans="asinh", breaks=c(-10, 0, 10, 1e2, 1e3), labels=c("-10", "0", "10", "100", "1K")) print(g) g <- ggplot(dsm, aes(x=Date, y=value, colour=variable)) g <- g + geom_line() g <- g + scale_y_continuous(trans="asinh", breaks=c(-10, 0, 10, 1e2, 1e3), labels=c("-10", "0", "10", "100", "1K")) print(g) draw.lines <- c("CumRainfall", "CumEvaporation") g <- ggplot(dsm, aes(x=Date, y=value, colour=variable)) g <- g + geom_point(data=subset(dsm, !variable %in% draw.lines)) g <- g + geom_line(data=subset(dsm, variable %in% draw.lines)) g <- g + scale_y_continuous(trans="asinh", breaks=c(-10, 0, 10, 1e2, 1e3), labels=c("-10", "0", "10", "100", "1K")) print(g) events <- as.Date(c("2007-12-25", "2008-03-22")) g <- ggplot(dsm, aes(x=Date, y=value, colour=variable)) g <- g + geom_point(data=subset(dsm, !variable %in% draw.lines)) g <- g + geom_line(data=subset(dsm, variable %in% draw.lines)) g <- g + scale_y_continuous(trans="asinh", breaks=c(-10, 0, 10, 1e2, 1e3), labels=c("-10", "0", "10", "100", "1K")) g <- g + geom_vline(xintercept=as.numeric(events), linetype=3) g <- g + annotate("text", events[1], -9, label="Christmas", size=3, colour="blue") g <- g + annotate("text", events[2], -9, label="Easter", size=3, colour="purple") print(g) g <- ggplot(dsm, aes(x=Date, y=value, colour=variable)) g <- g + geom_point(data=subset(dsm, !variable %in% draw.lines)) g <- g + geom_line(data=subset(dsm, variable %in% draw.lines)) g <- g + scale_y_continuous(trans="asinh", breaks=c(-10, 0, 10, 1e2, 1e3), labels=c("-10", "0", "10", "100", "1K")) g <- g + geom_vline(xintercept=as.numeric(events), linetype=3) g <- g + annotate("text", events[1], -9, label="Christmas", size=3, colour="blue") g <- g + annotate("text", events[2], -9, label="Easter", size=3, colour="purple") g <- g + labs(title=sprintf("Weather pattern for %s", weather$Location[1])) g <- g + theme(legend.direction="horizontal", legend.position="bottom") print(g)