This is related to earlier question: R: How to pass parameters to ggplot geom_ within a function?
I want to be able to write utility functions that use geom_xxxxx
functions.
Here is an example
I want to have a function that overlays milestones on any time-series plot, as shown below.
set.seed(0); library(data.table); library(ggplot2)
DT <- data.table(
Date = as.Date(1:100, origin="2010-01-01"),
state = LETTERS[1:3],
sex = c("m","f"),
value = as.integer(runif(1:100)*100)
)
dtMilestones <- data.table(
Date = paste( "2010-15-", 2:4) %>% ydm,
Event = paste( "Phase ", 1:3)
)
g0 <- ggplot(DT, aes(Date, value)) +
geom_line() +
facet_grid(state ~ sex, scales = "free")
g0
g <- g0
for (i in 1:nrow(dtMilestones)) {
g <- g +
geom_label(aes(x=dtMilestones$Date[i], max(value), label=dtMilestones$Event[i]),label.size = 0.5) +
geom_vline(xintercept=dtMilestones$Date[i], linetype=5)
}
g
The code above creates the plots shown below (without milestones and with milestones) (ignore the wrong label placement - that's for another stackoverflow question)
How can I make a function out of it? I tried this code below and it does not work
g.add.milestones <- function(dtMilestones) {
gg <- list()
for (i in 1:nrow(dtMilestones)) {
gg[[i]] <-
geom_label(aes(x=milestones[i], max(value), label=dtMilestones$Event[i]),label.size = 0.5)
gg[[i+nrow(dtMilestones)]] <-
geom_vline(xintercept=dtMilestones$Date[i], linetype=5)
}
}
g0 + g.add.milestones(dtMilestones)
PS. If anyone can suggest the code that prints labels where needed (besides EACH vertical line on top of the highest point in each facet - currently all of them are printed in the )