问题描述
我有这个 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 Method
是 factor
类型。有四个值:Chr. 19: Haplotype-Based
、Chr. 19: SNP-Based
、Chr. 23: Haplotype-Based
和 Chr. 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")