问:facet_wrap 每个图块的比例

Q: facet_wrap scales per tile

我有两种类型的参数和一种化合物的响应:

用于生成此图片的代码是

    for (i in levels(data$ProteinName))
    {
      temp <- subset(data, data$ProteinName == i)
            plot <- ggplot(data = temp, aes(x= temp$id, y = temp$Matrix))+
              geom_tile( aes( fill= temp$TotalArea))+
              labs(title= i, x = NULL, y = NULL, fill = "Average Total Area")+
              geom_text(aes(label=round(TotalArea, digits = 0)), color = "White")+
              scale_fill_gradientn (colors=c(low = "blue4", mid="gold", high = "red"),
              na.value = "violetred")+
              theme_bw()
       print(plot)
    }

但这是 12 个图之一,所以对于我的报告,我不得不将它纳入一个方面,但我还没有找到任何方法来为 "z axis" 比例创建自由比例,当前代码是

    temp <- data
            plot <- ggplot(data = temp, aes(x= temp$id, y = temp$Matrix))+
              facet_wrap(~temp$ProteinName, scale = "free")+
              geom_tile( aes( fill= temp$TotalArea))+
              labs(title= i, x = NULL, y = NULL, fill = "Average Total Area")+
              geom_text(aes(label=round(TotalArea, digits = 0)), color = "White")+
              scale_fill_gradientn (colors=c(low = "blue4", mid="gold", high = "red"), 
              na.value = "violetred")+
              theme_bw()
       print(plot)

并给出以下内容:

但这里是瓷砖的颜色(z 轴)不是自由的有没有人知道如何创建一个自由的 z 轴? 你可以看到 PE 面的广告,它只是蓝色,但在这个面内,与观察到的浓度有很大差异。

目标是让读者可以看到最大的响应(红色)和最低的响应(蓝色)。

希望你能帮助我。

感谢您的回答, 在 post 的帮助下: Place a legend for each facet_wrap grid in ggplot2 这很容易完成, 旧脚本是:

for (i in levels(data$ProteinName))
{
  temp <- subset(data, data$ProteinName == i)
        plot <- ggplot(data = temp, aes(x= temp$id, y = temp$Matrix))+
          geom_tile( aes( fill= temp$TotalArea))+
          labs(title= i, x = NULL, y = NULL, fill = "Average Total Area")+
          geom_text(aes(label=round(TotalArea, digits = 0)), color = "White")+
          scale_fill_gradientn (colors=c(low = "blue4", mid="gold", high = "red"),
          na.value = "violetred")+
          theme_bw()
   print(plot)
}

结果: old plot

之后我们用 gridextra 包中的函数 grid.arrange(grobs = list_of_plots) 更新了脚本 因为我不得不在循环中制作一个列表,这是通过以下方式完成的:plot_v[[i]] <- plot,plot_v 是地块列表的名称。

此列表随后被添加到网格排列中

所以新脚本现在是

    require(gridExtra)
    require(ggplot2)    
plot_v <- list()
        for (i in levels(data$ProteinName))
        {
          temp <- subset(data, data$ProteinName == i)
                plot <- ggplot(data = temp, aes(x= temp$id, y = temp$Matrix))+
                  geom_tile( aes( fill= temp$TotalArea))+
                  labs(title= i, x = NULL, y = NULL, fill = "Average Total Area")+
                  geom_text(aes(label=round(TotalArea, digits = 0)), color = "White", size= 2.5)+
                  scale_fill_gradientn (colors=c(low = "blue4", mid="gold", high = "red"),
                     na.value = "violetred")+
                  theme_bw()+
                  guides(fill = "none")
           print(plot)
           assign(paste("plot", i, sep="_"), plot)
           plot_v[[i]] <-  plot
        }
        grid.arrange(grobs = plot_v)

这给出了结果 new plot

我要感谢你的帮助