R – How to color the density of dots in scatter plot using R

ggplot2r

I made a scatter plot by ggplot2 like it

example

but I want to color the density of the dots, I tried adding alpha value but it can not indicate the density well. So how to color the overlapping dots based on their counts?

The data I used looks contain 0.1 million numbers(range from 0 to 1) like this (the first column is x and the second is y):

0.07    0.04
0.02    0.12
0.00    0.03
0.14    0.10

I added alpha value and the plot looks like:

+alpha

The code:

library(ggplot2)
p <- ggplot(file, aes(X1,X2)) + geom_point(size=1,alpha = 0.1)
p + labs(x= " " , y=" ", title=" ") + xlim(0.0,1.0) + ylim(0.0,1.0)

Best Answer

I found some methods:

1) Color scatterplot points by density This one works good.

2) Josh O'Brien's answer This is awesome! I also want to know that how to present the relationship between exact values of density and colors...

3) Create smoothscatter like plots with ggplot2 These two are also good.

I am not good at programming so I can just find some codes provided by others on the Internet :(

Related Topic