如何使用 stat_ellipse 更改 ggplot2 中椭圆的线型?

How to change the linetype for ellipses in ggplot2 with stat_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 只是包含主要成分的向量,data.df 只是一个包含所有这些内容的数据框。

可以通过首先将线类型指定为 stat_ellipse 的 aes 中的一个因素来更改线类型,然后是用户 aosmith 在评论中指出的 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,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))