Plotting hexbin objects with small data fails ungracefully
I recognize that hexbin is intended for large amounts of data, but it might be nice if it failed a little more gracefully in cases where the hexbin() object is created with smaller data:
library(hexbin)
set.seed(1)
plot(hexbin(rnorm(1000), rnorm(1000)))
# Succeeds
set.seed(1)
plot(hexbin(rnorm(100), rnorm(100)))
# Error in M[, 1] : incorrect number of dimensions
set.seed(1)
plot(hexbin(rnorm(10), rnorm(10)))
# Error in cut.default(rcnt, colorcut, labels = FALSE) :
# invalid number of intervals
# Sometimes you can find random configurations where it works...
# I couldn't find any that worked with n = 10
set.seed(42)
plot(hexbin(rnorm(100), rnorm(100)))
# Succeeds
The M[,1] error appears to be an instance of the drop argument in a degenerate case in LinGray going back to length(colorcut) only being 2. I didn't debug the n = 10 case as thoroughly.
My sessionInfo():
R version 3.4.3 (2017-11-30) Platform: x86_64-apple-darwin15.6.0 (64-bit) Running under: macOS Sierra 10.12.5
Matrix products: default BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib
locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages: [1] stats graphics grDevices utils datasets methods base
other attached packages: [1] hexbin_1.27.2
loaded via a namespace (and not attached): [1] compiler_3.4.3 tools_3.4.3 yaml_2.1.14 grid_3.4.3 lattice_0.20-35
I also encountered this problem. Sometimes you want hexbin for a variety of datasets, and some may be small. Then it fails in a very confusing way. I agree with joranE on the source of the problem. The solution (I think) is to use "drop = FALSE" here: M <- rbind(c(0, 0, 0), c(0, 0, 0), c(1, 1, 1), c(1, 1, 1), c(2, 2, 2), c(3, 3, 3), c(4, 4, 4), c(5, 5, 5), c(6, 6, 6), c(7, 7, 7), c(8, 8, 8), c(9, 9, 9), c(10, 10, 10), c(11, 11, 11), c(12, 12, 12), c(13, 13, 13), c(14, 14, 14), c(15, 15, 15), c(16, 16, 16), c(17, 17, 17), c(18, 18, 18), c(19, 19, 19), c(20, 20, 20), c(21, 21, 21), c(22, 22, 22), c(23, 23, 23), c(24, 24, 24), c(25, 25, 25), c(26, 26, 26), c(27, 27, 27), c(28, 28, 28), c(29, 29, 29), c(30, 30, 30), c(32, 32, 32), c(34, 34, 34), c(35, 35, 35), c(37, 37, 37), c(39, 39, 39), c(41, 41, 41), c(43, 43, 43), c(45, 45, 45), c(46, 46, 46), c(47, 47, 47), c(49, 49, 49), c(51, 51, 51), c(52, 52, 52), c(54, 54, 54), c(56, 56, 56), c(59, 59, 59), c(61, 61, 61), c(64, 64, 64), c(67, 67, 67), c(69, 69, 69), c(72, 72, 72), c(75, 75, 75), c(76, 76, 76), c(78, 78, 78), c(81, 81, 81), c(84, 84, 84), c(87, 87, 87), c(91, 91, 91), c(94, 94, 94), c(97, 97, 97), c(101, 101, 101), c(104, 104, 104), c(107, 107, 107), c(108, 108, 108), c(112, 112, 112), c(116, 116, 116), c(120, 120, 120), c(124, 124, 124), c(128, 128, 128), c(132, 132, 132), c(136, 136, 136), c(141, 141, 141), c(145, 145, 145), c(147, 147, 147), c(150, 150, 150), c(154, 154, 154), c(159, 159, 159), c(164, 164, 164), c(169, 169, 169), c(174, 174, 174), c(179, 179, 179), c(185, 185, 185), c(190, 190, 190), c(195, 195, 195), c(201, 201, 201), c(207, 207, 207), c(212, 212, 212), c(216, 216, 216), c(218, 218, 218), c(224, 224, 224), c(226, 226, 226), c(230, 230, 230), c(237, 237, 237), c(243, 243, 243), c(245, 245, 245), c(252, 252, 252), c(255, 255, 255), c(255, 255, 255))[round(seq(beg, end, length = n)), , drop = FALSE]