在ggbiplot中指定箭头(线段)的颜色,透明度和位置

Jan*_*anP 3 r ggplot2 ggbiplot

我正在创建一个具有高度多变量数据的PCA双标图.

是否有一种方法可以指定线段的颜色/透明度/位置ggbiplot?此命令的所有参数都不提供此选项.

我知道它ggbiplot是基于ggplot- 它可能接受aes论证吗?或者可以在创建的绘图上使用一层颜色/透明度/位置来覆盖默认值?

具体来说,关于位置,如果可能的话,我想抖动线段(尽管使它们更透明可能已经解决了问题).

R数据集'iris'适合用作我的脚本(可行)的示例:

#load required packages
library(ggplot2)
library(devtools)
library(ggbiplot)

#load dataset
data(iris)

#perform principal component analysis
pca = prcomp(iris[ , 1:4], scale=T)

#define classes, generate & view PCA biplot
class = iris$Species
pca_biplot = ggbiplot(pca,obs.scale = 1, var.scale=1,groups=class,circle=F,varname.size=1,varname.adjust=6)
data_pca
Run Code Online (Sandbox Code Playgroud)

非常感谢 - 任何帮助表示赞赏!

亲切的问候.

Hen*_*rik 6

您似乎需要ggbiplot稍微更改功能.键入ggbiplot控制台,将代码复制到编辑器.在arglistfunction,为箭头的颜色,线型和透明度("alpha")添加"name = expression"术语.

ggbiplot2 <- function (pcobj, choices = 1:2, scale = 1, pc.biplot = TRUE, 
          obs.scale = 1 - scale, var.scale = scale, groups = NULL, 
          ellipse = FALSE, ellipse.prob = 0.68, labels = NULL, labels.size = 3, 
          alpha = 1, var.axes = TRUE, circle = FALSE, circle.prob = 0.69, 
          varname.size = 3, varname.adjust = 1.5, varname.abbrev = FALSE, 
          color = muted("red"), # <- add new arguments to the function
          linetype = "solid",
          alpha_arrow = 1) 
Run Code Online (Sandbox Code Playgroud)

然后搜索geom_segment部分,并添加论据color,linetype以及alpha:

g <- g + geom_segment(data = df.v, aes(x = 0, y = 0, xend = xvar, yend = yvar),
                      arrow = arrow(length = unit(1/2, "picas")),
                      color = color, linetype = linetype, alpha = alpha_arrow)
Run Code Online (Sandbox Code Playgroud)

将编辑的功能分配给新名称,例如ggbiplot2.试试吧,你可以设置箭头默认值以外的值:

ggbiplot2(pca, obs.scale = 1, var.scale = 1, groups = class, circle = F, varname.size = 1, varname.adjust = 6,
color = "blue", linetype = "dashed", alpha_arrow = 0.5) # <- use the new arguments
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述