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

时间:2013-07-02 02:14:16

标签: 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)) 

# 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

2 个答案:

答案 0 :(得分: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))

答案 1 :(得分:0)

如果没有具体数据,很难给出具体答案。

但是,这是一般方法:

#  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()`