如何使用stat_ellipse更改ggplot2中省略号的线型?

时间:2015-07-08 14:16:11

标签: r ggplot2 pca ellipse

所以我试图改变ggplot2中stat_ellipse生成的椭圆上的线型(参见这里https://raw.github.com/low-decarie/FAAV/master/r/stat-ellipse.R)。我可以很容易地手动设置颜色,但是我想给它一个线型矢量来改变椭圆的线型。我已经尝试在stat_ellipse()函数中设置线型,并且还单独使用+ scale_linetype_manual设置线型,但是只有行类型的单个值似乎在stat_ellipse函数中起作用,而scale_linetype_manual没有做任何事情。任何建议表示赞赏!

基本代码和示例图像是

ggplot(data.df,aes(x = PC1,y =PC2, color = mapping$Description))+
  geom_point(size=5,aes(shape=factor(mapping$Status)))+
  stat_ellipse(aes(x = PC1,y=PC2,fill=factor(mapping$Description)),
    geom="polygon",level=0.8,alpha=0.2)+
  scale_fill_manual(values=c("red","red","green","blue","blue"))

映射$ ...只是因素。 PC1和PC2只是具有主要组件和数据的向量.df只是一个包含所有这些内容的数据框。

enter image description here

1 个答案:

答案 0 :(得分:1)

可以通过首先将行类型指定为stat_ellipse的aes中的因子来更改行类型,然后将scale_linetype_manual作为用户aosmith在注释中指出。

ggplot(data.df,aes(x = PC1,y =PC2, color = mapping$Description))+
  geom_point(size=5,aes(shape=factor(mapping$Status)))+
  stat_ellipse(aes(x = PC1,y=PC2,lty=factor(mapping$Status),fill=factor(mapping$Description)),
    geom="polygon",level=0.8,alpha=0.2)+
  scale_fill_manual(values=c("red","red","green","blue","blue"))+
  scale_linetype_manual(values=c(1,2,1,2,1))
相关问题