1
votes

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)
2
can you share a reproducible example with subset of your data or toy data?Sal
I show some data, it has 100k coordinates...LiSAAA
I think what @Sal meant was that you should provide a reproducible R example. Please provide the Code that lead to the presented plot and share your data (or parts of it) with ?dputloki

2 Answers

2
votes

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

2
votes

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)