微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

R ggplot 基于条件的垂直线,但在 facet_grid 命令之后?

如何解决R ggplot 基于条件的垂直线,但在 facet_grid 命令之后?

The result of the plot.

我有这个 ggplot 命令的结果图像:

causative_snp = filter(LOD_table,str_detect(`Causative SNP Chromosome: Calculation Method`,"Chr. 23")) %>%
    pull(`Causative SNP`) %>%
    extract(1)

ggplot(data = LOD_table,mapping = aes(x = `Locus (Mb)`,y = `LOD score`)) +
    facet_grid(`Test Type` ~ `Causative SNP Chromosome: Calculation Method`) +
    geom_vline(xintercept = causative_snp,color = "blue",size = .5,alpha = .5,linetype = "dashed") +
    geom_point(alpha = .01,size = .1,na.rm = TRUE) +
    labs(title = "LOD score Tests",subtitle = paste("Causative SNP (on Chromosome 23): ",causative_snp," Mb",sep = ""),caption = "The causative SNP line is plotted on non-causative SNP chromosomes to show the lack of correlation.")

所以基本上,我想要发生的是最左边的两列(以Chr. 19开头的列)有一条垂直线,而最右边的两列(以 Chr. 23) 开头的那些do 有一条垂直线。

我的 LOD_table 数据集的 Causative SNP Chromosome: Calculation Methodfactor 类型。有四个值:Chr. 19: Haplotype-BasedChr. 19: SNP-BasedChr. 23: Haplotype-BasedChr. 23: SNP-Based,由于 facet_grid(),这些值成为我图中的列名。我想知道我是否可以做某种 if 语句来检查列值是否以“Chr. 23”开头,它将绘制垂直线,但如果否则,它将绘制不垂线。我想我的问题是,我不确定如何在 facet_grid() 中执行此操作。

我目前正在处理我的标题问题,但理想情况下,我希望标题不必在那里。感谢任何帮助,谢谢。

解决方法

最简单的方法是提供一个数据框,指定要在其中绘制线条的方面变量以及其中的 xintercept。将来,请提供重现您的情节所需的数据,下面我使用示例数据集:

set.seed(111)

LOD_table = data.frame('Locus (Mb)' = runif(360,1,40),`LOD Score` = rnbinom(360,mu=2,size=0.1),`Test Type` = rep(1:3,each = 120),`Causative SNP Chromosome: Calculation Method` = rep(c("chr19 A","chr19 B","chr23 A","chr23 B")),check.names=FALSE)

da  = expand.grid(`Test Type` = 1:3,`Causative SNP Chromosome: Calculation Method` = c("chr23 A","chr23 B"),causative_snp = 20)

ggplot(data = LOD_table,mapping = aes(x = `Locus (Mb)`,y = `LOD Score`)) +
geom_point() +
facet_grid(`Test Type` ~ `Causative SNP Chromosome: Calculation Method`) +
geom_vline(data = da,aes(xintercept = causative_snp),color = "blue",size = .5,alpha = .5,linetype = "dashed") 

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。