R 当';类型=";规范;?
当type=“norm”时,有没有办法计算这个椭圆的面积 默认值为R 当';类型=";规范;?,r,ggplot2,R,Ggplot2,当type=“norm”时,有没有办法计算这个椭圆的面积 默认值为type=“t”type=“norm”显示不同的椭圆,因为它采用多元正态分布而不是多元t分布 下面是代码和绘图(使用与其他帖子类似的代码): library(ggplot2) set.seed(1234) data <- data.frame(x = rnorm(1:1000), y = rnorm(1:1000)) ggplot (data, aes (x = x, y = y))+ geom_point()+
type=“t”
type=“norm”
显示不同的椭圆,因为它采用多元正态分布而不是多元t分布
下面是代码和绘图(使用与其他帖子类似的代码):
library(ggplot2)
set.seed(1234)
data <- data.frame(x = rnorm(1:1000), y = rnorm(1:1000))
ggplot (data, aes (x = x, y = y))+
geom_point()+
stat_ellipse(type = "norm")
#Plot object
p = ggplot (data, aes (x = x, y = y))+
geom_point()+
stat_ellipse(segments=201) # Default is 51. We use a finer grid for more accurate area.
#Get ellipse coordinates from plot
pb = ggplot_build(p)
el = pb$data[[2]][c("x","y")]
# Center of ellipse
ctr = MASS::cov.trob(el)$center
# I tried changing this to 'stats::cov.wt' instead of 'MASS::cov.trob'
#from what is saw from (https://github.com/tidyverse/ggplot2/blob/master/R/stat-ellipse.R#L98)
# Calculate distance to center from each point on the ellipse
dist2center <- sqrt(rowSums((t(t(el)-ctr))^2))
# Calculate area of ellipse from semi-major and semi-minor axes.
These are, respectively, the largest and smallest values of dist2center.
pi*min(dist2center)*max(dist2center)
库(ggplot2)
种子集(1234)
数据好问题,我学到了一些东西。但我不能重现你们的问题,用不同的方法得到(当然)不同的价值观
我认为链接答案中的方法不太正确,因为椭圆中心不是用数据计算的,而是基于椭圆坐标。我已经根据数据进行了更新计算
库(ggplot2)
种子集(1234)
数据这里是如何获得a
和b
(然后区域是pi*a*b
),而不使用stat\u eliple
生成的数据
library(ggplot2)
gg <- ggplot(faithful, aes(eruptions, waiting)) +
geom_point() +
stat_ellipse(type = "norm", segments = 2000)
Sigma <- cov(faithful)
evalues <- eigen(Sigma, symmetric = TRUE, only.values = TRUE)$values
p <- 0.95
r <- 2 * qf(p, 2, nrow(faithful)-1)
( a <- sqrt(r * evalues[1]) )
# 33.55752
( b <- sqrt(r * evalues[2]) )
# 1.216351
### check
ggb <- ggplot_build(gg)
el <- ggb$data[[2]][c("x","y")]
center <- colMeans(faithful)
dist2center <- sqrt(rowSums((t(t(el)-center))^2))
max(dist2center)
# 33.55751
min(dist2center)
# 1.216396
库(ggplot2)
gg下面是我使用来自使用地震数据集的ellipse包的代码提出的解决方案。它要长得多,但更容易理解它是如何工作的(至少对我来说)。我相信面积解是以平方米为单位的。对这种方法有什么想法吗
#Both plots together
Bothgg <- ggplot(quakes, aes(long, lat)) +
geom_point() +
stat_ellipse(type = "t")+ #type = "t" is unnecessary because it is the default, but I put it here for clarity
stat_ellipse(type = "norm", linetype = 2)
Bothgg
###From ellipses code
dfn <- 2
dfd <- nrow(quakes) - 1
segments = 51
level = .95
#Area for solid line
v <- MASS::cov.trob(quakes[ ,c(1,2)])
shape <- v$cov
center <- v$center
chol_decomp <- chol(shape)
radius <- sqrt(dfn * stats::qf(level, dfn, dfd))
angles <- (0:segments) * 2 * pi/segments
unit.circle <- cbind(cos(angles), sin(angles))
ellipse <- as.data.frame(t(center + radius * t(unit.circle %*% chol_decomp)))
centerd = as.data.frame(center)
ellipse$centerLat = centerd[1,1]
ellipse$centerLong = centerd[2,1]
ellipse$distance = distm(ellipse[,c('long','lat')], ellipse[,c('centerLong','centerLat')], fun=distVincentyEllipsoid)
pi*(min(ellipse$distance)/2)*(max(ellipse$distance)/2)
#Area for dashed line
v <- stats::cov.wt(quakes[ ,c(1,2)])
shape <- v$cov
center <- v$center
chol_decomp <- chol(shape)
radius <- sqrt(dfn * stats::qf(level, dfn, dfd))
angles <- (0:segments) * 2 * pi/segments
unit.circle <- cbind(cos(angles), sin(angles))
ellipse <- as.data.frame(t(center + radius * t(unit.circle %*% chol_decomp)))
centerd = as.data.frame(center)
ellipse$centerLat = centerd[1,1]
ellipse$centerLong = centerd[2,1]
ellipse$distance = distm(ellipse[,c('long','lat')], ellipse[,c('centerLong','centerLat')], fun=distVincentyEllipsoid)
pi*(min(ellipse$distance)/2)*(max(ellipse$distance)/2)
#两个图形一起绘制
欢迎来到SO。好的第一个问题。小提示您可以使用data.frame(x=rnorm(1:1000),y=rnorm(1:1000))更轻松地生成数据帧。
这是否回答了您的问题@DanielMuñoz在我的问题中链接了这篇文章,这就是我要问的about@OliviaJeanaaa我理解你,朋友,很抱歉我没有很好地理解你。我认为你应该能够从cov.wt
给出的协方差矩阵中得到面积,尽管不确定如何得到。但是,通过直接在协方差矩阵上使用car::eliple
,您可以完全避免所有ggplot内容。@Axeman我们开始吧。忽略我最后的评论。有关基于协方差矩阵的计算,请参见我的更新答案。我认为这将是一条道路。在这种特殊情况下,可以简单地使用变量varianceAwesome!你能解释一下5.991是从哪里来的吗?@Axeman它代表卡方概率-这是基于对数据的一些假设,这里主要是正态分布。我发现答案中的链接非常有用