有重叠点的情节

Cod*_*Guy 3 plot r

我在R中有重叠点的数据.

x = c(4,4,4,7,3,7,3,8,6,8,9,1,1,1,8)
y = c(5,5,5,2,1,2,5,2,2,2,3,5,5,5,2)
plot(x,y)
Run Code Online (Sandbox Code Playgroud)

如何绘制这些点以使重叠点比例大于非点.例如,如果3点位于(4,5),那么位置(4,5)处的点应该是仅有一个点的点的三倍.

jor*_*ran 8

这是使用的一种方式ggplot2:

x = c(4,4,4,7,3,7,3,8,6,8,9,1,1,1,8)
y = c(5,5,5,2,1,2,5,2,2,2,3,5,5,5,2)
df <- data.frame(x = x,y = y)
ggplot(data = df,aes(x = x,y = y)) + stat_sum()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

默认情况下,stat_sum使用实例的比例.您可以通过执行以下操作来使用原始计数:

ggplot(data = df,aes(x = x,y = y)) + stat_sum(aes(size = ..n..))
Run Code Online (Sandbox Code Playgroud)


The*_*ras 6

这是一个更简单(我认为)的解决方案:

x <- c(4,4,4,7,3,7,3,8,6,8,9,1,1,1,8)
y <- c(5,5,5,2,1,2,5,2,2,2,3,5,5,5,2)
size <- sapply(1:length(x), function(i) { sum(x==x[i] & y==y[i]) })
plot(x,y, cex=size)
Run Code Online (Sandbox Code Playgroud)


Jos*_*ien 5

## Tabulate the number of occurrences of each cooordinate
df <- data.frame(x, y)
df2 <- cbind(unique(df), value = with(df, tapply(x, paste(x,y), length)))

## Use cex to set point size to some function of coordinate count
## (By using sqrt(value), the _area_ of each point will be proportional
##  to the number of observations it represents)
plot(y ~ x, cex = sqrt(value), data = df2, pch = 16)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述