How to create a time series plot in the style of a horizontal stacked bar plot in r -


i create horizontal ‘stacked bar’ type plot in date runs along x-axis , samples appear bars on y-axis. in simple example below, have 3 samples (a, b, c) each containing 3 values (0, 1, 2). horizontal bar coloured based on value @ each time step along x-axis, end 3 horizontal bars (one each sample) run first last time point , contain series of blocks colours relate different values.

for example, want value 0 blue, value 1 yellow , value 2 red: sample a, first 2 days of trace blue, next 2 days yellow, followed single blue , on……

example data:

df <- structure(list(date = c("30/04/2011", "01/05/2011", "02/05/2011", "03/05/2011", "04/05/2011", "05/05/2011", "06/05/2011", "07/05/2011", "08/05/2011", "09/05/2011", "10/05/2011", "11/05/2011", "12/05/2011", "13/05/2011", "14/05/2011", "15/05/2011", "16/05/2011", "17/05/2011", "18/05/2011", "19/05/2011", "20/05/2011", "21/05/2011", "22/05/2011", "23/05/2011", "24/05/2011", "25/05/2011", "26/05/2011", "27/05/2011", "28/05/2011", "29/05/2011", "30/05/2011", "31/05/2011", "01/06/2011", "02/06/2011", "03/06/2011", "04/06/2011", "05/06/2011", "06/06/2011", "07/06/2011", "08/06/2011", "09/06/2011", "10/06/2011", "11/06/2011", "12/06/2011", "13/06/2011", "14/06/2011", "15/06/2011", "16/06/2011", "17/06/2011", "18/06/2011", "19/06/2011", "20/06/2011", "21/06/2011", "22/06/2011", "23/06/2011", "24/06/2011", "25/06/2011", "26/06/2011", "27/06/2011", "28/06/2011", "29/06/2011", "30/06/2011", "01/07/2011", "02/07/2011", "03/07/2011", "04/07/2011", "05/07/2011", "06/07/2011", "07/07/2011", "08/07/2011", "09/07/2011", "10/07/2011", "11/07/2011", "12/07/2011", "13/07/2011", "14/07/2011", "15/07/2011", "16/07/2011", "17/07/2011", "18/07/2011", "19/07/2011", "20/07/2011", "21/07/2011", "22/07/2011", "23/07/2011", "24/07/2011"), = c(0l, 0l, 1l, 1l, 0l, 1l, 0l, 0l, 0l, 1l, 0l, 0l, 0l, 1l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l,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# 3 02/05/2011 1 1 0 # 4 03/05/2011 1 0 0 # 5 04/05/2011 0 0 0 

this must easy thing achieve can’t head around (i.e. bar plot doesn't seem work this). appreciated. thanks!

for ggplot2 plot first convert df long form (using melt reshape2 package), convert date column "date" class , value column factor , use geom_tile:

library(ggplot2) library(reshape2)  long <- melt(df, measure.var = 2:4) long <- transform(long, date = as.date(long$date, "%d/%m/%y"), value = factor(value))  ggplot(long, aes(date, variable)) +     geom_tile(aes(fill = value)) +     scale_fill_manual(values = c("blue", "yellow", "red")) 

screenshot


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -