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

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

问题描述

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

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

我想在 ggplot2 散点图上为称为'site'的因子的每个级别叠加一些这样的省略号,从这个命令:

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

这是运行一个名为 dflat 的数据集,如下所示:

  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) :

 > 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
$ b

过去我设法解决了这个问题,但我不记得它是如何的。看起来geom_path依赖于相同的点而不是绘制新的如果您有任何帮助,我们将不胜感激。

解决方案

也许这可以帮助您:

  #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,平均值= 2)
y <-1.5 * x + 0.4 + rnorm(n)
df <-rbind(df,data.frame(x = x,y = y,group = B))

#计算省略号
libr ary(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)),
center = (平均(x),平均(y))))),组= g))
}
#drawing
library(ggplot2)
p < - ggplot = df,aes(x = x,y = y,color = group))+ geom_point(size = 1.5,alpha = .6)+
geom_path(data = df_ell,aes(x = x,y = y ,color = group),size = 1,linetype = 2)

输出如下所示: p>



这里是更复杂的例子。


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

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

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

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]))

I get an error message: "Error in data.frame(x = c(0.0977338099339815, 0.0844334944904515, 0.0697467834016782, : arguments imply differing number of rows: 50, 211

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.

解决方案

Maybe this could help you:

#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)

Output looks like this:

Here is more complex example.

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

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