Skip to content

Instantly share code, notes, and snippets.

@MrFlick
Created March 23, 2022 21:03
Show Gist options
  • Save MrFlick/57ce06b7e3a78c1f8988c8c25872fa0f to your computer and use it in GitHub Desktop.
Save MrFlick/57ce06b7e3a78c1f8988c8c25872fa0f to your computer and use it in GitHub Desktop.
Pheweb variant plot in ggplot2
library(httr)
library(jsonlite)
library(ggplot2)
library(ggrepel)
variant <- "4-74853287-G-A"
getVariantData <- function(variant) {
raw <- GET(paste0("http://pheweb.sph.umich.edu/FinMetSeq/variant/", variant))
html <- content(raw, as="text")
js <- regmatches(html,regexpr("window.variant = (.*)};", html, perl=T))
json_string <- gsub(";$","",gsub("window.variant = ", "", js))
fromJSON(json_string)
}
variant_data <- getVariantData(variant)
pheno_data <- variant_data$phenos
adjustOrder <- function(phenocodes, categories) {
codes_by_cat <- split(phenocodes, categories)
list(
reordered = factor(phenocodes, levels=unlist(codes_by_cat)),
firstcode = lapply(codes_by_cat, function(x) x[1])
)
}
adj <- with(pheno_data, adjustOrder(phenocode, category))
pheno_data$ordcat <- adj$reordered
to_label <- subset(pheno_data, -log10(pval)>5)
ggplot(pheno_data, aes(ordcat, -log10(pval))) +
geom_point(aes(color=category)) +
geom_text_repel(aes(label = phenostring), data=to_label) +
scale_x_discrete(breaks=unlist(adj$firstcode),labels=names(adj$firstcode)) +
theme(axis.text.x = element_text(angle = -45, hjust=0)) +
guides(color=FALSE) + xlab("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment