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

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

ggplot2 - boxplot with two x-axes

问题

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

这是我的问题:

使用这个简单的示例数据框:

    Samples = c("SRR9292593", "SRR9292593", "SRR9292593", "SRR9292594", "SRR9292594", "SRR9292594")
    expression_gene = c(3.5, 0.4, 8.2, 4.5, 3.2, 3.1)
    expression_intron = c(0.1, 0.1, 0.1, 0.5, 0.5, 0.5)
    p <- data.frame(Samples, expression_gene, expression_intron)

我想制作一个带有两个x轴的箱线图,一个指示"Samples"中的值,另一个带有"expression_intron"中相应的值。

我尝试了这个:

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

但它给我这个错误:

    Error: Discrete value supplied to continuous scale

我该如何修复这个问题?

提前感谢您的回答。
英文:

here's my problem:

with this simple example dataframe:

Samples =  c(&quot;SRR9292593&quot;, &quot;SRR9292593&quot;, &quot;SRR9292593&quot;, &quot;SRR9292594&quot;, &quot;SRR9292594&quot;, &quot;SRR9292594&quot;)
expression_gene = c(3.5, 0.4, 8.2, 4.5, 3.2, 3.1)
expression_intron = c(0.1, 0.1, 0.1, 0.5, 0.5, 0.5)
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:

ggplot(p, aes(x=reorder(Samples, expression_intron), y=expression_gene)) + 
       geom_boxplot(color=&quot;black&quot;) +
       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:

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。

library(dplyr)
p2 <- p %>%
  mutate(
    Samples = reorder(Samples, expression_intron),
    expression_intron = factor(expression_intron)
  )

ggplot(p2, aes(x = as.numeric(Samples), y = expression_gene, group = Samples)) +
  geom_boxplot(color="black") +
  scale_x_continuous(
    # 分配断点/标签以将我们的数字转换为离散标签
    breaks = seq_along(levels(p2$Samples)),
    labels = levels(p2$Samples),
    sec.axis = sec_axis(
      ~ .,
      name = "expression_intron",
      # 这也可以是 seq_along(levels(p2$expr...)),效果一样
      breaks = seq_along(levels(p2$Samples)),
      labels = levels(p2$expression_intron)
    )
  ) +
  labs(x = "Samples")

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


[1]: https://i.stack.imgur.com/PFuK7.png

<details>
<summary>英文:</summary>

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.

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.

I&#39;ll demo with dplyr, but this can easily be adapted to base R if needed.


```r
library(dplyr)
p2 &lt;- p %&gt;%
  mutate(
    Samples = reorder(Samples, expression_intron),
    expression_intron = factor(expression_intron)
  )

ggplot(p2, aes(x = as.numeric(Samples), y = expression_gene, group = Samples)) +
  geom_boxplot(color=&quot;black&quot;) +
  scale_x_continuous(
    # assign breaks/labels to convert our numerics to discrete labels
    breaks = seq_along(levels(p2$Samples)),
    labels = levels(p2$Samples),
    sec.axis = sec_axis(
      ~ .,
      name = &quot;expression_intron&quot;,
      # this could be seq_along(levels(p2$expr...)) as well, same thing
      breaks = seq_along(levels(p2$Samples)),
      labels = levels(p2$expression_intron)
    )
  ) +
  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:

确定