library(tidyverse)
data <- data.frame(x = rgamma(1000, shape = 2, rate = 1),
                   y = rnorm(1000, 10, 30),
                   gr = ifelse(rpois(1000, 2) < 2, "A", "B"))

# data$y[22] <- 500

stat_normal

ggplot(data, aes(y, color = gr, fill = gr)) + 
    geom_histogram(aes(y = ..density..),
                   color = "grey20",
                   alpha = 0.7,
                   # position = position_dodge(12),
                   bins = 11,
                   size = 1) +
    # stat_function(fun = dnorm, args = list(mean = mean(data$y), sd = sd(data$y))) +
    stat_normal_density(aes(y = ..density..), color = "black", size = 1.5, trim = 0) +
facet_grid(c(".~gr"))
ggplot(data, aes(sample = y, color = gr, fill = gr)) + 
    stat_qq_line(color = "black") +
    stat_qq() +
    facet_wrap(~gr)
qq_plot(y ~ gr, data)


GegznaV/BioStat documentation built on Aug. 14, 2020, 9:30 p.m.