[R] plotting a timeline
Peter Ehlers
ehlers at ucalgary.ca
Tue Nov 23 01:22:11 CET 2010
On 2010-11-20 14:26, Marcin Gomulka wrote:
> I was trying to recreate this kind of timeline plot:
> http://www.vertex42.com/ExcelArticles/create-a-timeline.html
>
> As you can see in their excel example, the events are nicely placed out on
> both sides of the timeline axis.
>
> AFAIK there is no function to do this nicely in R-project. Furthermore,
> graphics and lattice packages are unable to draw the x-axis in the middle of
> the plot. (datapoints cannot be plotted below the axis, as in the Excel
> example).
>
> My question: Is there a function to draw the x-axis inside the plot? (at a
> certain vertical position?) Is there a function for the whole timeline plot
> that I do not know about?
>
> I tried to visually replicate the plot using additional elements to create
> my own x-axis (code below). I have placed the x-axis ticks on a fixed
> y-height (-0.1 and -0.05), but this might look badly with a different
> dataset or at other image proportions. I'd rather do this with a dedicated
> package function ( like axis() ).
>
It wouldn't be difficult to write such a function.
Here's some code to get you started:
with(the_data, {
plot(eventtime, impact, type="h", axes=FALSE,
ann=FALSE, col="grey", ylim=c(-.7,1.2))
points(eventtime, impact, pch=95, font=5, cex=2, col=4)
text(eventtime, impact, label, pos = 1 + 2*(impact > 0))
})
abline(h=0, lwd=2)
axis(1, pos=0, lwd=2, lwd.ticks=1)
Peter Ehlers
> --
> mrgomel
> -----------------------
> Below is my example code in R:
>
>
> the_data<-
> structure(list(eventtime = c(1914L, 1917L, 1918L, 1939L, 1945L,
> 1963L, 1989L, 2001L, 2003L), impact = c(1, -.5, 0.8, 1, 0.8, 0.5,
> -.5, 0.5, 1), label = structure(c(8L, 7L, 4L, 9L, 5L, 2L, 3L,
> 1L, 6L), .Label = c("9/11", "Cuban crisis", "end of communism",
> "end WW1", "end WW2", "Iraq war", "start of communism", "WW1",
> "WW2"), class = "factor")), .Names = c("eventtime", "impact",
> "label"), class = "data.frame", row.names = c(NA, -9L))
>
>
> plot(the_data$eventtime, the_data$impact, type="h", frame.plot=FALSE, axes =
> FALSE, xlab="",ylab="", col="grey")
> text(the_data$eventtime,the_data$impact, the_data$label)
> #axis(1)
> abline(h=0,lwd=2)
> text(axTicks(1),-0.1, axTicks(1))
> points(axTicks(1),rep(-0.05,length(axTicks(1))), type="h")
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list