charts - Stacked bar plot in R with multiple rows per day -
i display work done in day stacked bar plot, in order see, day day, how much activity i've done in each category, y-axis representing time 0:00 23:59.
# day tstart tend duration category 1 2012-10-01 13:40 14:16 36 recreation 2 2012-10-02 10:15 10:57 42 work 3 2012-10-02 13:23 13:47 24 chores 4 2012-10-02 13:47 14:48 61 work 5 2012-10-03 09:09 11:40 151 work 6 2012-10-03 13:33 14:04 31 recreation 7 2012-10-03 17:00 19:40 160 recreation i know have convert "time start" numeric, don't know how "merge" multiple rows same day, they're making 1 bar in plot.
in (very primitive) ascii art, i'm expecting like:
23:00 22:00 21:00 20:00 19:00 c 18:00 c 17:00 c 16:00 15:00 14:00 w r 13:00 r c 12:00 11:00 w 10:00 w w 9:00 w 8:00 7:00 6:00 5:00 4:00 3:00 2:00 1:00 0:00 01 02 03 (where r, w , c bars of different colors different activites: recreation, work , chores)
in fact, beingness newbie in r plots, don't know plot function (and plot package) have at, they're holes in plot -- no activity recorded (for example) between 0:00 , 09:09, between 11:40 , 13:33, etc. on 2012-10-03...
here quick solution ggplot2 :
d <- read.table(textconnection(" day tstart tend duration category 2012-10-01 13:40 14:16 36 recreation 2012-10-02 10:15 10:57 42 work 2012-10-02 13:23 13:47 24 chores 2012-10-02 13:47 14:48 61 work 2012-10-03 09:09 11:40 151 work 2012-10-03 13:33 14:04 31 recreation 2012-10-03 17:00 19:40 160 recreation"), header=true) d$day <- as.date(d$day) d$tstart <- as.posixct(d$tstart, format="%h:%m") d$tend <- as.posixct(d$tend, format="%h:%m") library(ggplot2) library(scales) g <- ggplot(data=d, aes()) + geom_segment(aes(x=day,xend=day,y=tstart,yend=tend,color=category),size=20) + scale_x_date(labels = date_format("%d")) g + scale_y_datetime(limits=c(as.posixct("00:00", format="%h:%m"),as.posixct("23:59", format="%h:%m")), labels = date_format("%h:%m")) which gives :
edited : y axis in initial reply wrong.
r charts plot data-visualization bar-chart
No comments:
Post a Comment