get_density: Compute the density of points in a grid

Description Usage Arguments Value Examples

View source: R/plotHelp.R

Description

Credit to Kamil Slowikowski See post: http://slowkow.com/notes/ggplot2-color-by-density/

Usage

1
get_density(x, y, n = 200)

Arguments

x

X coordinates

y

Y coordinates

n

Number of bins to divide grid

Value

A vector of densities for plotting

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
dat <- data.frame(
  x = c(
    rnorm(1e4, mean = 0, sd = 0.1),
    rnorm(1e3, mean = 0, sd = 0.1)
  ),
  y = c(
    rnorm(1e4, mean = 0, sd = 0.1),
    rnorm(1e3, mean = 0.1, sd = 0.2)
  )
)
dat$density <- get_density(dat$x, dat$y)
ggplot2::ggplot(dat) + geom_point(aes(x, y, color = density))

caleblareau/BuenColors documentation built on March 7, 2020, 3:34 p.m.