【问题标题】:How to set different scales for each group/factor in tile plot如何为平铺图中的每个组/因子设置不同的比例
【发布时间】:2016-11-10 08:53:13
【问题描述】:

我有下面给出的代码

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)

现在,我想要为每组单独的渐变比例,而不是单一比例。

【问题讨论】:

标签: r ggplot2


【解决方案1】:

ggplot2 内无法做到这一点,所以gridExtra 来救援!

library(ggplot2)
library(gridExtra)

n <- length(unique(d$panel))
l <- vector(mode = "list", length = n)
for (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)

为了说明不同的比例,改d$value[d$panel == "Group 3"] &lt;- rnorm(36)

【讨论】:

    猜你喜欢
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-16
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多