Almost certainly a head-smakingly simple solution …
I want to use a set of histograms as the diagonal in a pairs()
chart in which different rating-scale items are correlated. (Typically these are integer variables ranging from ‘1’ to ‘5’ but sometimes ‘1’ to ‘7’, ‘-3’ to ‘3’, etc., or as a summated scale ranging from ‘5’ to ’25’.)
So I want the histogram to show a bar for each level in the scale: 5 bars for a 1-5 scale, 7 bars for a 1-7 scale, etc.
But the hist()
function instead gives me 8 bars for a 1-5 scale, 6 bars for a 1-7 scale, 8 bars for a 1-9 scale, etc.
n <- 128
set.seed(42)
x1 <- sample(c(1:5), size = n, replace = TRUE)
x2 <- sample(c(1:7), size = n, replace = TRUE)
x3 <- sample(c(1:9), size = n, replace = TRUE)
hist(x1, breaks = "Sturges", right = TRUE)
hist(x2, breaks = "Sturges", right = TRUE)
hist(x3, breaks = "Sturges", right = TRUE)
Created on 2024-08-03 with reprex v2.1.1
If I use the breaks = "FD"
or breaks = "Scott"
options then I always get 1 fewer bars than number of values.
Changing the right = TRUE
and right = FALSE
options simply changes which bar is left out.
I expect that the issue has something to do the the “pretty” process within hist()
, but I can’t work out how to work around it.
Any thoughts?