如何绘制带有标签的facet_wrap,类似于facet_grid(独立的y轴)。

huangapple go评论68阅读模式
英文:

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)

My toy matrix

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

the result of t1

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

the result of t2

But the result of t2 (facet_grid) doesn't offer a free-y axis.

I want to make t1 figure with t2 labels.

What I want to draw

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)

My toy matrix

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 &lt;- ggplot(TPM_matrix, aes(x=Type, y=log10(TPM_value)),group=Type)+
  geom_point(aes(color=Type)) + 
  ggtitle(&quot;TPM levels&quot;) + 
  facet_wrap(Cell_Line ~ gene_name, ncol=4, scales=&quot;free_y&quot;) +
  theme(strip.background = element_blank(), 
        strip.text.y = element_text(size=12,face=&#39;bold&#39;),
        axis.text.x=element_blank(), legend.position = &#39;right&#39;,
        legend.title=element_text(size=13),legend.text=element_text(size=13),
        plot.title = element_text(color = &quot;black&quot;,hjust = 0.5, vjust=1, size = 20, face = &#39;bold&#39;))

the result of t1

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 &lt;- ggplot(TPM_matrix, aes(x=Type, y=log10(TPM_value)),group=Type)+
  geom_point(aes(color=Type)) + 
  ggtitle(&quot;TPM levels&quot;) + 
  facet_grid(Cell_Line ~ gene_name, scales=&quot;free_y&quot;) +
  theme(strip.background = element_blank(), 
        strip.text.y = element_text(size=12,face=&#39;bold&#39;),
        axis.text.x=element_blank(), legend.position = &#39;right&#39;,
        legend.title=element_text(size=13),legend.text=element_text(size=13),
        plot.title = element_text(color = &quot;black&quot;,hjust = 0.5, vjust=1, size = 20, face = &#39;bold&#39;))

the result of t2

but the result of t2 (facet_grid) doesn't offer a free-y axis,

I want to make t1 figure with t2 labels..

What I want to draw

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

如何绘制带有标签的facet_wrap,类似于facet_grid(独立的y轴)。

英文:

One option is ggh4x::facet_grid2 which adds the option to have "independent" scales to facet_grid:

library(ggh4x)
#&gt; Loading required package: ggplot2
library(ggplot2)

set.seed(123)

TPM_matrix &lt;- data.frame(
  Type = rep(c(&quot;KD&quot;, &quot;WT&quot;), each = 4),
  Cell_Line = rep(c(&quot;BT20&quot;, &quot;HEK292&quot;), each = 8),
  gene_name = c(&quot;BRCA2&quot;, &quot;CDC42&quot;, &quot;THBS1&quot;, &quot;TP53&quot;),
  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(&quot;TPM levels&quot;) +
  facet_grid2(Cell_Line ~ gene_name, scales = &quot;free_y&quot;, independent = &quot;y&quot;) +
  theme(
    strip.background = element_blank(),
    strip.text.y = element_text(size = 12, face = &quot;bold&quot;),
    axis.text.x = element_blank(), legend.position = &quot;right&quot;,
    legend.title = element_text(size = 13), legend.text = element_text(size = 13),
    plot.title = element_text(color = &quot;black&quot;, hjust = 0.5, vjust = 1, size = 20, face = &quot;bold&quot;)
  )

如何绘制带有标签的facet_wrap,类似于facet_grid(独立的y轴)。<!-- -->

huangapple
  • 本文由 发表于 2023年4月17日 00:08:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76028900.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定