【问题标题】:How to change the linetype for ellipses in ggplot2 with stat_ellipse?如何使用 stat_ellipse 更改 ggplot2 中椭圆的线型?
【发布时间】:2015-07-08 14:16:10
【问题描述】:

所以我正在尝试更改从 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 只是一个包含所有这些内容的数据框。

【问题讨论】:

  • 您能否添加您尝试使用linetype 的数据以及reproducible example 的数据?如果要将变量映射到 linetype,请将其放在全局或 stat_ellipse aes 中。然后,您可以使用scale_linetype_manual 控制每个组使用哪些行。

标签: r ggplot2 pca ellipse


【解决方案1】:

可以通过首先将线型指定为 stat_ellipse 的 aes 中的一个因子,然后按照用户 aosmith 在 cmets 中指出的 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))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多