【问题标题】:Plot 95% confidence limits in scatterplot在散点图中绘制 95% 置信限
【发布时间】:2013-02-11 01:55:09
【问题描述】:

我需要绘制几个定义为

的数据点

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

【问题讨论】:

    标签: r ggplot2 scatter-plot confidence-interval


    【解决方案1】:

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

    【讨论】:

    • 哇,这是一个优雅的解决方案,谢谢!不过,我仍然希望我可以在 ggplot2 中做到这一点。
    【解决方案2】:

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

    g <- ggplot (d, aes(se,sp, group=id)) +
      scale_x_continuous(limits=c(0,0.5))+
      scale_y_continuous(limits=c(0.5,1)) +
      theme(aspect.ratio=0.6) + 
      geom_point(alpha=I(1/50)) +
      stat_density2d()
    

    使用函数ggplot_build() 保存用于绘图的所有信息。轮廓存储在对象data[[2]] 中。

    gg<-ggplot_build(g)
    str(gg$data)
    head(gg$data[[2]])
      level         x         y piece group PANEL
    1    10 0.1363636 0.7390318     1   1-1     1
    2    10 0.1355521 0.7424242     1   1-1     1
    3    10 0.1347814 0.7474747     1   1-1     1
    4    10 0.1343692 0.7525253     1   1-1     1
    5    10 0.1340186 0.7575758     1   1-1     1
    6    10 0.1336037 0.7626263     1   1-1     1
    

    等高线一共有12条,但是只保留外线,你应该只子集group=="1-1"并替换原始信息。

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

    然后使用ggplot_gtable() 和grid.draw() 得到你的情节。

    p1<-ggplot_gtable(gg)
    grid.draw(p1)
    

    【讨论】:

    • 感谢您的回答。例如,我们如何知道外轮廓为 95% 而不是 97 或 99%?这可能很明显,但我在文档中没有找到它(包括 kde2d 的)。
    • 现在这个解决方案只展示了如何只保留一个轮廓线。关于这个信心还得再看下去。
    【解决方案3】:

    在这里,我根据您的数据绘制水平 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,...)
    
           })
    

    【讨论】:

      【解决方案4】:

      刚刚找到了函数stat_ellipse()here(和here),它可以很好地处理这个问题。

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

      不同的数据集,当然是:

      【讨论】:

        【解决方案5】:

        看起来您发现的 stat_ellipse 函数确实是一个很好的解决方案,但这里有另一个(非 ggplot),只是为了记录,使用来自 car 包的 dataEllipse。

        # 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="+"))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-11-16
          • 2021-12-12
          • 2017-07-21
          • 2015-01-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多