【问题标题】:Specifying colour, transparency and position of arrows (line segments) in ggbiplot在 ggbiplot 中指定箭头(线段)的颜色、透明度和位置
【发布时间】:2014-09-23 12:29:29
【问题描述】:

我正在创建一个包含多变量数据的 PCA 双图。

有没有办法在ggbiplot 中指定线段的颜色/透明度/位置?此命令的所有参数均未提供此选项。

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

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

工作示例,使用 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
ggbiplot(pca, obs.scale = 1, var.scale = 1, groups = class, circle = FALSE, 
         varname.size = 1, varname.adjust = 6)

非常感谢 - 感谢任何帮助!

亲切的问候。

【问题讨论】:

    标签: r ggplot2 ggbiplot


    【解决方案1】:

    看来您需要稍微更改ggbiplot 函数。在控制台中输入ggbiplot,将代码复制到编辑器。在function 中的arglist 中,为箭头添加颜色、线型和透明度(“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) 
    

    然后搜索geom_segment 部分,并为colorlinetypealpha 添加参数:

    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)
    

    将已编辑的功能分配给新名称,例如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
    

    【讨论】:

    • 非常感谢@Henrik,这是一个很大的帮助。我还添加了一个 line.size 参数来传递给 size,因为默认的箭头宽度非常小。
    【解决方案2】:

    感谢您的建议。

    我写了这个函数,以便它可以很容易地使用。您还可以指定要选择的列(=变量)以及分配给每个选定变量的颜色。到目前为止,它仅基于 princomp(),因为我还没有使用 PCA 的其他函数。随意贡献:

    https://github.com/EhrmannS/r-snippets/blob/master/graphics/ggplot/ggbiplot_more_graphics_options.R

    只需指定:

    g <- ggbiplot2(pcobj, 
                   coi <- list(c("variables", "with", "first", "colour"), 
                               c("variables", "with", "second", "colour")), 
                   arrow.alpha = c(0.2, 1, 1),
                   arrow.color = c(muted("red"), "black", "red"))
    

    连同你的其他论点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多