为每行CSV数据在R中创建图表

为每行CSV数据在R中创建图表,r,R,我正在尝试为CSV文件中的每一行创建一个图表。目前,我的方法相当手动: require(fmsb) range <- c(0, 2) # information about eID1 eID1 <- c(attribute1[1], attribute2[1], attribute3[1], attribute4[1], attribute5[1]) eID1.df <- data.frame(rbind(max=range[2], min=range[1], eID1))

我正在尝试为CSV文件中的每一行创建一个图表。目前,我的方法相当手动:

require(fmsb)
range <- c(0, 2)

# information about eID1
eID1 <- c(attribute1[1], attribute2[1], attribute3[1], 
attribute4[1], attribute5[1])
eID1.df <- data.frame(rbind(max=range[2], min=range[1], eID1)) 

# create a radar chart for eID1
radarchart(eID1.df, axistype=1, pcol=topo.colors(3, 0.5), plty=1, pdensity=10, pfcol=topo.colors(3, 0.5), seg=2, caxislabels=c("Negative", "Neutral", "Positive"),
vlabels=c("Category 1", "Category 2", "Category 3", "Category 4", "Category 5"),
title = "About Employee ID 1")

# information about eID2
eID2 <- c(attribute1[2], attribute2[2], attribute3[2], 
attribute4[2], attribute5[2])
eID2.df <- data.frame(rbind(max=range[2], min=range[1], eID2)) 

# create a radar chart for eID2
radarchart(eID2.df, axistype=1, pcol=topo.colors(3, 0.5), plty=1, pdensity=10, pfcol=topo.colors(3, 0.5), seg=2, caxislabels=c("Negative", "Neutral", "Positive"),
vlabels=c("Category 1", "Category 2", "Category 3", "Category 4", "Category 5"),
title = "About Employee ID 2")

没有具体的数据,很难给出具体的答案

然而,这里有一个通用方法:

#  1.  Create a genearal function for an arbitrary row. 
# There are many ways to go about this, but having it expect all 
# the inputs in a single vector makes step 3 easier

plotFunction <- function(eID, range=c(0, 2) )  {
 # eID is an arbitrary row
 # range is whatever you are using range for (side note: range is also a function, be careful in the usage)

  eID.df <- data.frame(rbind(max=range[2], min=range[1], eID)) 

  # create a radar chart for eID
  radarchart(eID1.df, axistype=1, pcol=topo.colors(3, 0.5), plty=1, pdensity=10, pfcol=topo.colors(3, 0.5), seg=2, caxislabels=c("Negative", "Neutral", "Positive"),
  vlabels=c("Category 1", "Category 2", "Category 3", "Category 4", "Category 5"),
  title = "About Employee ID 1")

  ## I'm not familiar with radarchart.  You might have to wrap it in `print()`
}



#  2. Read in the CSV
myFile <- "~/path/to/file.csv"
myData <- read.csv(myfile)


#  3.  Use `apply` to iterate over the rows: 
apply(myData, 1, plotFunction, range=c(0,2))   # if range needs to vary for each line, have a look at `mapply()`
#1。为任意行创建一个通用函数。
#有很多方法可以做到这一点,但让它期待一切
#单个向量中的输入使步骤3更容易

绘图功能最终解决方案:

require(fmsb)

# automated plot function to plot a radar chart for each of the employees
plotFunction <- function(eID, range=c(0, 2)) {
eID.df <- data.frame(rbind(max=range[2], min=range[1], eID[2:6])) 

# create a radar chart in the form of a png and pdf file for each eID
png(paste("figure/eId", eID[1], "eIDRadarChart.png", sep=""), width=10, height=8, units="in", res=300)
radarchart(eID.df, axistype=1, pcol=topo.colors(1, 0.5), plty=1, pdensity=10, pfcol=topo.colors(1, 0.5), seg=2, caxislabels=c("Negative", "Neutral", "Positive"), vlabels=c("Category 1", "Category 2", "Category 3", "Category 4", "Category 5"), title = paste("About Employee ID", eID[1]))
dev.off()

pdf(paste("figure/PDF/eId", eID[1], "eIDRadarChart.pdf", sep=""), paper="a4")
radarchart(eID.df, axistype=1, pcol=topo.colors(1, 0.5), plty=1, pdensity=10, pfcol=topo.colors(1, 0.5), seg=2, caxislabels=c("Negative", "Neutral", "Positive"), vlabels=c("Category 1", "Category 2", "Category 3", "Category 4", "Category 5"), title = paste("About Employee ID", eID[1]))
dev.off()
}

# read in the CSV
myFile <- "MockData.csv"
myData <- read.csv(myFile)

# use 'apply' to iterate over the rows
apply(myData, 1, plotFunction, range=c(0, 2))
require(fmsb)
#自动绘图功能,为每位员工绘制雷达图

是的,这是可能的。如果无法访问您的数据,则很难显示如何访问。我已经提供了一个数据示例。如您所见,我在CSV文件中有一个标题行,后面是这些标题下的相应属性数据。实际上,答案的输出是它正在迭代所有行,但只输出生成的图形中的最后一行(eID)?我如何使它能够将前面的行(eID)输出到它自己的单独图形中?我目前的解决方案采用了上面的答案:“2”。阅读CSV myFile Alex,注意关于打印输出的注释。您需要在某个地方捕获输出,但该过程应该可以工作。如果您有有效的解决方案,可以将其作为答案提交,并标记为已解决;)非常感谢你的帮助,Richard。我提交了答案,希望其他人会觉得有用。
require(fmsb)

# automated plot function to plot a radar chart for each of the employees
plotFunction <- function(eID, range=c(0, 2)) {
eID.df <- data.frame(rbind(max=range[2], min=range[1], eID[2:6])) 

# create a radar chart in the form of a png and pdf file for each eID
png(paste("figure/eId", eID[1], "eIDRadarChart.png", sep=""), width=10, height=8, units="in", res=300)
radarchart(eID.df, axistype=1, pcol=topo.colors(1, 0.5), plty=1, pdensity=10, pfcol=topo.colors(1, 0.5), seg=2, caxislabels=c("Negative", "Neutral", "Positive"), vlabels=c("Category 1", "Category 2", "Category 3", "Category 4", "Category 5"), title = paste("About Employee ID", eID[1]))
dev.off()

pdf(paste("figure/PDF/eId", eID[1], "eIDRadarChart.pdf", sep=""), paper="a4")
radarchart(eID.df, axistype=1, pcol=topo.colors(1, 0.5), plty=1, pdensity=10, pfcol=topo.colors(1, 0.5), seg=2, caxislabels=c("Negative", "Neutral", "Positive"), vlabels=c("Category 1", "Category 2", "Category 3", "Category 4", "Category 5"), title = paste("About Employee ID", eID[1]))
dev.off()
}

# read in the CSV
myFile <- "MockData.csv"
myData <- read.csv(myFile)

# use 'apply' to iterate over the rows
apply(myData, 1, plotFunction, range=c(0, 2))