英文:
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("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)
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="black") +
scale_x_continuous(sec.axis = sec_axis(~ . * 1, breaks = c("SRR9292593", "SRR9292594"), labels = c("0.1","0.5")))
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=
以按需要拆分它。
请注意,这需要在 Samples
和 expression_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")
[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 "adapt" 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't quite make sense in the perspective of your plotting request.
I'll demo with dplyr, but this can easily be adapted to base R if needed.
```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(
# 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 = "expression_intron",
# 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 = "Samples")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论