Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions R/tinyplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,15 @@ tinyplot.default = function(
y = rep(NA, length(x))
} else if (type == "density") {
if (is.null(ylab)) ylab = "Density"
} else if (type %in% c("histogram", "function")) {
} else if (type == "function") {
if (is.null(ylab)) ylab = "Frequency"
} else if (type == "histogram") {
if (is.null(ylab)) {
mc = calls[[idx[1]]]
if (is.character(mc$type)) freq = mc$freq else freq = mc$type$freq
freq = ifelse(is.null(freq), TRUE, eval(freq))
ylab = ifelse(freq, "Frequency", "Density")
}
} else {
y = x
x = seq_along(x)
Expand Down Expand Up @@ -1359,7 +1366,12 @@ tinyplot.formula = function(
if (is.null(ylab)) ylab = "Density"
if (is.null(xlab)) xlab = xnam
} else if (!is.null(type) && hist_type) {
if (is.null(ylab)) ylab = "Frequency"
if (is.null(ylab)) {
mc = match.call()
if (is.character(type)) freq = mc$freq else freq = mc$type$freq
freq = ifelse(is.null(freq), TRUE, eval(freq))
ylab = ifelse(freq, "Frequency", "Density")
}
if (is.null(xlab)) xlab = xnam
} else if (is.null(y)) {
if (is.null(ylab)) ylab = xnam
Expand Down
20 changes: 13 additions & 7 deletions R/type_histogram.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@
#' it was larger). If breaks is a function, the x vector is supplied to it as
#' the only argument (and the number of breaks is only limited by the amount of
#' available memory).
#' @param freq Passed to \code{\link[graphics]{hist}}. logical; if TRUE, the
#' histogram graphic is a representation of frequencies, the counts component
#' of the result; if FALSE, probability densities, component density, are plotted
#' (so that the histogram has a total area of one). Defaults to TRUE if and only
#' if breaks are equidistant.
#' @examples
#' # "histogram"/"hist" type convenience string(s)
#' tinyplot(Nile, type = "histogram")
#'
#' # Use `type_histogram()` to pass extra arguments for customization
#' tinyplot(Nile, type = type_histogram(breaks = 30))
#' tinyplot(Nile, type = type_histogram(breaks = 30, freq = FALSE))
#' @export
type_histogram = function(breaks = "Sturges") {
type_histogram = function(breaks = "Sturges", freq = TRUE) {
out = list(
data = data_histogram(breaks = breaks),
data = data_histogram(breaks = breaks, freq = freq),
draw = draw_rect(),
name = "histogram"
)
Expand All @@ -36,12 +41,13 @@ type_histogram = function(breaks = "Sturges") {
type_hist = type_histogram


data_histogram = function(breaks = "Sturges") {
data_histogram = function(breaks = "Sturges", freq = TRUE) {
hbreaks = breaks
fun = function(by, facet, ylab, col, bg, ribbon.alpha, datapoints, .breaks = hbreaks, ...) {
hfreq = freq
fun = function(by, facet, ylab, col, bg, ribbon.alpha, datapoints, .breaks = hbreaks, .freq = hfreq,...) {
hbreaks = ifelse(!is.null(.breaks), .breaks, "Sturges")

if (is.null(ylab)) ylab = "Frequency"
if (is.null(ylab)) ylab = ifelse(.freq, "Frequency", "Density")
if (is.null(by) && is.null(palette)) {
if (is.null(col)) col = par("fg")
if (is.null(bg)) bg = "lightgray"
Expand All @@ -59,7 +65,7 @@ data_histogram = function(breaks = "Sturges") {
by = k$by[1], # already split
facet = k$facet[1], # already split
ymin = 0,
ymax = h$counts,
ymax = if (.freq) h$counts else h$density,
xmin = h$breaks[-1],
xmax = h$mids + (h$mids - h$breaks[-1])
)
Expand Down
1 change: 1 addition & 0 deletions tinyplot.Rproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Version: 1.0
ProjectId: 8cea7a09-6164-4787-92ac-d2570520bcee

RestoreWorkspace: Default
SaveWorkspace: Default
Expand Down
Loading