这个问题是" 如何将数据椭圆叠加在ggplot2散点图上? " 的后续内容.
我想创建使用二维散点图ggplot2
与充满叠加置信椭圆.使用上面提到的帖子中的Etienne Low-Décarie的解决方案,我确实得到了叠加的省略号.该解决方案是基于stat_ellipse
从https://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)
归档时间: |
|
查看次数: |
7630 次 |
最近记录: |