How can a data ellipse be superimposed on a ggplot2 scatterplot?

后端 未结 2 1191
有刺的猬
有刺的猬 2020-12-01 02:10

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



        
2条回答
  •  春和景丽
    2020-12-01 02:48

    Keelan Evanini, Ingrid Rosenfelder and Josef Fruehwald (JoFrhwld@gmail.com) have created a ggplot2 stat implementation of a 95% confidence interval ellipses (and an easier way to plot ellipses in ggplot2):

    GitHub stat-ellipse.R

    their site

    You can use it as:

    library(ggplot2)
    library(devtools)
    library(digest)
    source_url("https://raw.github.com/low-decarie/FAAV/master/r/stat-ellipse.R")    
    qplot(data=df, x=x, y=y, colour=colour)+stat_ellipse()
    

    enter image description here

    To create the data

    set.seed(101)
    n <- 1000
    x <- rnorm(n, mean=2)
    y <- 1.5 + 0.4*x + rnorm(n)
    colour <- sample(c("first", "second"), size=n, replace=T)
    df <- data.frame(x=x, y=y, colour=colour)
    

提交回复
热议问题