From 24e66460a9615b5ded14007ba39eb50ff0a3e954 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=20Antonio=20Maga=C3=B1a=20Mesa?= Date: Sun, 17 May 2015 22:35:24 +1000 Subject: [PATCH 1/2] Added parameter color.col so that Text shown can be different from the one used for coloring. --- R/timeline.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/R/timeline.R b/R/timeline.R index a240043..b9418ec 100644 --- a/R/timeline.R +++ b/R/timeline.R @@ -57,6 +57,7 @@ #' timeline(ww2, ww2.events, event.spots=2, event.label='', event.above=FALSE) timeline <- function(df, events, label.col = names(df)[1], + color.col = names(df)[1], group.col = names(df)[2], start.col = names(df)[3], end.col = names(df)[4], @@ -187,10 +188,11 @@ timeline <- function(df, events, p <- p + geom_rect(data=df, aes_string(xmin=start.col, xmax=end.col, - ymin='ymin', ymax='ymax', fill=label.col), alpha=.9, + ymin='ymin', ymax='ymax', fill=color.col), alpha=.9, color=border.color, linetype=border.linetype) + geom_text(data=df, aes_string(y='labelpos', x='labelpos.x', label=label.col), hjust=text.hjust, + angle= text.angle, size=text.size, color=text.color, alpha=text.alpha, From 8ca028ea02fdde3a50c2b16599efc9a8a3e0c6b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=20Antonio=20Maga=C3=B1a=20Mesa?= Date: Sun, 17 May 2015 22:52:15 +1000 Subject: [PATCH 2/2] added doc for new parameter added column to dataset ww2 added example to ww2.R --- R/timeline.R | 1 + demo/ww2.R | 15 +++++++++++++++ timeline.dev.r | 3 +++ 3 files changed, 19 insertions(+) diff --git a/R/timeline.R b/R/timeline.R index b9418ec..6ff047e 100644 --- a/R/timeline.R +++ b/R/timeline.R @@ -10,6 +10,7 @@ #' @param df data for time periods. #' @param events data for events (optional). #' @param label.col the column name in \code{df} to use for labeling. +#' @param color.col the column name in \code{df} to use for coloring. #' @param group.col the column name in \code{df} to use for grouping. #' @param start.col the column name in \code{df} that specifies the start date. #' @param end.col the column name in \code{df} that specifies the end date. diff --git a/demo/ww2.R b/demo/ww2.R index d19fe85..1533cfe 100644 --- a/demo/ww2.R +++ b/demo/ww2.R @@ -19,3 +19,18 @@ timeline(ww2, event.spots=2, event.label='', event.above=FALSE) #Modify the ggplot2 express after returing timeline(ww2, ww2.events, text.position='center', text.angle=45) + theme(axis.text.y=element_text(angle=90, hjust=.5)) + +# Flips coordinates and prints Text instead of Person +timeline(ww2, #ww2.events, + color.col = "Person", + label.col = "Text", + text.position = "center", + text.size = 4, + text.angle = 0, + num.label.steps = 3, + event.spots = 4, + event.label.method = 1, + event.text.size = 4, + event.label = '', + event.line = TRUE, + event.above = TRUE) + coord_flip() diff --git a/timeline.dev.r b/timeline.dev.r index 5a07dd8..7b8a3cd 100644 --- a/timeline.dev.r +++ b/timeline.dev.r @@ -53,6 +53,9 @@ ww2 <- as.data.frame(matrix(c( names(ww2) <- c('Person','Group','StartDate','EndDate') ww2$StartDate <- as.Date(ww2$StartDate) ww2$EndDate <- as.Date(ww2$EndDate) +ww2$Text<-paste(gsub("[\\\n]"," ",ww2$Person),'\n',ww2$StartDate,'to',ww2$EndDate) +# will generate string such as 'Franklin D. Roosevelt\n1933-03-04 to 1945-04-12' +# does not always prints nice ww2.events <- as.data.frame(matrix(c( 'Japan Invades Manchuria','1931-09-18','Axis',