3
votes

I am attempting to place individual points on a plot using ggplot2, however as there are many points, it is difficult to gauge how densely packed the points are. Here, there are two factors being compared against a continuous variable, and I want to change the color of the points to reflect how closely packed they are with their neighbors. I am using the geom_point function in ggplot2 to plot the points, but I don't know how to feed it the right information on color.

Here is the code I am using:

s1 = rnorm(1000, 1, 10)
s2 = rnorm(1000, 1, 10)

data = data.frame(task_number = as.factor(c(replicate(100, 1), 
                                            replicate(100, 2))), 
                  S = c(s1, s2))

ggplot(data, aes(x = task_number, y = S)) + geom_point()

Which generates this plot:

1D Density Plot

However, I want it to look more like this image, but with one dimension rather than two (which I borrowed from this website: https://slowkow.com/notes/ggplot2-color-by-density/):

2D Density Plot

How do I change the colors of the first plot so it resembles that of the second plot?

2
Have you considered using a histogram for displaying 1D density? ggplot(data, aes(x=S)) + geom_histogram(bins=100, fill="grey80", colour="grey30") + facet_grid(task_number ~ .) - bdemarest

2 Answers

4
votes

I think the tricky thing about this is you want to show the original values, and evaluate the density at those values. I borrowed ideas from here to achieve that.

library(dplyr) 

data = data %>%
    group_by(task_number) %>%
    # Use approxfun to interpolate the density back to
    # the original points
    mutate(dens = approxfun(density(S))(S))

ggplot(data, aes(x = task_number, y = S, colour = dens)) +
    geom_point() +
    scale_colour_viridis_c()

Result:

enter image description here

1
votes

One could, of course come up with a meausure of proximity to neighbouring values for each value... However, wouldn't adjusting the transparency basically achieve the same goal (gauging how densely packed the points are)?

geom_point(alpha=0.03)

enter image description here