一个数据椭圆如何叠加在ggplot2散点图上? [英] How can a data ellipse be superimposed on a ggplot2 scatterplot?

查看:28
本文介绍了一个数据椭圆如何叠加在ggplot2散点图上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 R 函数,它可以为散点图生成 95% 置信度椭圆.输出看起来像这样,每个椭圆(50 行)的默认值为 50 个点:

I have an R function which produces 95% confidence ellipses for scatterplots. The output looks like this, having a default of 50 points for each ellipse (50 rows):

           [,1]         [,2]
 [1,]  0.097733810  0.044957994
 [2,]  0.084433494  0.050337990
 [3,]  0.069746783  0.054891438

我想在一个 ggplot2 散点图上为称为站点"的因子的每个级别叠加多个这样的椭圆,该散点图由以下命令生成:

I would like to superimpose a number of such ellipses for each level of a factor called 'site' on a ggplot2 scatterplot, produced from this command:

> plat1 <- ggplot(mapping=aes(shape=site, size=geom), shape=factor(site)); plat1 + geom_point(aes(x=PC1.1,y=PC2.1))

这是在一个名为 dflat 的数据集上运行的,它看起来像这样:

This is run on a dataset, called dflat which looks like this:

site      geom         PC1.1        PC2.1       PC3.1        PC1.2       PC2.2
1 Buhlen 1259.5649 -0.0387975838 -0.022889782  0.01355317  0.008705276  0.02441577
2 Buhlen  653.6607 -0.0009398704 -0.013076251  0.02898955 -0.001345149  0.03133990

结果很好,但是当我尝试添加椭圆时(假设这个站点名为Buhlen"):

The result is fine, but when I try to add the ellipse (let's say for this one site, called "Buhlen"):

> plat1 + geom_point(aes(x=PC1.1,y=PC2.1)) + geom_path(data=subset(dflat, site="Buhlen"),mapping=aes(x=ELLI(PC1.1,PC2.1)[,1],y=ELLI(PC1.1,PC2.1)[,2]))

我收到一条错误消息:"Data.frame 中的错误(x = c(0.0977338099339815, 0.0844334944904515, 0.0697467834016782, :参数意味着不同的行数:50, 211

我过去曾设法解决此问题,但我不记得是如何解决的.似乎 geom_path 依赖于相同的点而不是绘制新的点.任何帮助将不胜感激.

I've managed to fix this in the past, but I cannot remember how. It seems that geom_path is relying on the same points rather than plotting new ones. Any help would be appreciated.

推荐答案

也许这可以帮到你:

#bootstrap
set.seed(101)
n <- 1000
x <- rnorm(n, mean=2)
y <- 1.5 + 0.4*x + rnorm(n)
df <- data.frame(x=x, y=y, group="A")
x <- rnorm(n, mean=2)
y <- 1.5*x + 0.4 + rnorm(n)
df <- rbind(df, data.frame(x=x, y=y, group="B"))

#calculating ellipses
library(ellipse)
df_ell <- data.frame()
for(g in levels(df$group)){
df_ell <- rbind(df_ell, cbind(as.data.frame(with(df[df$group==g,], ellipse(cor(x, y), 
                                         scale=c(sd(x),sd(y)), 
                                         centre=c(mean(x),mean(y))))),group=g))
}
#drawing
library(ggplot2)
p <- ggplot(data=df, aes(x=x, y=y,colour=group)) + geom_point(size=1.5, alpha=.6) +
  geom_path(data=df_ell, aes(x=x, y=y,colour=group), size=1, linetype=2)

输出如下:

这里是更复杂的例子.

这篇关于一个数据椭圆如何叠加在ggplot2散点图上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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