我可以在 R facet plot 中保持条形宽度不变,每个 facet 中的类别数量不同吗?
Can I keep bar width constant in R facet plot with different number of categories in each facet?
我希望有人能帮我解决我的问题:
- 我的数据由包含许多上调和下调基因的类别组成。我希望它们显示为水平堆叠的列。行得通
- 我用两种方法(Terms)对这些基因类别进行了排序,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",
"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
- 我希望条形在所有方面都具有相同的宽度。有办法吗?
- 我在这里找到了一个可能的解决方案:https://community.rstudio.com/t/preserve-constant-bar-widths-for-uneven-faceted-plots/26837
但是,简单地在 facet_wrap() 中添加 space="free" 不会导致任何变化,但会出现警告:未使用的参数 (space = "free")
如果有人能解决我的问题,我将不胜感激。请尝试用普通生物学家能理解的方式解释。
指定方面的 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 创建
我希望有人能帮我解决我的问题:
- 我的数据由包含许多上调和下调基因的类别组成。我希望它们显示为水平堆叠的列。行得通
- 我用两种方法(Terms)对这些基因类别进行了排序,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",
"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
- 我希望条形在所有方面都具有相同的宽度。有办法吗?
- 我在这里找到了一个可能的解决方案:https://community.rstudio.com/t/preserve-constant-bar-widths-for-uneven-faceted-plots/26837 但是,简单地在 facet_wrap() 中添加 space="free" 不会导致任何变化,但会出现警告:未使用的参数 (space = "free")
如果有人能解决我的问题,我将不胜感激。请尝试用普通生物学家能理解的方式解释。
指定方面的 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 创建