在ggplot2散点图中填充叠加的椭圆

Qku*_*HBH 13 r ggplot2

这个问题是" 如何将数据椭圆叠加在ggplot2散点图上? " 的后续内容.

我想创建使用二维散点图ggplot2充满叠加置信椭圆.使用上面提到的帖子中的Etienne Low-Décarie的解决方案,我确实得到了叠加的省略号.该解决方案是基于stat_ellipsehttps://github.com/JoFrhwld/FAAV/blob/master/r/stat-ellipse.R

问:如何用特定颜色填充椭圆的内部区域(更具体地说,我想使用椭圆边框的颜色与某些alpha)?

这是从上述帖子修改的最小工作示例:

# create data
set.seed(20130226)
n <- 200
x1 <- rnorm(n, mean = 2)
y1 <- 1.5 + 0.4 * x1 + rnorm(n)
x2 <- rnorm(n, mean = -1)
y2 <- 3.5 - 1.2 * x2 + rnorm(n)
class <- rep(c("A", "B"), each = n)
df <- data.frame(x = c(x1, x2), y = c(y1, y2), colour = class)

# get code for "stat_ellipse"
library(devtools)
library(ggplot2)
source_url("https://raw.github.com/JoFrhwld/FAAV/master/r/stat-ellipse.R")

# scatterplot with confidence ellipses (but inner ellipse areas are not filled)
qplot(data = df, x = x, y = y, colour = class) + stat_ellipse()
Run Code Online (Sandbox Code Playgroud)

工作实例的输出: 示例输出的图像

Jul*_*ora 17

正如评论中所提到的,polygon这里需要:

qplot(data = df, x = x, y = y, colour = class) + 
  stat_ellipse(geom = "polygon", alpha = 1/2, aes(fill = class))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述