我可以在 R 分面图中保持条宽不变,每个分面中有不同数量的类别吗?

问题描述

我希望有人能帮助我解决我的问题:

  • 我的数据由包含许多上调和下调基因的类别组成。我希望它们显示为水平堆叠的列。那行得通
  • 我使用两种方法(术语)、UP_Keyword 和 GO-Term 对生成这些基因类别进行排序。他们产生了不同数量的基因类别。使用 facet wrap 没问题。
# A tibble: 10 x 4
   Category             Term       Reg   Reg_val  
   <chr>                 <chr>      <chr>   <dbl> 
 1 Transport            UP_Keyword Up         63  
 2 Transmembrane helix  UP_Keyword Up        341  
 3 Transmembrane        UP_Keyword Up        344  
 4 Transport            UP_Keyword Down        3  
 5 Transmembrane helix  UP_Keyword Down       12  
 6 Transmembrane        UP_Keyword Down        9  
 7 translation          GO_term    Up         41  
 8 glycolytic process   GO_term    Up          9  
 9 translation          GO_term    Down        0  
10 glycolytic process   GO_term    Down        1  
structure(
  list(
    Category = c(
      "Transport","Transmembrane helix","Transmembrane","Transport","translation","glycolytic process","glycolytic process"
    ),Term = c(
      "UP_Keyword","UP_Keyword","GO_term","GO_term"
    ),Reg = c(
      "Up","Up","Down","Down"
    ),Reg_val = c(63,341,344,3,12,9,41,1)
  ),row.names = c(NA,-10L),class = c("tbl_df","tbl","data.frame")
)
ex_plot <- ggplot(example_data,aes(x=Reg_val,y=Category,fill=Reg))+
  geom_col(width=0.5)+
  facet_wrap(.~Term,nrow=2,scales="free")

This is how my plot looks now

如果有人能解决我的问题,我将不胜感激。请尽量用简单的生物学家能理解的方式来解释。

解决方法

char 指定方面的方式不允许使用 facet_wrap() 参数;这是 space = "free" 的参数。

根据您希望条带标签/方面标题的位置,您可以使用 facet_grid() 或使用 ggforce 包中的 facet_grid()。示例如下。

facet_row()

library(ggplot2)

df <- data.frame(
  Category = c("Transport","Transmembrane helix","Transmembrane","Transport","translation","glycolytic process","glycolytic process"),Term = rep(c("UP_Keyword","GO_term"),c(6,4)),Reg = rep(c("Up","Down","Up","Down"),c(3,3,2,2)),Reg_val = c(63,341,344,12,9,41,1)
)

# Make the core plot
plot <- ggplot(df,aes(Reg_val,Category,fill = Reg)) +
  geom_col(width = 0.5)

plot + facet_grid(Term ~ .,scales = "free",space = "free")

reprex package (v0.3.0) 于 2021 年 1 月 3 日创建

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...