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

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)


Solution 1:[1]

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 :(

Solution 2:[2]

To convey the information of density, a dot-plot or a scatter-plot may be suboptimal as alpha is really hard to identify.

Have a look at either hexplots (http://ggplot2.tidyverse.org/reference/geom_hex.html) or heatmaps (http://ggplot2.tidyverse.org/reference/geom_bin2d.html) in your case.

As I don't know your data, I will just use ggplot2s diamond-dataset. You can create the aforementioned plots like this (both examples are taken from the documentation):

library(ggplot2)
ggplot(diamonds, aes(carat, price)) +
 geom_hex()

Or like this


library(ggplot2)
ggplot(diamonds, aes(carat, price)) +
 geom_bin2d(bins = 100)

Addendum

I just noticed, that your second question regards the color breaks. To allow this use scale_fill_viridis_c(breaks = c(100, 500, 1500, 2500, 4000)) for this effect.

ggplot(diamonds, aes(carat, price)) +
  geom_bin2d(bins = 100) + 
  scale_fill_viridis_c(breaks = c(100, 500, 1500, 2500, 4000))

Created on 2020-04-20 by the reprex package (v0.3.0)

Solution 3:[3]

There is a library that does this well, called ggpointdensity.

It avoids the lack of smoothness for binned plots, and requires no extra calculation of density.

Example from the README:

library(ggplot2)
library(dplyr)
library(viridis)
library(ggpointdensity)

dat <- bind_rows(
  tibble(x = rnorm(7000, sd = 1),
         y = rnorm(7000, sd = 10),
         group = "foo"),
  tibble(x = rnorm(3000, mean = 1, sd = .5),
         y = rnorm(3000, mean = 7, sd = 5),
         group = "bar"))

ggplot(data = dat, mapping = aes(x = x, y = y)) +
  geom_pointdensity() +
  scale_color_viridis()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 slhck