Skip to content

Instantly share code, notes, and snippets.

@apinstein
Created December 18, 2016 20:06
Show Gist options
  • Save apinstein/e0997661a485c856c7e16b4055e2e0cf to your computer and use it in GitHub Desktop.
Save apinstein/e0997661a485c856c7e16b4055e2e0cf to your computer and use it in GitHub Desktop.
Plotting data via bin'd factors
## DEMO of appropriate way to bin variables with a factor
## this is particularly useful if one wants to bin things of asymmetric widths
## alternative would be non-linear axis
library(ggplot2)
library(grid)
library(gridExtra)
library(data.table)
r <- data.table(x = rgeom(1000, .01))
r
# chunk data into factors based on specified ranges
r$xBin <- cut(r$x, breaks=c(0,1,10,50,100,Inf))
r
table(r)
# with factor
pContinuous <- ggplot(r, aes(x=r$x)) +
geom_histogram()
pContinuousLog10 <- ggplot(r, aes(x=r$x)) +
geom_histogram() +
scale_x_log10()
pFactor <- ggplot(r, aes(x=r$xBin)) +
geom_histogram(stat='count')
grid.arrange(pFactor, pContinuous, pContinuousLog10, ncol=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment