应用stat_ellipse

时间:2019-06-06 03:12:54

标签: r ggplot2

我有自己的数据框:

test <- read.table(text = "id   pc1 pc2 pc3 Agroup  Bgroup
A1  -0.129930482    -0.018973092    0.008513136 A   Big
A2  -0.102143393    -0.016435414    0.016676682 A   Big
A3  -0.109578301    -0.01841788 0.016013233 A   Big
A4  -0.193120038    0.005807723 0.134615624 A   Big
A5  -0.190417928    0.003890153 0.129485682 A   Big
A6  -0.197611556    0.015157595 0.128532864 A   Big
A7  -0.161689559    -0.015019394    -0.027334255    A   Big
A8  -0.167302706    -0.021986733    -0.036093066    A   Big
A9  -0.163510462    -0.028561404    -0.051607276    A   Big
B1  0.057531585 -0.169399591    0.119578024 B   Big
B2  0.080965446 -0.156989768    0.12677146  B   Big
B3  0.073448144 -0.160349211    0.123441267 B   Big
B4  0.076568502 -0.089241009    -0.031250932    B   Big
B5  0.088085588 -0.059495295    -0.009239871    B   Small
B6  0.087626231 -0.064351002    -0.006697004    B   Small
B7  0.035071855 0.107124946 0.003178192 B   Small
B8  0.036478974 0.101984278 -0.002018618    B   Small
B9  0.033504132 0.099925374 -0.004985971    B   Small
C1  0.066332615 -0.061698336    -0.026622501    C   Small
C2  0.069818463 -0.077728241    -0.03427275 C   Small
C3  0.076282473 -0.070022474    -0.025407112    C   Small
C4  0.044764509 -0.126369255    -0.104260416    C   Small
C5  0.035967207 -0.130660359    -0.109687301    C   Small
C6  0.048683016 -0.102567141    -0.093864923    C   Small
C7  -0.032066776    -0.04061788 -0.163315462    C   Small
C8  -0.042253269    -0.049473754    -0.169515012    C   Small
C9  -0.048820978    -0.051181292    -0.171750722    C   Small", stringsAsFactors = FALSE, header = TRUE)

我在上面绘制了一个椭圆形的散点图:

library(ggplot2)
ggplot(test,aes(x=pc1,y=pc2,fill=Agroup,shape=Agroup))+geom_point(size=8,alpha = 0.8)+scale_fill_manual(name="Groups",values = c("red","blue","yellow"))+scale_shape_manual(name="Groups",values=c(21,22,23))+stat_ellipse(aes(group=Bgroup,color=Bgroup),size=2,alpha=0.5)

,一切顺利: enter image description here

但是,我希望填充椭圆,然后将代码更改为:

ggplot(test,aes(x=pc1,y=pc2,fill=Agroup,shape=Agroup))+geom_point(size=8,alpha = 0.8)+scale_fill_manual(name="Groups",values = c("red","blue","yellow"))+scale_shape_manual(name="Groups",values=c(21,22,23))+stat_ellipse(aes(group=Bgroup,fill=Bgroup),size=2,alpha=0.5,geom = "polygon")

然后我得到了错误:

Error: Insufficient values in manual scale. 5 needed but only 3 provided.

我认为是因为一开始我就已经有了fill,我该如何解决这个问题并得到我想要的东西?

1 个答案:

答案 0 :(得分:2)

这是两种可能的方法。

1)在aes之外为椭圆手动定义颜色:

ggplot(test,aes(x=pc1,y=pc2,fill=Agroup,shape=Agroup))+
    geom_point(size=8,alpha = 0.8)+
    scale_fill_manual(name="Groups",values = c("red","blue","yellow"))+
    scale_shape_manual(name="Groups",values=c(21,22,23))+
    stat_ellipse(data = filter(test, Bgroup == "Big"), aes(group=Bgroup), 
                 fill = "green", size=2, alpha=0.3,geom = "polygon") +
    stat_ellipse(data = filter(test, Bgroup == "Small"), aes(group=Bgroup), 
                 fill = "purple", size=2, alpha=0.3,geom = "polygon")

enter image description here

2)(就图例而言,这还不是完全成熟,只是围绕颜色选择进行手动操作。)您可以调整数据的形状以将要填充的分类放在一栏中,然后分别过滤{{1} }术语仅限于geom_point值,而Agroup仅限于geom_ellipse值。

Bgroup

enter image description here