r-组织ggplot2的物理实验数据



我目前正在尝试使用ggplot2来可视化简单电流-电压实验的结果。当然,我在一组数据上取得了不错的成绩。

然而,我有许多电流电压数据集,我递归地输入到R中,以获得以下组织(参见最小代码):

data.frame(cbind(batch(string list), sample(string list), dataset(data.frame list)))

编辑:我的数据存储在名为batchname_samplenumber.txt的文本文件中,其中包含电压和电流列。我用来导入它们的代码是:

require(plyr)
require(ggplot2)
#VARIABLES
regex <- "([[:alnum:]_]+).([[:alpha:]]+)"
regex2 <- "G5_([[:alnum:]]+)_([[:alnum:]]+).([[:alpha:]]+)"
#FUNCTIONS
getJ <- function(list, k) llply(list, function(i) llply(i, function(i, indix) getElement(i,indix), indix = k))
#FILES
files <- list.files("Data/",full.names= T)
#NAMES FOR FILES
paths <- llply(llply(files, basename),function(i) regmatches(i,regexec(regex,i)))
paths2 <- llply(llply(files, basename),function(i) regmatches(i,regexec(regex2,i)))
names <- llply(llply(getJ(paths, 2)),unlist)
batches <- llply(llply(getJ(paths2, 2)),unlist)
samples <- llply(llply(getJ(paths2, 3)),unlist)
#SETS OF DATA, NAMED
sets <- llply(files,function(i) read.table(i,skip = 0, header = F))
names(sets) <- names
for (i in as.list(names)) names(sets[[i]]) <- c("voltage","current")
df<-data.frame(cbind(batches,samples,sets))    

并且可以通过以下方式生成最小数据:

require(plyr)
batch <- list("A","A","B","B")
sample <- list(1,2,1,2)
set <- list(data.frame(voltage = runif(10), current = runif(10)),data.frame(voltage = runif(10), current = runif(10)),data.frame(voltage = runif(10), current = runif(10)),data.frame(voltage = runif(10), current = runif(10)))
df<-data.frame(cbind(batch,sample,set))

我的问题是:是否可以按原样使用数据,使用类似以下的代码进行绘图(这不起作用)?

ggplot(data, aes(x = dataset$current, y = dataset$voltage, colour = sample)) + facet_wrap(~batch)

更通用的版本是:ggplot2是否能够处理原始物理数据,而不是离散统计数据(如钻石、汽车)?

对于新定义的问题(两个名为"batchname_samplenumber.txt"的列文件),我建议采用以下策略:

read_custom <- function(f, ...) {
 d <- read.table(f, ...)
 names(d) <- c("V", "I")
 ## extract sample and batch from the base filename
 ids <- strsplit(gsub(".txt", "", f), "_")
 d$batch <- ids[[1]][1]
 d$sample <- ids[[1]][2]
 d
}
## list files to read
files <- list.files(pattern=".txt")
## read them all in a single data.frame
m <- ldply(files, read_custom)

尚不清楚样本名称是如何相对于数据集定义的。ggplot2的总体思想是,您应该将所有数据分组为熔化的(长格式)数据帧的形式。

library(ggplot2)
library(plyr)
library(reshape2)
l1 <- list(batch="b1", sample=paste("s", 1:4, sep=""),
           dataset=data.frame(current=rnorm(10*4), voltage=rnorm(10*4)))
l2 <- list(batch="b2", sample=paste("s", 1:4, sep=""),
           dataset=data.frame(current=rnorm(10*4), voltage=rnorm(10*4)))
l3 <- list(batch="b3", sample=paste("s", 1:4, sep=""),
           dataset=data.frame(current=rnorm(10*4), voltage=rnorm(10*4)))
list_to_df <- function(l, n=10){
  m <- l[["dataset"]]
  m$batch <- l[["batch"]]
  m$sample <- rep(l[["sample"]], each=n)
  m
}
## list_to_df(l1)
m <- ldply(list(l1, l2, l3), list_to_df)
ggplot(m) + facet_wrap(~batch)+
  geom_path(aes(current, voltage, colour=sample))

最新更新