【问题标题】:ggplot2: More complex facetingggplot2:更复杂的刻面
【发布时间】:2015-03-27 23:25:52
【问题描述】:

我有一个越来越复杂的热图。融合数据示例:

head(df2)
  Class     Subclass         Family               variable value
1     A chemosensory family_1005117 caenorhabditis_elegans    10
2     A chemosensory family_1011230 caenorhabditis_elegans     4
3     A chemosensory family_1022539 caenorhabditis_elegans    10
4     A        other family_1025293 caenorhabditis_elegans    NA
5     A chemosensory family_1031345 caenorhabditis_elegans    10
6     A chemosensory family_1033309 caenorhabditis_elegans    10
tail(df2)
     Class Subclass        Family        variable value
6496     C  class c family_455391 trichuris_muris     1
6497     C  class c family_812893 trichuris_muris    NA
6498     F  class f family_225491 trichuris_muris     1
6499     F  class f family_236822 trichuris_muris     1
6500     F  class f family_276074 trichuris_muris     1
6501     F  class f family_768194 trichuris_muris    NA

使用 ggplot2 和 geom_tile,我能够生成漂亮的数据热图。我为代码感到自豪(这是我在 R 中的第一次体验),所以把它贴在下面:

df2[df2 == 0] <- NA
df2[df2 > 11] <- 10
df2.t <- data.table(df2)
df2.t[, clade := ifelse(variable %in% c("pristionchus_pacificus", "caenorhabditis_elegans", "ancylostoma_ceylanicum", "necator_americanus", "nippostrongylus_brasiliensis", "angiostrongylus_costaricensis", "dictyocaulus_viviparus", "haemonchus_contortus"), "Clade V",
                 ifelse(variable %in% c("meloidogyne_hapla","panagrellus_redivivus", "rhabditophanes_kr3021", "strongyloides_ratti"), "Clade IV",
                 ifelse(variable %in% c("toxocara_canis", "dracunculus_medinensis", "loa_loa", "onchocerca_volvulus", "ascaris_suum", "brugia_malayi", "litomosoides_sigmodontis", "syphacia_muris", "thelazia_callipaeda"), "Clade III",
                 ifelse(variable %in% c("romanomermis_culicivorax", "trichinella_spiralis", "trichuris_muris"), "Clade I",
                 ifelse(variable %in% c("echinococcus_multilocularis", "hymenolepis_microstoma", "mesocestoides_corti", "taenia_solium", "schistocephalus_solidus"), "Cestoda",
                 ifelse(variable %in% c("clonorchis_sinensis", "fasciola_hepatica", "schistosoma_japonicum", "schistosoma_mansoni"), "Trematoda", NA))))))]
df2.t$clade <- factor(df2.t$clade, levels = c("Clade I", "Clade III", "Clade IV", "Clade V", "Cestoda", "Trematoda"))
plot2 <- ggplot(df2.t, aes(variable, Family))
tile2 <- plot2 + geom_tile(aes(fill = value)) + facet_grid(Class ~ clade, scales = "free", space = "free")
tile2 <- tile2 + scale_x_discrete(expand = c(0,0)) + scale_y_discrete(expand = c(0,0))
tile2 <- tile2 + theme(axis.text.y = element_blank(), axis.ticks.y = element_blank(), legend.position = "right", axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.55), axis.text.y = element_text(size = rel(0.35)), panel.border = element_rect(fill=NA,color="grey", size=0.5, linetype="solid"))
tile2 <- tile2 + xlab(NULL)
tile2 <- tile2 + scale_fill_gradientn(breaks = c(1,2,3,4,5,6,7,8,9,10),labels = c("1", "2", "3", "4", "5", "6", "7", "8", "9", ">10"), limits = c(1, 10), colours = palette(11), na.value = "white", name = "Members")'

如您所见,这涉及到相当多的手动重新排序,否则代码非常简单。这是图像输出:

