1

我附上了一个示例 CSV 文件,其中有 100 个,

它的一些列和它的 19 顶行应被过滤,其余数据应用于绘制图形并以更可用的格式导出图形。

这些数据是由Netlogo行为空间生成的,我找不到直接将输出可视化的好方法,所以每次实验后得到的都是很多csv文件。

数据文件:https ://www.dropbox.com/s/nj243qcs6sx6fu8/Rates.csv 示例输出:https ://www.dropbox.com/s/suomh0vwsfzisj4/SampleGraph.jpg

例如,粗体列是我需要绘制的:

x,y ,color,pen down?,x, y ,color,pen down?,x,y ,color,pen down?,x,y ,color,pen down?,x, y ,color,pen down?,x, y ,color,pen down?

谢谢 ;)

4

3 回答 3

2

这是这个情节的答案:

dat <- read.csv('Rates.csv', stringsAsFactors = FALSE, skip = 19)
colnames(dat)[which(names(dat) %in% c("y", "y.1", "y.2", "y.3"))] <- c("Age", "Revenge", "Homicide", "Hunger")

require(reshape2)
tmp <- melt(dat, id.vars = "x", measure.vars = c("Age", "Revenge", "Homicide", "Hunger"))
require(ggplot2)
ggplot(tmp,aes(x, value)) + 
        geom_point(aes(colour = factor(variable))) +
        xlab("time") +
        ylab("units") +
        ggtitle("My CSV file") +
        labs(colour = "my variables")

在此处输入图像描述

以下是您如何将它与 100 多个 CSV 文件一起使用...

files <- (dir("C:/my-csv-files", recursive=TRUE, full.names=TRUE, pattern="\\.(csv|CSV)$"))
listcsvs <- lapply(files, function(i) read.csv(i,  stringsAsFactors = FALSE, skip = 19))
names(listcsvs) <- files
require(reshape2)
require(ggplot2)
for (i in 1:length(files)) { 
  tmp <- melt(dat, id.vars = "x", measure.vars = c("y", "y.1", "y.2", "y.3"))
  print( ggplot(tmp,aes(x, value)) + 
    geom_point(aes(colour = factor(variable))) +
    xlab("time") +
    ylab("units") +
    ggtitle(names(listcsvs[i])) )
  )
}
于 2013-11-08T09:56:35.207 回答
2

在不知道文件的命名结构的情况下,我不能确定这是否会如您所愿地捕获它们。以下代码将获取当前目录中的所有 .csv 文件并绘制它们,在同一目录中创建绘图的 .png,其文件名与其来源的 .csv 相同(后缀除外)。

# get list of .csv files
files <- dir(".", pattern = "\\.csv", full.names = TRUE, ignore.case = TRUE)
# we'll use ggplot2 for plotting and reshape2 to get the data in shape
library(ggplot2)
library(reshape2)
# loop through the files
for (file in files) {
    # load the data, skipping the first 19 lines
    df <- read.csv(file, as.is=T, skip=19)
    # keep only the columns we want
    df <- df[,c(1,2,6,10,14)]
    # put the names back in 
    names(df) <- c('x','Age','Revenge','Homicide','Hunger')
    # convert to long format
    df <- melt(df, id=c('x'))
    # create the png nam
    name <- gsub(file, pattern='csv', replacement='png')
    # begin png output
    png(name, width=400, height=300)
    # plot
    p <- ggplot(df, aes(x=x, y=value, colour=variable)) +
            # line plot
            geom_line() +
            # use the black and white theme
            theme_bw() +
            xlab('x label') +
            ylab('y label')
    # we have to explicitly print to png
    print(p)
    # finish output to png
    dev.off()
}

Rates.csv 的示例输出图

于 2013-11-08T10:13:28.257 回答
1

这应该让你开始:

Df <- read.csv('https://dl.dropboxusercontent.com/s/nj243qcs6sx6fu8/Rates.csv?dl=1&token_hash=AAEvqZvmuesLhKJSrYHiasj-h0ULrABzbU0Q39bU6FJSCQ', 
           skip=19)

X <- as.matrix(Df[,grep("x",names(Df))])
Y <- as.matrix(Df[,grep("y",names(Df))])

matplot(X, Y, type="l", lty=1)

在此处输入图像描述

如果将其放入循环中,则可以为所有文件生成图表。

于 2013-11-08T09:45:08.173 回答