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

标签 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"))

mapping$... 只是因子。 PC1 和 PC2 只是包含主要成分的向量,而 data.df 只是包含所有这些内容的数据框。

enter image description here

最佳答案

可以通过首先将线型指定为 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))

关于r - 如何使用 stat_ellipse 更改 ggplot2 中椭圆的线型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31295382/

相关文章:

r - 写入文件时如何重命名 igraph 节点 ID

r - 使用 R/tidyverse 将数据框的一列分隔为未定义的列数

r - ggplot2 2.0 新的 stat_ 函数 : setting default scale for given aesthetics

r - 在 ggplot2 标题表达式中使用带有变量的 italic()

C++ Armadillo princomp() 段错误

machine-learning - 带 make_pipeline 的 StandardScaler

r - .Rpres(不是 rmarkdown)两列布局在 IE 中不兼容

r - 如何处理面板数据索引中的NA

r - 如何从高质量的绘图中将绘图导出为静态图像?

matlab - 分类前使用 PCA