R 散点图:符号颜色代表重叠点的数量 [英] R Scatter Plot: symbol color represents number of overlapping points

查看:43
本文介绍了R 散点图:符号颜色代表重叠点的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当许多点重叠时,散点图可能难以解释,因为这种重叠会掩盖特定区域中的数据密度.一种解决方案是对绘制点使用半透明颜色,以便不透明区域表明这些坐标中存在许多观察值.

Scatter plots can be hard to interpret when many points overlap, as such overlapping obscures the density of data in a particular region. One solution is to use semi-transparent colors for the plotted points, so that opaque region indicates that many observations are present in those coordinates.

以下是我在 R 中的黑白解决方案的示例:

Below is an example of my black and white solution in R:

MyGray <- rgb(t(col2rgb("black")), alpha=50, maxColorValue=255)
x1 <- rnorm(n=1E3, sd=2)
x2 <- x1*1.2 + rnorm(n=1E3, sd=2)
dev.new(width=3.5, height=5)
par(mfrow=c(2,1), mar=c(2.5,2.5,0.5,0.5), ps=10, cex=1.15)
plot(x1, x2, ylab="", xlab="", pch=20, col=MyGray)
plot(x1, x2, ylab="", xlab="", pch=20, col="black")

然而,我最近在 PNAS 上看到了这篇文章,它采用了类似的方法,但使用热图着色而不是不透明度作为有多少点重叠的指标.这篇文章是 Open Access,因此任何人都可以下载 .pdf 并查看图 1,其中包含我想要创建的图表的相关示例.本文的方法部分表明分析是在 Matlab 中完成的.

However, I recently came across this article in PNAS, which took a similar a approach, but used heat-map coloration as opposed to opacity as an indicator of how many points were overlapping. The article is Open Access, so anyone can download the .pdf and look at Figure 1, which contains a relevant example of the graph I want to create. The methods section of this paper indicates that analyses were done in Matlab.

为方便起见,这里是上述文章中图 1 的一小部分:

For the sake of convenience, here is a small portion of Figure 1 from the above article:

如何在 R 中创建使用颜色而非不透明度作为点密度指标的散点图?

How would I create a scatter plot in R that used color, not opacity, as an indicator of point density?

对于初学者来说,R 用户可以使用 tim.colors() 函数在 install.packages("fields") 库中访问这个 Matlab 配色方案.

For starters, R users can access this Matlab color scheme in the install.packages("fields") library, using the function tim.colors().

是否有一种简单的方法可以制作与上述文章的图 1 类似的图形,但在 R 中?谢谢!

Is there an easy way to make a figure similar to Figure 1 of the above article, but in R? Thanks!

推荐答案

一种选择是使用 densCols() 来提取每个点的核密度.将这些密度映射到所需的色带,并按局部密度增加的顺序绘制点,可以获得与链接文章中的图非常相似的图.

One option is to use densCols() to extract kernel densities at each point. Mapping those densities to the desired color ramp, and plotting points in order of increasing local density gets you a plot much like those in the linked article.

## Data in a data.frame
x1 <- rnorm(n=1E3, sd=2)
x2 <- x1*1.2 + rnorm(n=1E3, sd=2)
df <- data.frame(x1,x2)

## Use densCols() output to get density at each point
x <- densCols(x1,x2, colramp=colorRampPalette(c("black", "white")))
df$dens <- col2rgb(x)[1,] + 1L

## Map densities to colors
cols <-  colorRampPalette(c("#000099", "#00FEFF", "#45FE4F", 
                            "#FCFF00", "#FF9400", "#FF3100"))(256)
df$col <- cols[df$dens]

## Plot it, reordering rows so that densest points are plotted on top
plot(x2~x1, data=df[order(df$dens),], pch=20, col=col, cex=2)

这篇关于R 散点图:符号颜色代表重叠点的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