2013-07-02 87 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,注意打印輸出的註釋。你需要在某個地方捕獲輸出,但這個過程應該可以工作。如果你有一個工作解決方案,你可以提交它作爲答案,並標記爲已解決;) –

相關問題