简体   繁体   English

如何在ggplot2中绘制具有不同因子的一维点的密度

[英]How to plot density of points in one dimension with different factors in ggplot2

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. 我正在尝试使用ggplot2在图上放置单个点,但是由于有很多点,因此很难衡量点的密集程度。 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. 我在ggplot2中使用geom_point函数来绘制点,但是我不知道如何在颜色上提供正确的信息。

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: 生成此图:

一维密度图

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/ ): 但是,我希望它看起来更像此图像,但具有一维而不是二维(我是从该网站借来的: https//slowkow.com/notes/ggplot2-color-by-density/ ):

2D密度图

How do I change the colors of the first plot so it resembles that of the second plot? 如何更改第一个图的颜色使其类似于第二个图的颜色?

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: 结果:

在此处输入图片说明

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)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM