原型弧图上的标记点

时间:2014-03-21 16:42:28

标签: r

如何从archetypes包中为archmap添加标签?或者,是否可以在ggplot中重新创建archmap输出?

使用SportsAnalytics演示中的代码(我希望这不是一个糟糕的形式)

library("SportsAnalytics")
library("archetypes")
data("NBAPlayerStatistics0910")

dat <- subset(NBAPlayerStatistics0910,
             select = c(Team, Name, Position,
                        TotalMinutesPlayed, FieldGoalsMade))

mat <- as.matrix(subset(dat, select = c(TotalMinutesPlayed, FieldGoalsMade)))
a3 <- archetypes(mat, 3)

archmap(a3)

我喜欢图表上各点的玩家名称(NBAPlayerStatistics0910 $ Name)。像下面的东西,但更具可读性。

ggplot geom_text example

1 个答案:

答案 0 :(得分:1)

如果你不介意稍微调整一下,你可以从archmap()函数库开始,抛出一个额外的参数并添加一个text()调用:

amap2 <- function (object, a.names, projection = simplex_projection, projection_args = list(), 
          rotate = 0, cex = 1.5, col = 1, pch = 1, xlab = "", ylab = "", 
          axes = FALSE, asp = TRUE, ...) 
{
  stopifnot("archetypes" %in% class(object))
  stopifnot(is.function(projection))
  k <- object$k
  if (k < 3) {
    stop("Need at least 3 archetypes.\n")
  }
  cmds <- do.call(projection, c(list(parameters(object)), projection_args))
  if (rotate != 0) {
    a <- pi * rotate/180
    A <- matrix(c(cos(a), -sin(a), sin(a), cos(a)), ncol = 2)
    cmds <- cmds %*% A
  }
  hmds <- chull(cmds)
  active <- 1:k %in% hmds
  plot(cmds, type = "n", xlab = xlab, ylab = ylab, axes = axes, 
       asp = asp, ...)
  points(coef(object) %*% cmds, col = col, pch = pch)

  ######################
  # PLAY WITH THIS BIT #
  ######################
  text(coef(object) %*% cmds, a.names, pos=4)
  ######################

  rad <- ceiling(log10(k)) + 1.5
  polygon(cmds[hmds, ])
  points(cmds[active, ], pch = 21, cex = rad * cex, bg = "grey")
  text(cmds[active, ], labels = (1:k)[active], cex = cex)
  if (any(!active)) {
    points(cmds[!active, , drop = FALSE], pch = 21, cex = rad * 
             cex, bg = "white", fg = "grey")
    text(cmds[!active, , drop = FALSE], labels = (1:k)[!active], 
         cex = cex, col = "grey20")
  }
  invisible(cmds)
}

amap2(a3, dat$Name)

显然,我完全快速的刺伤并不是你想要的最终结果,但它应该可以帮助你顺利开始(如果我读了你想要做的正确的话)。

vis