Skip to content

Instantly share code, notes, and snippets.

@Zepeng-Mu
Created September 27, 2022 16:23
Show Gist options
  • Save Zepeng-Mu/4d113cf4cf4e1f11df15f0854fe2e395 to your computer and use it in GitHub Desktop.
Save Zepeng-Mu/4d113cf4cf4e1f11df15f0854fe2e395 to your computer and use it in GitHub Desktop.
Plot QQ-plot using ggplot2
ggQQplot <- function(pList, colVec = c("black", "orange", "red", "purple"),
legendLabel = NULL, title = "", openRange = F, sampling = 1) {
if (is.null(legendLabel)) {
legendLabel = colVec
}
if (length(pList) > length(colVec)) {
stop("pList needs to be shorter than colVec!!!")
}
if (openRange) {
pList <- lapply(pList, function(x) x[x > 0 | x < 1])
}
if (sampling < 1) {
pList <- lapply(pList, function(x) sample(x, length(x) * sampling))
}
g <- ggplot() +
theme_classic(base_size = 7) +
labs(title = title,
x = "Empirical -log<sub>10</sub> (P)",
y = "Observed -log<sub>10</sub> (P)",) +
theme(axis.title.x = ggtext::element_markdown(size = unit(7, "pt")),
axis.title.y = ggtext::element_markdown(size = unit(7, "pt")),
axis.text = element_text(size = unit(6, "pt")),
legend.title = element_blank(),
legend.text = element_text(size = unit(7, "pt")),
plot.title = element_text(size = unit(8, "pt")),
aspect.ratio = 1)
for (i in 1:length(pList)) {
xValue <- -log10(ppoints(length(pList[[i]])))
yValue <- sort(-log10(pList[[i]]), decreasing = T)
g <- g + ggrastr::geom_point_rast(mapping = aes_(x = xValue, y = yValue, col = legendLabel[i]),
size = 0.1)
}
g <- g +
geom_abline(slope = 1, intercept = 0, col = "red", lty = 2) +
scale_color_manual(limits = legendLabel, values = colVec)
return(g)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment