ggplot2 – 带有两个 x 轴的箱线图

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

ggplot2 - boxplot with two x-axes

问题

以下是您要翻译的代码部分:

  1. 这是我的问题:
  2. 使用这个简单的示例数据框:
  3. Samples = c("SRR9292593", "SRR9292593", "SRR9292593", "SRR9292594", "SRR9292594", "SRR9292594")
  4. expression_gene = c(3.5, 0.4, 8.2, 4.5, 3.2, 3.1)
  5. expression_intron = c(0.1, 0.1, 0.1, 0.5, 0.5, 0.5)
  6. p <- data.frame(Samples, expression_gene, expression_intron)
  7. 我想制作一个带有两个x轴的箱线图,一个指示"Samples"中的值,另一个带有"expression_intron"中相应的值。
  8. 我尝试了这个:
  9. ggplot(p, aes(x=reorder(Samples, expression_intron), y=expression_gene)) +
  10. geom_boxplot(color="black") +
  11. scale_x_continuous(sec.axis = sec_axis(~ . * 1, breaks = c("SRR9292593", "SRR9292594"), labels = c("0.1","0.5")))
  12. 但它给我这个错误:
  13. Error: Discrete value supplied to continuous scale
  14. 我该如何修复这个问题?
  15. 提前感谢您的回答。
英文:

here's my problem:

with this simple example dataframe:

  1. Samples = c(&quot;SRR9292593&quot;, &quot;SRR9292593&quot;, &quot;SRR9292593&quot;, &quot;SRR9292594&quot;, &quot;SRR9292594&quot;, &quot;SRR9292594&quot;)
  2. expression_gene = c(3.5, 0.4, 8.2, 4.5, 3.2, 3.1)
  3. expression_intron = c(0.1, 0.1, 0.1, 0.5, 0.5, 0.5)
  4. p &lt;- data.frame(Samples, expression_gene, expression_intron)

I would like to make a boxplot with 2 x-axes, one indicating the values in "Samples", the other with the respective values in "expression_intron".

I tried this:

  1. ggplot(p, aes(x=reorder(Samples, expression_intron), y=expression_gene)) +
  2. geom_boxplot(color=&quot;black&quot;) +
  3. scale_x_continuous(sec.axis = sec_axis(~ . * 1, breaks = c(&quot;SRR9292593&quot;, &quot;SRR9292594&quot;), labels = c(&quot;0.1&quot;,&quot;0.5&quot;)))

But it gives me this error:

  1. Error: Discrete value supplied to continuous scale

How could i fix this?

Thanks in advance for the answers.

答案1

得分: 2

我认为我们可以将 https://stackoverflow.com/q/45361904/3358272 改进,以强制 x 轴连续;我说"改进",因为当我们将 Samples 转换为 factor 并使用 as.numeric(Samples) 时,它会生成一个单一的箱线图;我们需要添加 group= 以按需要拆分它。

请注意,这需要在 Samplesexpression_intron 之间保持完美的一对一关系;在绘图请求的角度看,其他情况都不太合理。

我将使用dplyr进行演示,但如果需要,这可以很容易地适应为基本的R。

  1. library(dplyr)
  2. p2 <- p %>%
  3. mutate(
  4. Samples = reorder(Samples, expression_intron),
  5. expression_intron = factor(expression_intron)
  6. )
  7. ggplot(p2, aes(x = as.numeric(Samples), y = expression_gene, group = Samples)) +
  8. geom_boxplot(color="black") +
  9. scale_x_continuous(
  10. # 分配断点/标签以将我们的数字转换为离散标签
  11. breaks = seq_along(levels(p2$Samples)),
  12. labels = levels(p2$Samples),
  13. sec.axis = sec_axis(
  14. ~ .,
  15. name = "expression_intron",
  16. # 这也可以是 seq_along(levels(p2$expr...)),效果一样
  17. breaks = seq_along(levels(p2$Samples)),
  18. labels = levels(p2$expression_intron)
  19. )
  20. ) +
  21. labs(x = "Samples")

ggplot2 – 带有两个 x 轴的箱线图

  1. [1]: https://i.stack.imgur.com/PFuK7.png
  2. <details>
  3. <summary>英文:</summary>
  4. I think we can adapt https://stackoverflow.com/q/45361904/3358272 to force the x-axis to be continuous; I say &quot;adapt&quot; because when we convert `Samples` to a `factor` and use `as.numeric(Samples)`, it produces a single boxplot; we need to add `group=` to get it to split as needed.
  5. Note that this requires a perfect 1-to-1 between `Samples` and `expression_intron`; anything else doesn&#39;t quite make sense in the perspective of your plotting request.
  6. I&#39;ll demo with dplyr, but this can easily be adapted to base R if needed.
  7. ```r
  8. library(dplyr)
  9. p2 &lt;- p %&gt;%
  10. mutate(
  11. Samples = reorder(Samples, expression_intron),
  12. expression_intron = factor(expression_intron)
  13. )
  14. ggplot(p2, aes(x = as.numeric(Samples), y = expression_gene, group = Samples)) +
  15. geom_boxplot(color=&quot;black&quot;) +
  16. scale_x_continuous(
  17. # assign breaks/labels to convert our numerics to discrete labels
  18. breaks = seq_along(levels(p2$Samples)),
  19. labels = levels(p2$Samples),
  20. sec.axis = sec_axis(
  21. ~ .,
  22. name = &quot;expression_intron&quot;,
  23. # this could be seq_along(levels(p2$expr...)) as well, same thing
  24. breaks = seq_along(levels(p2$Samples)),
  25. labels = levels(p2$expression_intron)
  26. )
  27. ) +
  28. labs(x = &quot;Samples&quot;)

ggplot2 – 带有两个 x 轴的箱线图

huangapple
  • 本文由 发表于 2023年3月15日 20:27:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744690.html
匿名

发表评论

匿名网友

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

确定