R 在散点图中绘制95%置信限

R 在散点图中绘制95%置信限,r,ggplot2,scatter-plot,confidence-interval,R,Ggplot2,Scatter Plot,Confidence Interval,我需要绘制几个定义为 c(x,y,stdev_x,stdev_y) 作为散点图,表示其95%置信限,例如,显示点及其周围的一个轮廓。理想情况下,我希望围绕点绘制椭圆形,但不知道如何绘制。我正在考虑构建样本并绘制它们,添加stat_density2d(),但需要将等高线的数量限制为1,并且无法确定如何进行 require(ggplot2) n=10000 d <- data.frame(id=rep("A", n), se=rnorm(n, 0.18,0.02

我需要绘制几个定义为

c(x,y,stdev_x,stdev_y)

作为散点图,表示其95%置信限,例如,显示点及其周围的一个轮廓。理想情况下,我希望围绕点绘制椭圆形,但不知道如何绘制。我正在考虑构建样本并绘制它们,添加stat_density2d(),但需要将等高线的数量限制为1,并且无法确定如何进行

require(ggplot2)
n=10000
d <- data.frame(id=rep("A", n),
                se=rnorm(n, 0.18,0.02), 
                sp=rnorm(n, 0.79,0.06) )
g <- ggplot (d, aes(se,sp)) +
  scale_x_continuous(limits=c(0,1))+
  scale_y_continuous(limits=c(0,1)) +
  theme(aspect.ratio=0.6)
g + geom_point(alpha=I(1/50)) +
  stat_density2d()
require(ggplot2)
n=10000

d我对ggplot2库一无所知,但您可以使用plotrix绘制椭圆。这个情节看起来像你想要的吗

library(plotrix)
n=10
d <- data.frame(x=runif(n,0,2),y=runif(n,0,2),seX=runif(n,0,0.1),seY=runif(n,0,0.1))
plot(d$x,d$y,pch=16,ylim=c(0,2),xlim=c(0,2))
draw.ellipse(d$x,d$y,d$seX,d$seY)
库(plotrix)
n=10

d首先,将所有绘图保存为对象(更改的限制)

总共有12条等高线,但为了只保留外线,您应该只子集
group==“1-1”
并替换原始信息

gg$data[[2]]<-subset(gg$data[[2]],group=="1-1")

latticeCextra
提供了
面板。ellipse
是一个晶格面板函数,它从二元数据计算并绘制置信椭球,可能由第三个变量分组

这里我根据你的数据画出了0.65和0.95的水平

library(latticeExtra)
xyplot(sp~se,data=d,groups=id,
       par.settings = list(plot.symbol = list(cex = 1.1, pch=16)),
       panel = function(x,y,...){
         panel.xyplot(x, y,alpha=0.2)
         panel.ellipse(x, y, lwd = 2, col="green", robust=FALSE,  level=0.65,...)
         panel.ellipse(x, y, lwd = 2, col="red", robust=TRUE,  level=0.95,...)

       })

刚刚找到函数
stat\u eliple()
(and),它很好地处理了这个问题

g + geom_point(alpha=I(1/10))  +
  stat_ellipse(aes(group=id), color="black")

当然,不同的数据集:

看起来您发现的
stat\u eliple
函数确实是一个很好的解决方案,但这里有另一个(非ggplot),只是为了记录,使用
car
包中的
dataeliple

# some sample data
n=10000
g=4
d <- data.frame(ID = unlist(lapply(letters[1:g], function(x) rep(x, n/g))))
d$x <- unlist(lapply(1:g, function(i) rnorm(n/g, runif(1)*i^2))) 
d$y <- unlist(lapply(1:g, function(i) rnorm(n/g, runif(1)*i^2))) 

# plot points with 95% normal-probability contour
# default settings...
library(car)
with(d, dataEllipse(x, y, ID, level=0.95, fill=TRUE, fill.alpha=0.1))
#一些示例数据
n=10000
g=4

d谢谢你的回答。我们如何知道外轮廓是95%,而不是97%或99%,例如?这可能很明显,但我在文档中没有找到它(包括kde2d的文档)。现在这个解决方案只显示了如何只保留一条等高线。关于这一点,我们需要进一步了解。哇,这是一个优雅的解决方案,谢谢!不过,我还是希望我能在ggplot2中做到这一点。
library(latticeExtra)
xyplot(sp~se,data=d,groups=id,
       par.settings = list(plot.symbol = list(cex = 1.1, pch=16)),
       panel = function(x,y,...){
         panel.xyplot(x, y,alpha=0.2)
         panel.ellipse(x, y, lwd = 2, col="green", robust=FALSE,  level=0.65,...)
         panel.ellipse(x, y, lwd = 2, col="red", robust=TRUE,  level=0.95,...)

       })
g + geom_point(alpha=I(1/10))  +
  stat_ellipse(aes(group=id), color="black")
# some sample data
n=10000
g=4
d <- data.frame(ID = unlist(lapply(letters[1:g], function(x) rep(x, n/g))))
d$x <- unlist(lapply(1:g, function(i) rnorm(n/g, runif(1)*i^2))) 
d$y <- unlist(lapply(1:g, function(i) rnorm(n/g, runif(1)*i^2))) 

# plot points with 95% normal-probability contour
# default settings...
library(car)
with(d, dataEllipse(x, y, ID, level=0.95, fill=TRUE, fill.alpha=0.1))
# with a little more effort...
# random colours with alpha-blending
d$col <- unlist(lapply(1:g, function (x) rep(rgb(runif(1), runif(1), runif(1), runif(1)),n/g)))
# plot points first
with(d, plot(x,y, col=col, pch="."))
# then ellipses over the top
with(d, dataEllipse(x, y, ID, level=0.95, fill=TRUE, fill.alpha=0.1, plot.points=FALSE, add=TRUE,  col=unique(col), ellipse.label=FALSE, center.pch="+"))