2013-02-26 46 views
10

この質問は「How can a data ellipse be superimposed on a ggplot2 scatterplot?」のフォローアップです。ggplot2散布図に重ねた楕円を塗ります

ggplot2を使用してを埋め込んだ2次元散布図を作成したいと思います。重ね合わせた信頼の楕円。上記の投稿からのEtienne Low-Décarieのソリューションを使用して、私は重なり合った楕円を働かせます。解決策は以下から入手できstat_ellipseに基づいていますhttps://github.com/JoFrhwld/FAAV/blob/master/r/stat-ellipse.R

Q:私は、より具体的に、私はいくつかのアルファと楕円の境界線の色を使用したい(特定の色の楕円(S)の内側の領域を埋めることができますどのように )?ここで

は、最小限の作業の例では、上記のポストから変更されます。

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

実施例の出力: コメントで述べたようにimage of example output

+0

'stat_ellipse(alpha = 0.4)'を試しましたか? – Arun

+0

ちょうどチェックしました - それは楕円の境界のアルファを変更しますが、内側の領域はまだ塗りつぶされません。 – QkuCeHBH

+2

'geom'は' stat_ellipse'実装ではデフォルトで 'path'と思われます。 'path'が' fill'オプションを持つことができるかどうか疑問です... – Arun

答えて

12

polygonはここに必要とされている:

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

enter image description here

+0

正確に動作します、ありがとう! – QkuCeHBH