QQplot in R using ggplot

library(qqplotr)
library(tidyverse)
library(qqman)
N=100000
df <- data.frame(testnum=1:N,
                 p=runif(N),
                 group=sample(paste0("G",1:3),N,replace=TRUE))
df$p[which(df$group=="G1")[1:100]]=runif(100)/10000
hist(df$p,breaks=100)

qq(df$p)

##make qq plot with ggplot manually

ci=0.95

res <- df %>% filter(!is.na(p)) %>%
    arrange(p) %>%
    group_by(group) %>%
    mutate(r=rank(p, ties.method = "random"),
           pexp=r/length(p),
           clower   = -log10(qbeta(p = (1 - ci) / 2, shape1 = r, shape2 = length(p)-r)),
           cupper   = -log10(qbeta(p = (1 + ci) / 2, shape1 = r, shape2 = length(p)-r)))

           
p1 <- res %>%
    ggplot(aes(x=-log10(pexp),y=-log10(p),color=group)) +
    geom_ribbon(mapping = aes(x = -log10(pexp), ymin = clower, ymax = cupper),
      alpha = 0.1,color="darkgray") +
    geom_point() +
    geom_abline(slope=1,intercept=0) +
##    facet_grid(Origin ~ Location) +
    xlab(expression(Expected -log[10](p))) +
    ylab(expression(Observed -log[10](p))) + 
    theme_bw()

p1

##make qq plot with qqplotr

di <- "unif"

pq <- ggplot(data = df, mapping = aes(sample = p, color = group)) +
      stat_qq_band(distribution = di) +
        stat_qq_line(distribution = di) +
        stat_qq_point(distribution = di) +
      ggtitle("Test") +
      theme_bw()+
      geom_abline(intercept=0, slope=1) 
##      coord_trans(x ="log10", y="log10")  

pq