[R] drawing a ... barplot (?) along time

Jim Lemon drjimlemon at gmail.com
Tue Dec 6 22:25:44 CET 2016


Hi Dagmar,
Having recovered somewhat, I can refine my suggestion a bit:

datframe <- data.frame(Name=c("Kati","Kati","Kati","Leon","Leon","Leon"),
 changepoint=as.POSIXct(strptime(c("03.01.2011","05.01.2011", "27.01.2011",
  "26.01.2011","28.01.2011", "28.02.2011"), "%d.%m.%Y")),
 knownstate =c("breeding",NA, NA,"breeding","moulting",NA))
library(plotrix)
tm.info<-list(labels=datframe$Name,
 starts=c(as.POSIXct(strptime("01.01.2011","%d.%m.%Y")),
 datframe$changepoint[1:2],
 as.POSIXct(strptime("24.01.2011","%d.%m.%Y")),
 datframe$changepoint[4:5]),
 ends=datframe$changepoint)
gantt.chart(tm.info,
 xlim=as.POSIXct(strptime(c("1.1.2011","10.3.2011"),"%d.%m.%Y")),
 taskcolors=c(2,3,3,2,"orange",3))
legend(as.POSIXct(strptime("10.02.2011","%d.%m.%Y")),2,
 c("Breeding","Moulting","Loafing"),fill=c(2,"orange",3))

Jim

On Tue, Dec 6, 2016 at 10:18 PM, Jim Lemon <drjimlemon at gmail.com> wrote:
> Hi Dagmar,
> I think you want something like a gantt.chart. I know this is wrong in
> some ways, but it is late and I must retire:
>
> datframe <- data.frame(Name=c("Kati","Kati","Kati","Leon","Leon","Leon"),
>  changepoint=as.Date(c("03.01.2011","05.01.2011", "27.01.2011",
>   "26.01.2011","28.01.2011", "28.02.2011"), "%d.%m.%Y"),
>  knownstate =c("breeding",NA, NA,"breeding","moulting",NA))
> library(plotrix)
> tm.info<-list(labels=datframe$Name,
>  starts=c(as.Date("01.01.2011","%d.%m.%Y"),datframe$changepoint[1:2],
>  as.Date("01.01.2011","%d.%m.%Y"),datframe$changepoint[4:5]),
>  ends=datframe$changepoint)
> gantt.chart(tm.info,xlim=as.Date(c("1.1.2011","1.3.2011"),"%d.%m.%Y"),
> taskcolors=c(2,3,3,2,"orange",3))
>
> Jim
>
> On Tue, Dec 6, 2016 at 9:28 PM, Dagmar <Ramgad82 at gmx.net> wrote:
>> # Dear all,
>> # I hope someone can help me with this, I am kind of desperated even though
>> it doesn't sound tooooo complicated at all. Let's see:
>> # I have a data frame like this:
>> datframe <- data.frame(Name=c("Kati","Kati","Kati","Leon","Leon","Leon"),
>>                        changepoint =as.POSIXct
>> (strptime(as.character("03.01.2011","05.01.2011", "27.01.2011",
>> "26.01.2011","28.01.2011", "28.02.2011"), "%d.%m.%Y")),
>>                        knownstate =c("breeding",NA,
>> NA,"breeding","moulting",NA))
>> datframe
>> str(datframe)
>> # Now I want a stacked horizontal bar diagram: One bar-line for animal Kati
>> and bellow one for animal Leon.
>> # Each bar should be "drawn" along the time (on the x-axes) and be
>> interupted (by a vertical line) at the changepoints
>> # then additionally I want colours for the known states (e.g. red for the
>> time from 03.01.2011 to 05.01.2011 when animal Kati was breeding)
>>
>> # help would be much appreciated!
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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