How to transform density scatter plot?

3

I'd like to know how to transform a scatter plot into a datum plot in the region, for example, where there are few dots, the region's color will be light, where there are many dots the color of the region will be darker. Thanks!

    
asked by anonymous 05.02.2016 / 21:53

1 answer

3

Using ggplot2 the following scatterplot:

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

Thiscanbedonebychangingthelinegeom_point:

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

Youcanalsodoitlikethis:

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

    
06.02.2016 / 14:43