R ggplot2:更复杂的镶嵌面

R ggplot2:更复杂的镶嵌面,r,ggplot2,heatmap,facet,R,Ggplot2,Heatmap,Facet,我有一张越来越复杂的热图。熔化数据的一个示例: 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

我有一张越来越复杂的热图。熔化数据的一个示例:

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")'

df2[df2==0]11]用一些简单的演示数据将我的评论变成答案:

这并不难(在
?facet\u grid
中甚至有一些示例,尽管它们位于底部)


您可以使用
~
两侧任意多个因素来执行此操作

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

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)
库(ggplot2)
图书馆(gtable)
图书馆(网格)

p如果您有
facet\u网格(Class~clade,
),请将其更改为
facet\u网格(Class+Subclass~clade,
),但对于标签的排序,您可能需要
Subclass+Class
。这是一种方法,谢谢。这与我预想的不完全一样(嵌套facets),因为它只是将它分解成子类,然后添加一个类标签。我想它没有我想要的那么漂亮,但也许它已经足够好了。谢谢你的输入,@Gregor。如果不是这样的话,我想我不确定你想要什么。从功能上来说,这正是我想要的。从美学上来说,这不是我想要的。但这可能是我想要的问题是,不是ggplot2。我不喜欢它如何为每个嵌套的子类重复类标签。我说得通吗?对我来说,这样做的正确方法似乎是有一个更大的类栏(灰色标签区域)使用嵌套子类标签的子集跨越它所包含的整个数据@Gregor@Nic是的,我现在明白了。问题是,
ggplot
不知道这些因子是嵌套的,所以解决方案对于它们是嵌套的还是交叉的都是通用的。@Nic,类似这样的@Sandy Yes!这正是我要找的。谢谢!@Sandy,我想我没有足够的代表对消息来源发表评论……无论如何,我认为我能够将grob定位在正确的位置,但现在它比它应该的大得多。我认为现在发生的事情是,它被放置在与传奇相同的列中,相对较宽,因此它完全覆盖了传奇和传奇跨越相当大的区域。我想这与原始GTTable的组织有关。不幸的是,表格太大,GTTable_show_布局没有多大帮助。你能帮我吗?
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)