Reputation: 325
I have a dataframe with which I'm trying to use geom_tile to show the differences in the column "value" for each point in time "day" (x axis) across each site "zid" (y axis).
Now the problem is that for each site and each time interval, I may or may not have another observation with a different treatment (column "Num" in the data sample below). I would like to make my geom_tile plot such that each of the two treatments can be stacked on top of each other yet corresponding to the same point on the y axis (column "zid" in the data sample below). In order to differentiate the two treatments corresponding to the same zid and same day, I would like to use two separate color schemes for each of the treatments.
I've scoured the web and cant find anyway to do this. I'm not opposed to using another geom structure like geom_rect if anyone has any ideas as to how to achieve what I'm looking for.
EDIT: Per the advice by @JasonAizkalns, here is a hand-drawn mockup of what I'm looking for -- except instead of two scales of solid colors for the two treatments, I'd like two gradient scales (which I couldn't render with my magic markers).
EDIT2: I've tried faceting, but it's far from what I'm hoping for (see comment below.)
The ggplot code I've used is here, followed by the reproducible data:
ggplot(dat, aes(x=as.factor(zid), y=day))+
geom_tile(aes(ymin=day,ymax=day+1, fill=value), color="white", linetype=5)+
scale_fill_gradient2(low = "red",high = "steelblue")+
coord_flip()
Data sample:
dat <- structure(list(zid = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 6L, 6L, 6L), variable = structure(c(1L, 2L, 9L, 10L, 11L,
12L, 13L, 15L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L,
28L, 29L, 30L, 31L, 32L, 33L, 29L, 32L, 33L, 34L, 35L, 36L, 37L,
33L, 34L, 35L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 32L,
33L, 34L), .Label = c("X237", "X238", "X239", "X240", "X241",
"X242", "X243", "X244", "X245", "X246", "X247", "X248", "X249",
"X250", "X251", "X252", "X253", "X254", "X255", "X256", "X257",
"X258", "X259", "X260", "X261", "X262", "X263", "X264", "X265",
"X266", "X267", "X268", "X269", "X270", "X271", "X272", "X273",
"X274", "X275", "X276", "X277", "X282", "X283", "X284", "X285",
"X286", "X287", "X288"), class = "factor"), value = c(350L, 456L,
779L, 594L, 370L, 192L, 132L, 471L, 363L, 290L, 406L, 355L, 342L,
329L, 208L, 207L, 250L, 441L, 384L, 432L, 269L, 275L, 392L, 306L,
367L, 208L, 241L, 226L, 212L, 237L, 210L, 252L, 274L, 297L, 292L,
281L, 308L, 291L, 280L, 431L, 315L, 447L, 213L, 532L, 372L, 693L
), num = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 2L,
1L), day = c(237L, 238L, 245L, 246L, 247L, 248L, 249L, 251L,
254L, 255L, 256L, 257L, 258L, 259L, 260L, 261L, 262L, 263L, 264L,
265L, 266L, 267L, 268L, 269L, 265L, 268L, 269L, 270L, 271L, 272L,
273L, 269L, 270L, 271L, 263L, 264L, 265L, 266L, 267L, 268L, 269L,
270L, 271L, 268L, 269L, 270L)), .Names = c("zid", "variable",
"value", "num", "day"), row.names = c(3L, 6L, 9L, 12L, 15L, 18L,
21L, 24L, 27L, 30L, 33L, 36L, 39L, 42L, 45L, 48L, 51L, 54L, 57L,
60L, 63L, 66L, 69L, 72L, 75L, 78L, 81L, 84L, 87L, 90L, 93L, 96L,
99L, 102L, 117L, 120L, 123L, 126L, 129L, 132L, 135L, 138L, 141L,
144L, 147L, 150L), class = "data.frame")
Upvotes: 2
Views: 999
Reputation: 325
From User20650's ingenious answer below in the comments:
library(grid)
ggplot(dat, aes(x=factor(num), y=day)) + geom_tile(aes(ymin=day, ymax=day+1, fill=value), color="white", linetype=5) + scale_fill_gradient2(low = "red",high = "steelblue") + coord_flip() + facet_grid(zid~.) + theme(panel.margin = unit(0, "null")).
Upvotes: 1