I am looking for a way to have a line chart with values over a timeline, however, I need the markers to be pie charts that I can programmatically generate. Something similar to this is what I am looking for:
我正在寻找一种方法来获得一个时间轴上的值的折线图,但是,我需要标记是我可以编程生成的饼图。我正在寻找类似于此的东西:
Is this even possible in R, and if so, what libraries would I need to download to achieve this.
这在R中是否可能,如果是这样,我需要下载哪些库来实现这一目标。
Assuming I have a dataset like this(in a comma-separated list):
假设我有这样的数据集(在逗号分隔的列表中):
I want the line chart to be constructed with time on the X-axis and status on the Y-axis. However, the markers should be pie charts with equal proportions with different colors based on the Quality, Cost, and Delivery status in the Dataset. Similar to this:
我希望在X轴上构建折线图,在Y轴上构造状态。但是,标记应该是基于“数据集”中“质量”,“成本”和“交付”状态的具有相同比例和不同颜色的饼图。与此类似:
1 个解决方案
#1
2
Not sure if there exists a package to do this. But with some assistance and inspiration from the plotrix
package (in particular plotrix::getYmult()
) (see more here: https://CRAN.R-project.org/package=plotrix) it is doable.
不确定是否存在执行此操作的程序包。但是从plotrix包(特别是plotrix :: getYmult())获得一些帮助和灵感(参见更多这里:https://CRAN.R-project.org/package=plotrix)它是可行的。
First define the function
首先定义函数
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]
}
}
}
Inputs are:
输入是:
x
is a number (for single pie) or vector (for multiple pies) of x-coordinates. y
same. radius
same. shareVector
is a vector (for single pie) or list of vectors (multiple pies), only for integers and should sum up to 100, or else it will have a blank spot.
x是x坐标的数字(对于单个饼图)或向量(对于多个饼图)。同样的。半径相同。 shareVector是一个矢量(用于单个饼图)或矢量列表(多个饼图),仅适用于整数,应总计为100,否则它将有一个空白点。
Example single pie:
单个馅饼示例:
plot(0,0, type="n")
addPies(0)
Example multiple pies over line:
在线上的多个馅饼示例:
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)
Save plots by using Device Size and it should look okay
使用“设备大小”保存绘图,它看起来应该没问题
#1
2
Not sure if there exists a package to do this. But with some assistance and inspiration from the plotrix
package (in particular plotrix::getYmult()
) (see more here: https://CRAN.R-project.org/package=plotrix) it is doable.
不确定是否存在执行此操作的程序包。但是从plotrix包(特别是plotrix :: getYmult())获得一些帮助和灵感(参见更多这里:https://CRAN.R-project.org/package=plotrix)它是可行的。
First define the function
首先定义函数
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]
}
}
}
Inputs are:
输入是:
x
is a number (for single pie) or vector (for multiple pies) of x-coordinates. y
same. radius
same. shareVector
is a vector (for single pie) or list of vectors (multiple pies), only for integers and should sum up to 100, or else it will have a blank spot.
x是x坐标的数字(对于单个饼图)或向量(对于多个饼图)。同样的。半径相同。 shareVector是一个矢量(用于单个饼图)或矢量列表(多个饼图),仅适用于整数,应总计为100,否则它将有一个空白点。
Example single pie:
单个馅饼示例:
plot(0,0, type="n")
addPies(0)
Example multiple pies over line:
在线上的多个馅饼示例:
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)
Save plots by using Device Size and it should look okay
使用“设备大小”保存绘图,它看起来应该没问题