有重叠点的情节

时间:2013-02-04 18:58:34

标签: r plot

我在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)

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

8 个答案:

答案 0 :(得分: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()

enter image description here

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

ggplot(data = df,aes(x = x,y = y)) + stat_sum(aes(size = ..n..))

答案 1 :(得分: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)

答案 2 :(得分: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)

enter image description here

答案 3 :(得分:4)

你并没有真正要求这种方法,但alpha可能是解决这个问题的另一种方法:

library(ggplot2)
ggplot(data.frame(x=x, y=y), aes(x, y)) + geom_point(alpha=.3, size = 3)

enter image description here

答案 4 :(得分:3)

您需要将参数cex添加到绘图功能中。首先我要做的是使用函数as.data.frametable将数据减少为唯一(x,y)对及其频率:

new.data = as.data.frame(table(x,y))
new.data = new.data[new.data$Freq != 0,] # Remove points with zero frequency

唯一的缺点是它将数字数据转换为因子。所以转换回数字,并绘制!

plot(as.numeric(new.data$x), as.numeric(new.data$y), cex = as.numeric(new.data$Freq))

答案 5 :(得分:2)

您可能还想尝试sunflowerplot

sunflowerplot(x,y)

enter image description here

答案 6 :(得分:1)

让我提出调整积分大小的替代方案。使用尺寸(半径?面积?)的一个缺点是读者对光斑尺寸与基础数值的评估是主观的。

所以,选项1:用透明度绘制每个点---泰勒的忍者! 选项2:使用jitter稍微推动您的数据,以使绘制的点不重叠。

答案 7 :(得分:0)

使用latticetable的解决方案(类似于@R_User但不需要删除0,因为格子完成工作)

   dt <-  as.data.frame(table(x,y))
   xyplot(dt$y~dt$x, cex = dt$Freq^2, col =dt$Freq)

enter image description here