我可以在 R facet plot 中保持条形宽度不变,每个 facet 中的类别数量不同吗?

Can I keep bar width constant in R facet plot with different number of categories in each facet?

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

# 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",
      "Transmembrane helix",
      "Transmembrane",
      "translation",
      "glycolytic process",
      "translation",
      "glycolytic process"
    ),
    Term = c(
      "UP_Keyword",
      "UP_Keyword",
      "UP_Keyword",
      "UP_Keyword",
      "UP_Keyword",
      "UP_Keyword",
      "GO_term",
      "GO_term",
      "GO_term",
      "GO_term"
    ),
    Reg = c(
      "Up",
      "Up",
      "Up",
      "Down",
      "Down",
      "Down",
      "Up",
      "Up",
      "Down",
      "Down"
    ),
    Reg_val = c(63, 341, 344, 3, 12,
                9, 41, 9, 0, 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

如果有人能解决我的问题,我将不胜感激。请尝试用普通生物学家能理解的方式解释。

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

的论点

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

library(ggplot2)

df <- data.frame(
  Category = c("Transport", "Transmembrane helix", "Transmembrane",
               "Transport", "Transmembrane helix", "Transmembrane",
               "translation", "glycolytic process",
               "translation", "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, 3, 12, 9, 41, 9, 0, 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")

plot + ggforce::facet_col(~ Term, space = "free", scales = "free_y")

reprex package (v0.3.0)

于 2021-01-03 创建