diff --git a/NAMESPACE b/NAMESPACE index 5fe68df..1df63b4 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -18,6 +18,7 @@ importFrom(ggplot2,aes) importFrom(ggplot2,aes_string) importFrom(ggplot2,after_stat) importFrom(ggplot2,annotate) +importFrom(ggplot2,coord_cartesian) importFrom(ggplot2,element_blank) importFrom(ggplot2,element_text) importFrom(ggplot2,geom_abline) @@ -31,6 +32,8 @@ importFrom(ggplot2,labs) importFrom(ggplot2,scale_color_manual) importFrom(ggplot2,scale_colour_manual) importFrom(ggplot2,scale_fill_manual) +importFrom(ggplot2,scale_x_continuous) +importFrom(ggplot2,scale_y_continuous) importFrom(ggplot2,theme) importFrom(glue,glue) importFrom(graphics,hist) diff --git a/R/qqplot.R b/R/qqplot.R index 6c5664a..2f1aad1 100644 --- a/R/qqplot.R +++ b/R/qqplot.R @@ -40,6 +40,7 @@ qqplot <- function(x, ...) { #' @param color_line Color of the reference line (detault is `"#BB9FC9"`). #' @param size_points Size of the points (default is `1`). #' @param size_line Thickness of the reference line (default is `1`). +#' @param ... Additional arguments. #' @return An object of classes gg and ggplot with the QQ-Plot between the #' observed quantiles generated by the return of the function `accept_reject()` #' and the theoretical quantiles of the true distribution. @@ -65,17 +66,22 @@ qqplot <- function(x, ...) { #' qqplot(y) #' @importFrom Rcpp evalCpp #' @importFrom ggplot2 ggplot geom_point geom_abline labs theme element_text +#' coord_cartesian scale_x_continuous scale_y_continuous #' aes_string #' @export -qqplot.accept_reject <- function(x, alpha = 0.5, color_points = "#FE4F0E", color_line = "#BB9FC9", size_points = 1, size_line = 1) { +qqplot.accept_reject <- function(x, alpha = 0.5, color_points = "#FE4F0E", color_line = "#BB9FC9", size_points = 1, size_line = 1, ...) { sample_quantiles <- sort(x) p <- (rank(sample_quantiles) - 0.375) / (length(sample_quantiles) + 0.25) theoretical_quantiles <- sapply(p, function(p) quantile_custom(x = x, p = p)) df <- data.frame(Theoretical = theoretical_quantiles, Sample = sample_quantiles) - ggplot(df, aes_string(x = "Theoretical", y = "Sample")) + + xlim <- attr(x, "xlim") + continuous <- attr(x, "continuous") + + plot <- ggplot(df, aes_string(x = "Theoretical", y = "Sample")) + geom_abline(slope = 1, intercept = 0, color = color_line, size = size_line) + geom_point(alpha = alpha, color = color_points, size = size_points) + + coord_cartesian(xlim = xlim, ylim = xlim) + labs(x = "Theoretical Quantiles", y = "Sample Quantiles", title = "QQ-Plot") + theme( axis.title = ggplot2::element_text(face = "bold"), @@ -83,4 +89,11 @@ qqplot.accept_reject <- function(x, alpha = 0.5, color_points = "#FE4F0E", color legend.title = ggplot2::element_text(face = "bold"), plot.subtitle = ggplot2::element_text(face = "plain") ) + + if (!continuous) { + plot <- plot + + scale_x_continuous(breaks = xlim[1L]:xlim[2L]) + + scale_y_continuous(breaks = xlim[1L]:xlim[2L]) + } + return(plot) } diff --git a/README.md b/README.md index 67b4be5..4d154d3 100644 --- a/README.md +++ b/README.md @@ -325,7 +325,7 @@ case_1 <- accept_reject( xlim = c(0, 10) ) toc() -#> 0.005 sec elapsed +#> 0.01 sec elapsed # Specifying the base probability density function tic() @@ -341,7 +341,7 @@ case_2 <- accept_reject( c = 1.2 ) toc() -#> 0.004 sec elapsed +#> 0.006 sec elapsed # Visualizing the results p1 <- plot(case_1) diff --git a/man/figures/README-unnamed-chunk-2-1.png b/man/figures/README-unnamed-chunk-2-1.png index 4874b6b..06c96d4 100644 Binary files a/man/figures/README-unnamed-chunk-2-1.png and b/man/figures/README-unnamed-chunk-2-1.png differ diff --git a/man/figures/README-unnamed-chunk-2-2.png b/man/figures/README-unnamed-chunk-2-2.png index 42847b4..88c03c7 100644 Binary files a/man/figures/README-unnamed-chunk-2-2.png and b/man/figures/README-unnamed-chunk-2-2.png differ diff --git a/man/figures/README-unnamed-chunk-3-1.png b/man/figures/README-unnamed-chunk-3-1.png index 56aad4f..a99e174 100644 Binary files a/man/figures/README-unnamed-chunk-3-1.png and b/man/figures/README-unnamed-chunk-3-1.png differ diff --git a/man/figures/README-unnamed-chunk-3-2.png b/man/figures/README-unnamed-chunk-3-2.png index f83ec38..c3cac30 100644 Binary files a/man/figures/README-unnamed-chunk-3-2.png and b/man/figures/README-unnamed-chunk-3-2.png differ diff --git a/man/qqplot.accept_reject.Rd b/man/qqplot.accept_reject.Rd index 40d3e23..6e54fcc 100644 --- a/man/qqplot.accept_reject.Rd +++ b/man/qqplot.accept_reject.Rd @@ -11,7 +11,8 @@ Plot the QQ-Plot between observed quantiles and theoretical quantiles.} color_points = "#FE4F0E", color_line = "#BB9FC9", size_points = 1, - size_line = 1 + size_line = 1, + ... ) } \arguments{ @@ -26,6 +27,8 @@ Plot the QQ-Plot between observed quantiles and theoretical quantiles.} \item{size_points}{Size of the points (default is \code{1}).} \item{size_line}{Thickness of the reference line (default is \code{1}).} + +\item{...}{Additional arguments.} } \value{ An object of classes gg and ggplot with the QQ-Plot between the