简体   繁体   English

如何在 R 上的雷达图中添加数据标签?

[英]How to add data labels to a radar chart on R?

I'm currently working with the fmsb package to build a radarchart .我目前正在使用fmsb package 构建一个radarchart

I've made a dataframe like this one to be able to draw the chart:我制作了一个像这样的 dataframe 以便能够绘制图表:

spyder_data <- data.frame(DuelsAeriensOff = c(20, 0, DuelsAeriensOff),
                   DuelsAeriensOffGagnes = c(1, 0, DuelsAeriensOffGagnes),
                   ButTirs16  = c(0.2, 0, ButTirs16),
                   TirsBloques = c(1, 0, TirsBloques),
                   ConversionPasses16 = c(0.2, 0, ConversionPasses16),
                   PassesMatch16 = c(20, 0, PassesMatch16),
                   PassesReussies16 = c(1, 0, PassesReussies16),
                   DangerositePassesCles = c(0.2, 0, DangerositePassesCles),
                   PartCentresSurface = c(1, 0, PartCentresSurface),
                   DistancePassesSurface = c(20, 0, DistancePassesSurface),
                   row.names = c("max", "min", "value"))

and I'm drawing the radarchart with the fonction like this:我正在用这样的功能绘制雷达图:

radarchart(spyder_data,
           vlabels = labels,
           vlcex = 0.5,
           cglcol = "grey",
           cglty = 1,
           pcol = c(scales::alpha("orange", 1)),
           pfcol = c(scales::alpha("orange", 0.4)),
           plwd = 2
           )

Everything is going fine and I'm obtaining this chart:一切都很好,我得到了这张图表:

雷达图

But I would like to add data labels next to my oranges chart's points, I mean the specific values for each point, who are contained in the third column of my spyder_data dataframe.但我想在我的橙子图表点旁边添加数据标签,我的意思是每个点的具体值,它们包含在我的 spyder_data dataframe 的第三列中。

I searched for hours this afternoon but I didn't find anything... except maybe using the function text() but I don't know how to do it since it's a specific chart.今天下午我搜索了几个小时,但我没有找到任何东西......除了可能使用 function text() 但我不知道该怎么做,因为它是一个特定的图表。

If anyone has an idea about my issue, I would be very grateful.如果有人对我的问题有任何想法,我将不胜感激。

I am not aware of a good solution for this - but here's something to consider.我不知道有一个好的解决方案 - 但这里有一些需要考虑的事情。

You can review the code for radarchart here and adapt your own custom function.您可以在此处查看雷达图的代码并调整您自己的自定义radarchart This would allow you to add a line of code to add values as text embedded in radarchart :这将允许您添加一行代码以将值添加为嵌入到radarchart中的文本:

text(xx*scale*1.2, yy*scale*1.2, df[3,], cex = .5)

Here is the entire edited function called radarchart2 :这是整个编辑后的 function 称为radarchart2

