Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion R/timeline.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -57,6 +58,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],
Expand Down Expand Up @@ -187,10 +189,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,
Expand Down
15 changes: 15 additions & 0 deletions demo/ww2.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
3 changes: 3 additions & 0 deletions timeline.dev.r
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down