英文:
How to draw facet_wrap with labels like facet_grid (independent y-axis)
问题
I have data on gene expression levels (TPM value), which matrix consists of 2 cell lines and type. (experiment VS control)
I'd like to compare the TPM value between KD and the experiment(KD) for each gene type. Additionally, because the two cell lines share the same gene type, I also want to draw them parallel. To draw them with a free-y-axis, I should use facet_wrap instead of facet_grid but for facet_wrap, I found that I cannot make the same y labels as facet_grid can.
t1 <- ggplot(TPM_matrix, aes(x=Type, y=log10(TPM_value)), group=Type)+
geom_point(aes(color=Type)) +
ggtitle("TPM levels") +
facet_wrap(Cell_Line ~ gene_name, ncol=4, scales="free_y") +
theme(strip.background = element_blank(),
strip.text.y = element_text(size=12, face='bold'),
axis.text.x = element_blank(), legend.position = 'right',
legend.title = element_text(size=13), legend.text = element_text(size=13),
plot.title = element_text(color = "black", hjust = 0.5, vjust = 1, size = 20, face = 'bold'))
I can see each figure has its own cell line and gene name items, but it is duplicated, so I want to make them share the x and y axis like t2.
t2 <- ggplot(TPM_matrix, aes(x=Type, y=log10(TPM_value)), group=Type)+
geom_point(aes(color=Type)) +
ggtitle("TPM levels") +
facet_grid(Cell_Line ~ gene_name, scales="free_y") +
theme(strip.background = element_blank(),
strip.text.y = element_text(size=12, face='bold'),
axis.text.x = element_blank(), legend.position = 'right',
legend.title = element_text(size=13), legend.text = element_text(size=13),
plot.title = element_text(color = "black", hjust = 0.5, vjust = 1, size = 20, face = 'bold'))
But the result of t2 (facet_grid) doesn't offer a free-y axis.
I want to make t1 figure with t2 labels.
What should I do? I need some help.
英文:
I have data on gene expression levels (TPM value), which matrix consists of 2 cell lines and type. (experiment VS control)
![](https://i.stack.imgur.com/z7ePe.png)
I'd like to compare the TPM value between KD and the experiment(KD) for each gene type.Additionally, because the two cell lines share the same gene type, I also want to draw them parallel.To draw them with a free-y-axis, I should use facet_wrap instead of facet_gridbut for facet_wrap, I found that I cannot make the same y labels as facet_grid can.
t1 <- ggplot(TPM_matrix, aes(x=Type, y=log10(TPM_value)),group=Type)+
geom_point(aes(color=Type)) +
ggtitle("TPM levels") +
facet_wrap(Cell_Line ~ gene_name, ncol=4, scales="free_y") +
theme(strip.background = element_blank(),
strip.text.y = element_text(size=12,face='bold'),
axis.text.x=element_blank(), legend.position = 'right',
legend.title=element_text(size=13),legend.text=element_text(size=13),
plot.title = element_text(color = "black",hjust = 0.5, vjust=1, size = 20, face = 'bold'))
I can see each figure has its own cell line and gene name items.. but It is duplicated so I want to make them to share the x and y axis like t2
t2 <- ggplot(TPM_matrix, aes(x=Type, y=log10(TPM_value)),group=Type)+
geom_point(aes(color=Type)) +
ggtitle("TPM levels") +
facet_grid(Cell_Line ~ gene_name, scales="free_y") +
theme(strip.background = element_blank(),
strip.text.y = element_text(size=12,face='bold'),
axis.text.x=element_blank(), legend.position = 'right',
legend.title=element_text(size=13),legend.text=element_text(size=13),
plot.title = element_text(color = "black",hjust = 0.5, vjust=1, size = 20, face = 'bold'))
but the result of t2 (facet_grid) doesn't offer a free-y axis,
I want to make t1 figure with t2 labels..
What should I do? I need some help
答案1
得分: 2
以下是代码部分的翻译:
一种选择是 ggh4x::facet_grid2
,它为 facet_grid
添加了具有“独立”刻度的选项:
library(ggh4x)
#> Loading required package: ggplot2
library(ggplot2)
set.seed(123)
TPM_matrix <- data.frame(
Type = rep(c("KD", "WT"), each = 4),
Cell_Line = rep(c("BT20", "HEK292"), each = 8),
gene_name = c("BRCA2", "CDC42", "THBS1", "TP53"),
TPM_value = 10^(runif(16, 1, 4))
)
ggplot(TPM_matrix, aes(x = Type, y = log10(TPM_value)), group = Type) +
geom_point(aes(color = Type)) +
ggtitle("TPM levels") +
facet_grid2(Cell_Line ~ gene_name, scales = "free_y", independent = "y") +
theme(
strip.background = element_blank(),
strip.text.y = element_text(size = 12, face = "bold"),
axis.text.x = element_blank(), legend.position = "right",
legend.title = element_text(size = 13), legend.text = element_text(size = 13),
plot.title = element_text(color = "black", hjust = 0.5, vjust = 1, size = 20, face = "bold")
)
英文:
One option is ggh4x::facet_grid2
which adds the option to have "independent" scales to facet_grid
:
library(ggh4x)
#> Loading required package: ggplot2
library(ggplot2)
set.seed(123)
TPM_matrix <- data.frame(
Type = rep(c("KD", "WT"), each = 4),
Cell_Line = rep(c("BT20", "HEK292"), each = 8),
gene_name = c("BRCA2", "CDC42", "THBS1", "TP53"),
TPM_value = 10^(runif(16, 1, 4))
)
ggplot(TPM_matrix, aes(x = Type, y = log10(TPM_value)), group = Type) +
geom_point(aes(color = Type)) +
ggtitle("TPM levels") +
facet_grid2(Cell_Line ~ gene_name, scales = "free_y", independent = "y") +
theme(
strip.background = element_blank(),
strip.text.y = element_text(size = 12, face = "bold"),
axis.text.x = element_blank(), legend.position = "right",
legend.title = element_text(size = 13), legend.text = element_text(size = 13),
plot.title = element_text(color = "black", hjust = 0.5, vjust = 1, size = 20, face = "bold")
)
<!-- -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论