1

我有以下脚本:

myfunctionSD2 <- function(mydata) { return(sd(mydata,na.rm=TRUE))}
SDmeas <- tapply(datIn$Measurement,list(as.factor(datIn$Measurement.location),as.factor(datIn$Tube.number)),myfunctionSD2)
SDMeasurement <- SDmeas[order(as.numeric(rownames(SDmeas))), ]
dput(SDMeasurement)
structure(c(0.6555264303958, 0.605687762634627, NA, 0.683435292388886, 
NA, 0.615207645374612, NA, 0.739018717912613, NA, 0.79341715750565, 
NA, 0.769473075819677, NA, NA, NA, NA, NA, 0.6555264303958, 0.605687762634627, 
0.576875822240553, NA, 0.502849025908516, NA, 0.516028792109233, 
NA, 0.486023134629369, NA, 0.489281948774758, NA, 0.464851913610455, 
NA, 0.499546896146173, NA, NA, 0.6555264303958, 0.605687762634627, 
NA, 0.683435292388886, NA, 0.615207645374612, NA, 0.739018717912613, 
NA, 0.79341715750565, NA, 0.769473075819677, NA, NA, NA, NA, 
NA, 0.6555264303958, 0.605687762634627, NA, 0.683435292388886, 
NA, 0.615207645374612, NA, 0.739018717912613, NA, 0.79341715750565, 
NA, 0.769473075819677, NA, 0.773437871034822, NA, NA, NA, 0.6555264303958, 
0.605687762634627, NA, 0.683435292388886, NA, 0.615207645374612, 
NA, 0.739018717912613, NA, 0.79341715750565, NA, 0.769473075819677, 
NA, 0.773437871034822, NA, 0.760395526989779, 0.607323723612999, 
0.6555264303958, 0.605687762634627, NA, 0.683435292388886, NA, 
0.615207645374612, NA, 0.739018717912613, NA, 0.79341715750565, 
NA, 0.769473075819677, NA, 0.773437871034822, NA, 0.760395526989779, 
NA), .Dim = c(17L, 6L), .Dimnames = list(c("1", "3", "4", "5", 
"6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", 
"17", "19"), c("1", "2", "3", "4", "5", "6")))

表格的清晰格式如下所示:

           1         2         3         4         5         6
1  0.6555264 0.6555264 0.6555264 0.6555264 0.6555264 0.6555264
3  0.6056878 0.6056878 0.6056878 0.6056878 0.6056878 0.6056878
4         NA 0.5768758        NA        NA        NA        NA
5  0.6834353        NA 0.6834353 0.6834353 0.6834353 0.6834353
6         NA 0.5028490        NA        NA        NA        NA
7  0.6152076        NA 0.6152076 0.6152076 0.6152076 0.6152076
8         NA 0.5160288        NA        NA        NA        NA
9  0.7390187        NA 0.7390187 0.7390187 0.7390187 0.7390187
10        NA 0.4860231        NA        NA        NA        NA
11 0.7934172        NA 0.7934172 0.7934172 0.7934172 0.7934172
12        NA 0.4892819        NA        NA        NA        NA
13 0.7694731        NA 0.7694731 0.7694731 0.7694731 0.7694731
14        NA 0.4648519        NA        NA        NA        NA
15        NA        NA        NA 0.7734379 0.7734379 0.7734379
16        NA 0.4995469        NA        NA        NA        NA
17        NA        NA        NA        NA 0.7603955 0.7603955
19        NA        NA        NA        NA 0.6073237        NA

这里的列标题是 Tube.numbers,行标题是 Measurement.locations。

我想将每个管号(所以 6 条线)绘制成一个图表,x 轴是测量位置,y 轴是标准偏差(表中的计算值)

我尝试使用 matplot 但我似乎无法绕过必须具有相同数量的行和列的必要性。我宁愿循环进行。

有谁能够帮我?

非常感激!

4

2 回答 2

2
# Make an empty plot
plot(0, 0, type="n", xlim=c(1, 19), ylim=range(SDMeasurement, na.rm=TRUE))

# Add the lines
for(i in seq(df)){
    y <- SDMeasurement[[i]]
    lines(which(!is.na(y)), na.omit(y), col=i)
}

在此处输入图像描述

于 2013-10-16T14:23:33.543 回答
2

另一种选择是使用ggplot2额外的自动图例(因为您应该将其添加到带有基本图的手动图例中)。首先,您将数据以长格式放置。我在这里使用meltfrom reshape2。然后你应该删除缺失值。一种选择是使用complete.cases. 这是我的代码:

library(reshape2)

dat.m <- melt(dat)
library(ggplot2)
ggplot(dat.m[complete.cases(dat.m),]) +
  geom_line(aes(x=Var1,y=value,color=factor(Var2))) +
  xlab("location")+ylab("Measurement")+
  guides(color = guide_legend(title = "Tube numbers"))

在此处输入图像描述

于 2013-10-16T15:03:21.533 回答