Color-coded timeline plot in R -
i working on visualising user touch interactions within mobile application. plot them in one-dimensional timeline color-coded action type.
i.e., 2 variables time (in ms, on x-axis) , type of touch (used color-code line). colors should able repeated.
ideally plot this, , have found this previous question delivers similar, i'm wondering if there isn't better.
is there other methods using r allow me this?
i have workings of base r method. it's bit hacky, tweaking should work nicely. have removed reliance on data.table
, timeline
, , ggplot
packages, opting using polygon
function.
df <- data.frame(set=c("x","y","z","x","y","z","x","y","z","x","y","z","x"), startdate=c(1380708900,1402963200,1420070400,1421280000,1410912000,1396310400,1397520000,1418860800,1404172800,1405382400,1395100800,1412121600,1413331200), enddate= c(1395099900,1404171900,1421279100,1430985600,1412120700,1397519100,1402962300,1420069500,1405381500,1410911100,1396309500,1413330300,1418859900)) df$startlabel <- as.posixct(df$startdate,tz="utc",origin="1970-01-01") df$set <- as.factor(df$set)
above sets data.frame
object.
this sets plotting canvas
plot(c(df$startdate[1], tail(df$enddate,1)), c(0.4,0.65), type='n', xlab='time', ylab='', axes=false)
this clunk-ily loops through values , draws colored polygon each period of time each observation
for(i in 1:nrow(df)){ polygon(x=c(rep(df$startdate[i],2), rep(df$enddate[i],2)), y=c(0.6,0.4,0.4,0.6), col=rainbow(3, alpha=0.75)[df$set[i]], border=rainbow(3)[df$set[i]]) }
then add in axis (this use tweaking depending on how of date information care show.
axis(1, labels=df$startlabel, at=df$startdate)
then sets legend @ top of plot.
legend('top', horiz=true, c('test set', 'training set', 'validation set'), pch=16, col=rainbow(3), bty='n', x.intersp=1.5, title='group')
hopefully helps.
Comments
Post a Comment