I have a table:
> DT
id start end place
1: id1 2014-01-01 2014-02-01 town1
2: id1 2014-02-01 2014-04-01 town2
3: id2 2014-01-01 2014-02-01 town2
4: id2 2014-03-01 2014-05-01 town4
5: id3 2014-01-01 2015-02-01 town3
I need to plot a chart with X=timeline
, Y=id
and each row should be displayed as a series of bars (length depends on start and end dates, and color depends on place).
Graph example: (without timeline labels):
For example, I'm trying
ggplot(DT,aes(start,id,colour=place))+geom_bar()
Of course it doesn't work
stat_count() must not be used with a y aesthetic.
And I need to include end date in chart.
raw data:
id1;2014-01-01;2014-02-01;town1
id1;2014-02-01;2014-04-01;town2
id2;2014-01-01;2014-02-01;town2
id2;2014-03-01;2014-05-01;town4
id3;2014-01-01;2015-02-01;town3
You mean something along
start <- c("2014-01-01","2014-02-01","2014-01-01","2014-01-01","2014-02-01")
end <- c("2014-02-01","2014-04-01","2014-02-01","2014-05-01","2014-03-01")
id <- c("id1","id1","id2","id2","id3")
evnt <- c("town1","town2","town2","town4","town3")
df<-data.frame(start,end,id,evnt)
p <- ggplot(df, aes(xmin=as.Date(start),xmax=as.Date(end),
ymin=as.numeric(id)-0.5,ymax=as.numeric(id)+0.5,
fill=evnt))+geom_rect()
p <- p+ylim(levels(df$id))
p
You're example wasn't containig the data frame in reproducible form. Hence, you might need to adapt (c.f. https://stackoverflow.com/help/mcve)