但是,您可能会注意到一整列信息“子类”没有被使用。基本上,每个子类都适合一个类。如果我能够在已经显示的类方面对这些子类进行分面,那将是完美的。据我所知,这是不可能的。准确地说,只有 A 类有不同的子类。其他类只是将它们的类名镜像(F = f 类)。有没有另一种方法来组织这个热图,以便我可以显示所有相关信息?缺少的子类包含一些最关键的数据,并且对于从数据中进行推断是最必要的。

另一种方法是分面子类而不是类,手动重新排序它们以便类聚集在一起,然后在它们周围绘制某种框来划分每个类。我不知道这是怎么做到的。

任何帮助都会非常有用。如果您需要任何其他信息,请告诉我。

【问题讨论】:

  • 你有facet_grid(Class ~ clade,的地方,改成facet_grid(Class + Subclass ~ clade,
  • 不过,对于标签的排序,您可能需要Subclass + Class
  • 这是一种方法,谢谢。这并不完全是我所设想的(嵌套方面),因为它只是将其分解为子类,然后添加一个类标签。我想它没有我想要的那么漂亮,但也许它已经很好了。感谢您的意见,@Gregor。
  • 如果不是这样,我想我不确定你在追求什么。

标签: r ggplot2 heatmap facet


【解决方案1】:

这将在原始条带的右侧和图例的左侧放置一个新条带。

library(ggplot2)
library(gtable)
library(grid)

p <- ggplot(mtcars, aes(mpg, wt, colour = factor(vs))) + geom_point()
p <- p + facet_grid(cyl ~ gear)

# Convert the plot to a grob
gt <- ggplotGrob(p)

# Get the positions of the right strips in the layout: t = top, l = left, ...
strip <-c(subset(gt$layout, grepl("strip-r", gt$layout$name), select = t:r))

#  New column to the right of current strip
gt <- gtable_add_cols(gt, gt$widths[max(strip$r)], max(strip$r))  

# Add grob, the new strip, into new column
gt <- gtable_add_grob(gt, 
  list(rectGrob(gp = gpar(col = NA, fill = "grey85", size = .5)),
  textGrob("Number of Cylinders", rot = -90, vjust = .27, 
        gp = gpar(cex = .75, fontface = "bold", col = "black"))), 
        t = min(strip$t), l = max(strip$r) + 1, b = max(strip$b), name = c("a", "b"))

# Add small gap between strips
gt <- gtable_add_cols(gt, unit(1/5, "line"), max(strip$r))

# Draw it
grid.newpage()
grid.draw(gt)

【讨论】:

    【解决方案2】:

    用一些简单的演示数据将我的评论变成答案:

    这并不难(?facet_grid 中甚至还有示例,尽管它们位于底部)。

    # generate some nested data
    dat = data.frame(x = rnorm(12), y = rnorm(12), class = rep(LETTERS[1:2], each = 6),
                     subclass = rep(letters[1:6], each = 2))
    
    # plot it
    ggplot(dat, aes(x, y)) + geom_point() +
        facet_grid(subclass + class ~ .)
    

    您可以在~ 的任一侧使用任意多个因素来做到这一点!

    【讨论】:

    • 功能上,这正是我想要的。从美学上讲,这不是我想要的。但这可能是我的问题,而不是 ggplot2 的问题。我不喜欢它如何为每个嵌套的子类重复类标签。我说得有道理吗?对我来说,正确的做法似乎是让一个更大的类栏(灰色标签区域)跨越它包含的全部数据,其中包含嵌套子类标签的子集。 @Gregor
    • @Nic 是的,我现在明白了。问题是ggplot不知道因子是嵌套的,所以不管是嵌套还是交叉,解决方法都是通用的。
    • @Sandy 是的!这正是我正在寻找的。谢谢!
    • @Sandy 我想我没有足够的代表来评论源...无论如何,我认为我能够将 grob 定位在正确的位置,但现在它比它应该是。我认为发生的事情是它被放置在与图例相同的列中,相对较宽,因此它完全覆盖了图例并跨越了相当大的区域。我想这与原始 gtable 的组织有关。不幸的是,表格太大以至于 gtable_show_layout 不是很有帮助。你能帮我吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    • 2015-12-16
    相关资源
    最近更新 更多