From 9fbdd72e85c55e9b37bd7e7900f6bdf931aaac3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lie=20Siberchicot?= Date: Wed, 14 Feb 2024 10:48:31 +0100 Subject: [PATCH] Update the 'plotdist' function which now supports a vector for the 'breaks' argument --- R/plotdist.R | 10 +++++----- tests/t-plotdist.R | 4 +++- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/R/plotdist.R b/R/plotdist.R index f4b89b4a..e3858783 100644 --- a/R/plotdist.R +++ b/R/plotdist.R @@ -51,7 +51,7 @@ plotdist <- function(data, distr, para, histo = TRUE, breaks="default", demp = F { if(demp) { - if (breaks=="default") + if (identical(breaks, "default")) h <- hist(data, freq=FALSE, xlab="Data", main="Empirical density", ...) else h <- hist(data, freq=FALSE, xlab="Data", main="Empirical density", @@ -60,7 +60,7 @@ plotdist <- function(data, distr, para, histo = TRUE, breaks="default", demp = F } else { - if (breaks=="default") + if (identical(breaks, "default")) h <- hist(data, freq=FALSE, xlab="Data", main="Histogram", ...) else h <- hist(data, freq=FALSE, xlab="Data", main="Histogram", @@ -81,7 +81,7 @@ plotdist <- function(data, distr, para, histo = TRUE, breaks="default", demp = F else { # plot for discrete data alone - if (breaks!="default") + if (!identical(breaks, "default")) warning("Breaks are not taken into account for discrete data") # plot of empirical distribution t <- table(data) @@ -140,7 +140,7 @@ plotdist <- function(data, distr, para, histo = TRUE, breaks="default", demp = F # plot of empirical and theoretical density # computes densities in order to define limits for y-axis - if (breaks=="default") + if (identical(breaks, "default")) h <- hist(data, plot=FALSE) else h <- hist(data, breaks=breaks, plot=FALSE, ...) @@ -207,7 +207,7 @@ plotdist <- function(data, distr, para, histo = TRUE, breaks="default", demp = F { # plot of discrete data with theoretical distribution par(mfrow=c(1, 2)) - if (breaks!="default") + if (!identical(breaks, "default")) warning("Breaks are not taken into account for discrete distributions") # plot of empirical and theoretical distributions t <- table(data) diff --git a/tests/t-plotdist.R b/tests/t-plotdist.R index 2b318bf6..59858f27 100644 --- a/tests/t-plotdist.R +++ b/tests/t-plotdist.R @@ -11,7 +11,9 @@ plotdist(x1) plotdist(x1, col="blue", type="b", pch=16) plotdist(x1, type="s") plotdist(x1, demp = TRUE) -plotdist(x1,demp = TRUE, adjust = 1.5) +plotdist(x1, demp = TRUE, adjust = 1.5) +plotdist(x1, breaks = 2) +plotdist(x1, breaks = seq(-3, 2)) # (2) Plot of a discrete distribution against data #