radarchart2 <- function(df, axistype=0, seg=4, pty=16, pcol=1:8, plty=1:6, plwd=1,
                       pdensity=NULL, pangle=45, pfcol=NA, cglty=3, cglwd=1,
                       cglcol="navy", axislabcol="blue", title="", maxmin=TRUE,
                       na.itp=TRUE, centerzero=FALSE, vlabels=NULL, vlcex=NULL,
                       caxislabels=NULL, calcex=NULL,
                       paxislabels=NULL, palcex=NULL, ...) {
  if (!is.data.frame(df)) { cat("The data must be given as dataframe.\n"); return() }
  if ((n <- length(df))<3) { cat("The number of variables must be 3 or more.\n"); return() }
  if (maxmin==FALSE) { # when the dataframe does not include max and min as the top 2 rows.
    dfmax <- apply(df, 2, max)
    dfmin <- apply(df, 2, min)
    df <- rbind(dfmax, dfmin, df)
  }
  plot(c(-1.2, 1.2), c(-1.2, 1.2), type="n", frame.plot=FALSE, axes=FALSE, 
       xlab="", ylab="", main=title, asp=1, ...) # define x-y coordinates without any plot
  theta <- seq(90, 450, length=n+1)*pi/180
  theta <- theta[1:n]
  xx <- cos(theta)
  yy <- sin(theta)
  CGap <- ifelse(centerzero, 0, 1)
  for (i in 0:seg) { # complementary guide lines, dotted navy line by default
    polygon(xx*(i+CGap)/(seg+CGap), yy*(i+CGap)/(seg+CGap), lty=cglty, lwd=cglwd, border=cglcol)
    if (axistype==1|axistype==3) CAXISLABELS <- paste(i/seg*100,"(%)")
    if (axistype==4|axistype==5) CAXISLABELS <- sprintf("%3.2f",i/seg)
    if (!is.null(caxislabels)&(i<length(caxislabels))) CAXISLABELS <- caxislabels[i+1]
    if (axistype==1|axistype==3|axistype==4|axistype==5) {
      if (is.null(calcex)) text(-0.05, (i+CGap)/(seg+CGap), CAXISLABELS, col=axislabcol) else
        text(-0.05, (i+CGap)/(seg+CGap), CAXISLABELS, col=axislabcol, cex=calcex)
    }
  }
  if (centerzero) {
    arrows(0, 0, xx*1, yy*1, lwd=cglwd, lty=cglty, length=0, col=cglcol)
  }
  else {
    arrows(xx/(seg+CGap), yy/(seg+CGap), xx*1, yy*1, lwd=cglwd, lty=cglty, length=0, col=cglcol)
  }
  PAXISLABELS <- df[1,1:n]
  if (!is.null(paxislabels)) PAXISLABELS <- paxislabels
  if (axistype==2|axistype==3|axistype==5) {
    if (is.null(palcex)) text(xx[1:n], yy[1:n], PAXISLABELS, col=axislabcol) else
      text(xx[1:n], yy[1:n], PAXISLABELS, col=axislabcol, cex=palcex)
  }
  VLABELS <- colnames(df)
  if (!is.null(vlabels)) VLABELS <- vlabels
  if (is.null(vlcex)) text(xx*1.2, yy*1.2, VLABELS) else
    text(xx*1.2, yy*1.2, VLABELS, cex=vlcex)
  series <- length(df[[1]])
  SX <- series-2
  if (length(pty) < SX) { ptys <- rep(pty, SX) } else { ptys <- pty }
  if (length(pcol) < SX) { pcols <- rep(pcol, SX) } else { pcols <- pcol }
  if (length(plty) < SX) { pltys <- rep(plty, SX) } else { pltys <- plty }
  if (length(plwd) < SX) { plwds <- rep(plwd, SX) } else { plwds <- plwd }
  if (length(pdensity) < SX) { pdensities <- rep(pdensity, SX) } else { pdensities <- pdensity }
  if (length(pangle) < SX) { pangles <- rep(pangle, SX)} else { pangles <- pangle }
  if (length(pfcol) < SX) { pfcols <- rep(pfcol, SX) } else { pfcols <- pfcol }
  for (i in 3:series) {
    xxs <- xx
    yys <- yy
    scale <- CGap/(seg+CGap)+(df[i,]-df[2,])/(df[1,]-df[2,])*seg/(seg+CGap)
    if (sum(!is.na(df[i,]))<3) { cat(sprintf("[DATA NOT ENOUGH] at %d\n%g\n",i,df[i,])) # for too many NA's (1.2.2012)
    } else {
      for (j in 1:n) {
        if (is.na(df[i, j])) { # how to treat NA
          if (na.itp) { # treat NA using interpolation
            left <- ifelse(j>1, j-1, n)
            while (is.na(df[i, left])) {
              left <- ifelse(left>1, left-1, n)
            }
            right <- ifelse(j<n, j+1, 1)
            while (is.na(df[i, right])) {
              right <- ifelse(right<n, right+1, 1)
            }
            xxleft <- xx[left]*CGap/(seg+CGap)+xx[left]*(df[i,left]-df[2,left])/(df[1,left]-df[2,left])*seg/(seg+CGap)
            yyleft <- yy[left]*CGap/(seg+CGap)+yy[left]*(df[i,left]-df[2,left])/(df[1,left]-df[2,left])*seg/(seg+CGap)
            xxright <- xx[right]*CGap/(seg+CGap)+xx[right]*(df[i,right]-df[2,right])/(df[1,right]-df[2,right])*seg/(seg+CGap)
            yyright <- yy[right]*CGap/(seg+CGap)+yy[right]*(df[i,right]-df[2,right])/(df[1,right]-df[2,right])*seg/(seg+CGap)
            if (xxleft > xxright) {
              xxtmp <- xxleft; yytmp <- yyleft;
              xxleft <- xxright; yyleft <- yyright;
              xxright <- xxtmp; yyright <- yytmp;
            }
            xxs[j] <- xx[j]*(yyleft*xxright-yyright*xxleft)/(yy[j]*(xxright-xxleft)-xx[j]*(yyright-yyleft))
            yys[j] <- (yy[j]/xx[j])*xxs[j]
          } else { # treat NA as zero (origin)
            xxs[j] <- 0
            yys[j] <- 0
          }
        }
        else {
          xxs[j] <- xx[j]*CGap/(seg+CGap)+xx[j]*(df[i, j]-df[2, j])/(df[1, j]-df[2, j])*seg/(seg+CGap)
          yys[j] <- yy[j]*CGap/(seg+CGap)+yy[j]*(df[i, j]-df[2, j])/(df[1, j]-df[2, j])*seg/(seg+CGap)
        }
      }
      if (is.null(pdensities)) {
        polygon(xxs, yys, lty=pltys[i-2], lwd=plwds[i-2], border=pcols[i-2], col=pfcols[i-2])
      } else {
        polygon(xxs, yys, lty=pltys[i-2], lwd=plwds[i-2], border=pcols[i-2], 
                density=pdensities[i-2], angle=pangles[i-2], col=pfcols[i-2])
      }
      points(xx*scale, yy*scale, pch=ptys[i-2], col=pcols[i-2])

      ## Line added to add textvalues to points
      text(xx*scale*1.2, yy*scale*1.2, df[3,], cex = .5)
    }
  }
}

And tested (do not have your complete data to test):并经过测试(没有完整的数据要测试):

library(fmsb)

radarchart2(spyder_data,
            vlabels = "labels",
            vlcex = 0.5,
            cglcol = "grey",
            cglty = 1,
            pcol = c(scales::alpha("orange", 1)),
            pfcol = c(scales::alpha("orange", 0.4)),
            plwd = 2
)

Plot Plot

自定义雷达图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM