## Dotplot histogram ## Mark Gardener 2013 ## www.dataanalytics.org.uk hg_dot <- function(x, breaks = "sturges", offset = 0.4, cex = 3, pch = 19, ...) { # x = data vector # ... = other instructions for plot hg <- hist(x, breaks = breaks, plot = FALSE) # Make histogram data but do not plot bins <- length(hg\$counts) # How many bin categories are needed? yvals <- numeric(0) # A blank variable to fill in for(i in 1:bins) { # Start a loop yvals <- c(yvals, hg\$counts[i]:0) # Work out the y-values } # End the loop xvals <- numeric(0) # A blank variable for(i in 1:bins) { # Start a loop xvals <- c(xvals, rep(hg\$mids[i], hg\$counts[i]+1)) # Work out x-values } # End the loop dat <- data.frame(xvals, yvals) # Make data frame of x, y variables dat <- dat[yvals > 0, ] # Knock out any zero y-values minx <- min(hg\$breaks) # Min value for x-axis maxx <- max(hg\$breaks) # Max value x-axis miny <- min(dat\$yvals) # Min value for y-axis maxy <- max(dat\$yvals) # Max value for y-axis # Make the plot, without axes, allow ponts to overspill plot region plot(yvals + offset ~ xvals, data = dat, xlim = c(minx, maxx), ylim = c(miny, maxy), axes = FALSE, ylab = "", xpd = NA, cex = cex, pch = pch, ...) axis(1) # Add in the x-axis # Make results of original data, histogram and plot data result <- list(hist = hg, original = x, plot.data = dat) invisible(result) # Save the x, y data invisibly } # end ## END