有没有办法使用单独的饼图作为R中的标记来制作折线图?

时间:2018-01-22 19:58:49

标签: r ggplot2 charts pie-chart linechart

我正在寻找一种方法来获得一个包含时间轴上的值的折线图,但是,我需要将这些标记设置为可以通过编程方式生成的饼图。我正在寻找与此类似的东西:line plot with pie charts acting as symbols for the points

这在R中是否可行,如果是这样,我需要下载哪些库来实现这一目标。

假设我有这样的数据集(在逗号分隔的列表中): dataset of various values

我希望在X轴上构建折线图,在Y轴上构造状态。但是,标记应该是基于“数据集”中的“质量”,“成本”和“交付”状态,具有相同比例和不同颜色的饼图。与此类似:example of what is needed

2 个答案:

答案 0 :(得分:2)

不确定是否存在执行此操作的程序包。但是,在# Swiftmailer Configuration swiftmailer: transport: smtp host: smtp.gmail.com username: XXX password: XXX port: 465 encryption: ssl spool: type: file path: '%kernel.root_dir%/spool' 包(特别是plotrix)的帮助和灵感(特别是plotrix::getYmult())(请参阅此处:https://CRAN.R-project.org/package=plotrix)中,它是可行的。

首先定义函数

addPies <- function(x, y=x, radius=0.1, shareVector=c(25, 25, 25, 25),
                    col="cbPalette"){

  #setup

  if (!require('plotrix')) { stop('Need package plotrix. See https://CRAN.R-project.org/package=plotrix') }

  seqD <- seq(0, 2*pi, length=100)

  if(any(grepl("cbPalette", col))){
    #color palette from http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/
    col <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
  } else {
    col <- col
  }

  #iterate over number of circles

  for(j in 1:length(x)){
    xcord <- x[j]
    ycord <- y[j]
    r <- radius[j]
    if(is.list(shareVector)){
      shareVec <- shareVector[[j]]
    } else {
      shareVec <- shareVector
    }
    #the way xx and yy are defined is heavily inspired by the plotrix package
    xx <- cos(seqD)*r+xcord
    yy <- sin(seqD)*getYmult()*r+ycord

    inputPer <- cumsum(shareVec)

    #initiate circle
    inputPush <- 0

    #iterate over number of shares
    for(i in 1:length(inputPer)){

      nullX <- seq(xcord,xx[(inputPush[i]):inputPer[i]][1], length=100)
      nullY <- seq(ycord,yy[(inputPush[i]):inputPer[i]][1], length=100)

      xpol <- c(xx[(inputPush[i]):inputPer[i]], nullX)
      ypol <- c(yy[(inputPush[i]):inputPer[i]], nullY)
      polygon(xpol, ypol, col=col[i], border="white", lwd=2)
      inputPush[i+1] <- inputPer[i]
    }
  }
}

输入是:

x是x坐标的数字(对于单个饼图)或向量(对于多个饼图)。 y同样。 radius同样。 shareVector是一个向量(用于单个饼)或向量列表(多个馅饼),仅适用于整数,应总计为100,否则它将有一个空白点。

示例单饼:

plot(0,0, type="n")
addPies(0)

线上的多个馅饼示例:

xVec <- c(2010, 2011, 2012, 2013)
yVec <- c(20, 50, 10, 35)
radiusVec <- c(0.15, 0.25, 0.1, 0.20)
shareList <- list(c(70, 20, 10), c(20, 50, 30), c(20, 20, 40, 10, 10), c(50, 50))

plot(y=yVec, x=xVec, type='l', xlim=c(2009.5, 2013.5), ylim=c(0, 66), 
  lwd=2, col="lightblue")
addPies(xVec, yVec, radiusVec, shareList)

enter image description here

使用“设备大小”保存绘图,它看起来应该没问题

答案 1 :(得分:1)

这是一个老问题,但为了完整起见,您可以使用 add.pie() 包中的 mapplots