0

我正在尝试为 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")

我的问题是:是否可以遍历 CSV 文件中的数据并为每一行的数据创建图表?

原始数据的结构:(在 CSV 文件中)

(eID)   Attribute1,     Attribute2,     Attribute3,     Attribute4,    Attribute5
(1)     1,              2,              1.75,           1.75,          1
(2)     1,              2,              2,              2,             2
(3)     2,              2,              2,              1.5,           1.5
(4)     1,              1,              1,              1,             0
(5)     1,              2,              1,              0,             1
4

2 回答 2

1

最终解决方案:

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))
于 2013-07-03T03:26:00.307 回答
0

It is hard to give a concrete answer without concrete data.

However, here is a general approach:

#  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()`
于 2013-07-02T03:35:00.437 回答