1

The result of the plot.

I have this image that is the result of this ggplot command:

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.")

So basically, what I want to happen is for the 2 leftmost columns (the ones that start with Chr. 19) to not have a vertical line, while the 2 rightmost columns (the ones that start with Chr. 23) do have a vertical line.

The Causative SNP Chromosome: Calculation Method of my LOD_table dataset is of type factor. There are four values: Chr. 19: Haplotype-Based, Chr. 19: SNP-Based, Chr. 23: Haplotype-Based, and Chr. 23: SNP-Based, and these values become the column names in my plot as a result of facet_grid(). What I was wondering if I could do would be to have some sort if statement that checks that if the column value starts with "Chr. 23", it will plot the vertical line, but if else, it will plot no vertical line. I guess my problem is, then, that I am unsure how to do this while in a facet_grid().

I am currently dealing with my problem with the caption, but ideally, I would like for the caption to not have to be there. Any help appreciated, thanks.

  • Can you add data using `dput` i.e `dput(LOD_table)`? – Ronak Shah Feb 06 '21 at 07:07
  • 1
    Does this answer your question? [How to produce different geom\_vline in different facets in R?](https://stackoverflow.com/questions/44196384/how-to-produce-different-geom-vline-in-different-facets-in-r) or https://stackoverflow.com/questions/56673981/how-can-i-pass-a-vector-of-values-to-geom-vline-when-working-with-facets or https://stackoverflow.com/questions/10942360/how-to-get-geom-vline-to-honor-facet-wrap – stefan Feb 06 '21 at 08:08

1 Answers1

2

It's easiest if you provide a data frame that specifies the facet variables where you want the line to be plotted, and the xintercept in it too. In the future, please provide the data necessary to reproduce your plot, below i use an example dataset:

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

StupidWolf
  • 45,075
  • 17
  • 40
  • 72