2013-07-02 53 views
0

我想为CSV文件中的每一行创建图表。目前,我的做法是相当手册:为CSV数据的每一行在R中创建图表

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

是的,它是可能的。如果没有访问你的数据,很难说明如何。 – alexwhan

+0

我已经包含了一个数据样本。正如你所看到的,我在CSV文件中有一个标题行,然后是这些标题下的相应属性数据。 – Outrigger

回答

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)) 
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()` 
+0

实际上,答案的输出是遍历所有行,但只输出图中最后一行(eID)?我怎样才能让它将前面的行(eID)输出到它自己的单独图中? – Outrigger

+0

我目前的解决方案采用了上面的答案,如下所示:' #2.阅读CSV myFile < - “MockData.csv” myData < - read.csv(myFile,skip = 0,nrows = 1)我的数据< - read.csv(myFile,skip = 2,nrows = 1) myData < - read.csv(myFile,skip = 3, nrows = 1) myData < - read.csv(myFile,skip = 4,nrows = 1)' – Outrigger

+0

Alex,注意打印输出的注释。你需要在某个地方捕获输出,但这个过程应该可以工作。如果你有一个工作解决方案,你可以提交它作为答案,并标记为已解决;) –

相关问题