r - How to set different scales for each group/factor in tile plot -
i have code given below
d = data.frame(sites=rep(paste("s", 1:31),each=12), value=runif(31*12), panel=c(rep("group 1",16*12), rep("group 2", 12*12), rep("group 3", 3*12))) ggplot(d, aes(x = sites, y = factor(0))) + geom_tile(aes(fill = value)) + scale_fill_gradient(low = "green", high = "blue") + facet_wrap(~ panel, ncol = 1)
now instead of single scale, want separate gradient scales each group.
there's no way within ggplot2
, gridextra
rescue!
library(ggplot2) library(gridextra) n <- length(unique(d$panel)) l <- vector(mode = "list", length = n) (i in 1:n) { dd <- d dd[d$panel!=unique(d$panel)[i], "value"] <- na l[[i]] <- ggplot(dd, aes(x = sites, y = 0)) + geom_tile(aes(fill = value)) + scale_fill_gradient(low = "green", high = "blue", na.value = na) } grid.arrange(grobs = l, ncol = 1)
to illustrate different scales, change d$value[d$panel == "group 3"] <- rnorm(36)
:
Comments
Post a Comment