[R] Gantt chart like graphics

Jim Lemon jim at bitwrit.com.au
Thu May 29 14:30:11 CEST 2008


kljosc wrote:
> Dear R Community,
> 
> I have a dataframe like this
> 
> dat           product1 product2 ... productn
> 01.1.2008  1           1               1
> 02.1.2008  1           1               2
> .
> 15.2.2008  2           2               NA
> .
> 04.4.2008  2           2               1
> 05.4.2008  NA         2               NA
> 
> (date ascending order, 1:n products with status 1, 2 or NA)
> 
> and want to produce a graphic like this, where 1 or 2 are colored
> rectangles, NAs are white:
> 
> product1 11..........2............2_
> product2 11..........2............22
> .
> .
> productn 12.........._............1_
>                  time_axis (=dat) ->
> 
> which looks like a Gantt chart with multiple time slots. I have tried it
> with image() which looks good, but I don't know how to produce an y-axis
> with labels product1, etc. and a x-axis with date labels.
> Further, I want to draw a scatterplot below (using layout()) with the same
> x-axis. 
> Maybe someone has a better idea which is easier to label?
> 
Hi Klemens,
While this looks like a Gantt chart, you'll have to massage your data a 
bit to use the gantt.chart function. You would have to define start and 
end dates. As it looks like your data are in daily increments, it would 
probably be easier to use plot and mark each occurrence with a square 
filled with the color corresponding to your productx values. Let's see 
now, call your dataframe kl.df...

dates<-strptime(kl.df$dat,format="%d.%m.%Y")
datesP<-as.POSIXct(dates)
kldim<-dim(kl.df)
plot(0,xlim=range(datesP),ylim=c(1,2),type="n",xaxt="n")
axis(1,labels=dates,at=datesP)
for(i in 2:kldim[2])
  points(dates,kl.df[,i],pch=22,bg=kl.df[,i])

pretty rough, but it should give you a start.

Jim



More information about the R-help mailing list