plot/ggplot2 - 填充太多点的区域

gro*_*sgr 6 plot r ggplot2

最终实施 - 没有完成,但正确的方向

想法/问题:您有一个包含许多重叠点的图,并希望用平坦区域替换它们,因此提高了查看绘图的性能.

可能的实现:计算所有点之间的距离矩阵并连接指定距离以下的所有点.

待办事项/未完成:此功能目前适用于手动设置的距离,具体取决于打印图的大小.我在这里停下来因为结果不符合我的审美意识.

中间图的最小例子

set.seed(074079089)
n.points <- 3000

mat <- matrix(rnorm(n.points*2, 0,0.2), nrow=n.points, ncol=2)
colnames(mat) <- c("x", "y")

d.mat <- dist(mat)
fit.mat <-hclust(d.mat, method = "single")
lims <- c(-1,1)
real.lims <- lims*1.1               ## ggplot invokes them approximately

# An attempt to estimate the point-sizes, works for default pdfs pdf("test.pdf")
cutsize <- sum(abs(real.lims))/100  
groups <- cutree(fit.mat, h=cutsize) # cut tree at height cutsize
# plot(fit.mat) # display dendogram

# draw dendogram with red borders around the 5 clusters
# rect.hclust(fit.mat, h=cutsize, border="red")

library(ggplot2)
df <- data.frame(mat)
df$groups <- groups
plot00 <- ggplot(data=df, aes(x,y, col=factor(groups))) + 
    geom_point() + guides(col=FALSE) +  xlim(lims) + ylim(lims)+ 
    ggtitle("Each color is a group")
pdf("plot00.pdf")
print(plot00)
dev.off()
Run Code Online (Sandbox Code Playgroud)

plot00  - 具有组颜色的点

# If less than 4 points are connected, show them seperately
t.groups <- table(groups)   # how often which group
drop.group <- as.numeric(names(t.groups[t.groups<4]))   # groups with less than 4 points are taken together
groups[groups %in% drop.group] <- 0                     # in group 0
df$groups <- groups
plot01 <- ggplot(data=df, aes(x,y, col=factor(groups))) + 
    geom_point() + xlim(lims)+ ylim(lims) + 
    scale_color_hue(l=10)
pdf("plot01.pdf")
print(plot01)
dev.off()
Run Code Online (Sandbox Code Playgroud)

plot01  - 一组中的所有单点

find_hull <- function(df_0) 
{
    return(df_0[chull(df_0$x, df_0$y), ])
}


library(plyr)
single.points.df <- df[df$groups == 0 , ]
connected.points.df <- df[df$groups != 0 , ]
hulls <- ddply(connected.points.df, "groups", find_hull) #  for all groups find a hull
plot02 <- ggplot() + 
    geom_point(data=single.points.df, aes(x,y, col=factor(groups))) + 
    xlim(lims)+ ylim(lims) + 
    scale_color_hue(l=10)
pdf("plot02.pdf")
print(plot02)
dev.off()
Run Code Online (Sandbox Code Playgroud)

plot02  - 仅

plot03 <- plot02
for(grp in names(table(hulls$groups)))
{
    plot03 <- plot03 + geom_polygon(data=hulls[hulls$groups==grp, ],
                                    aes(x,y), alpha=0.4)
}
# print(plot03)
plot01 <- plot01 + theme(legend.position="none")
plot03 <- plot03 + theme(legend.position="none")
# multiplot(plot01, plot03, cols=2)
pdf("plot03.pdf")
print(plot03)
dev.off()
Run Code Online (Sandbox Code Playgroud)

plot03  - 最终

初步问题

我有一个(也许是奇怪的)问题.

在一些情节中,我的分析中有数千个点.为了显示它们,电脑需要相当多的时间,因为有很多点.此后,这些点中的许多点可以重叠,我有一个填充区域(这很好!).为了节省时间/精力显示,只需填充此区域即可自行绘制每个点.

我知道热图等有可能,但这不是我想到的想法.我的想法是这样的:

#plot00: ggplot with many many points and a filled area of points
plot00 <- plot00 + fill.crowded.areas()

# with plot(), I sadly have an idea how to manage it
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?或者这没有人会随时做什么?

# Example code
# install.packages("ggplot2")
library(ggplot2)

n.points <- 10000
mat <- matrix(rexp(n.points*2), nrow=n.points, ncol=2)
colnames(mat) <- c("x", "y")
df <- data.frame(mat)
plot00 <- ggplot(df, aes(x=x, y=y)) + 
    theme_bw()  +                       # white background, grey strips
    geom_point(shape=19)# Aussehen der Punkte

print(plot00)
Run Code Online (Sandbox Code Playgroud)

GGPLOT2

# NO ggplot2
plot(df, pch=19)
Run Code Online (Sandbox Code Playgroud)

情节

编辑:
要获得fdetsch提到的密度图(如何标记名称?),有一些关于此主题的问题.但这不是我想要的东西.我知道我的担心有点奇怪,但密度使得情节有时更加忙碌.

链接到密度主题:

具有太多点的散点图
高密度散点图

Ren*_*rop 1

您可以使用稳健的估计器来估计大多数点的位置并绘制点的凸包,如下所示:

set.seed(1337)
n.points <- 500
mat <- matrix(rexp(n.points*2), nrow=n.points, ncol=2)
colnames(mat) <- c("x", "y")
df <- data.frame(mat)

require(robustbase)
my_poly <- function(data, a, ...){
  cov_rob = covMcd(data, alpha = a)
  df_rob = data[cov_rob$best,]
  ch = chull(df_rob$x, df_rob$y)
  geom_polygon(data = df_rob[ch,], aes(x,y), ...)
}

require(ggplot2)
ggplot() + 
  geom_point(data=df, aes(x,y)) +
  my_poly(df, a = 0.5, fill=2, alpha=0.5) +
  my_poly(df, a = 0.7, fill=3, alpha=0.5) 
Run Code Online (Sandbox Code Playgroud)

这导致:

在此输入图像描述

通过控制 alpha 值,covMcd您可以增加/减少区域的大小。?robustbase::covMcd详情请参阅。顺便说一句:Mcd 代表最小协方差行列式。除了它之外,您还可以使用椭球体内点的百分比MASS::cov.mve来计算最小值椭球体。MASS::cov.mve(..., quantile.used=

对于 2 个以上班级:

my_poly2 <- function(data, a){
  cov_rob = covMcd(data, alpha = a)
  df_rob = data[cov_rob$best,]
  ch = chull(df_rob[,1], df_rob[,2])
  df_rob[ch,]
}

ggplot(faithful, aes(waiting, eruptions, color = eruptions > 3)) +
  geom_point() + 
  geom_polygon(data = my_poly2(faithful[faithful$eruptions > 3,], a=0.5), aes(waiting, eruptions), fill = 2, alpha = 0.5) +
  geom_polygon(data = my_poly2(faithful[faithful$eruptions < 3,], a=0.5), aes(waiting, eruptions), fill = 3, alpha = 0.5)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

或者,如果您对不稳健的椭球体感到满意,请看一下stat_ellipse