How to transform dot scatter plot into density?

I would like to know how to turn a dot scatter plot into a dot density plot in the region, for example, where there are few points the color of the region will be light, where there are many points the color of the region will be darker. Grateful!

Author: Geólogo, 2016-02-05

1 answers

Using the ggplot2 the following scatter plot:

library(ggplot2)
ggplot(faithful, aes(x = eruptions, y = waiting)) +
  geom_point() +
  xlim(0.5, 6) +
  ylim(40, 110)

insert the description of the image here

Can look like this, just by swapping the line geom_point:

ggplot(faithful, aes(x = eruptions, y = waiting)) +
  stat_density_2d(aes(fill = ..level..), geom = "polygon") +
  xlim(0.5, 6) +
  ylim(40, 110)

insert the description of the image here

You can also do it like this:

ggplot(faithful, aes(x = eruptions, y = waiting)) +
  geom_bin2d() + 
  xlim(0.5, 6) +
  ylim(40, 110)

insert the description of the image here

 3
Author: Daniel Falbel, 2016-02-06 13:43:06