我有以下脚本:
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,但我似乎无法绕过必须拥有相同数量的行和列的必要性。我宁愿在一个循环中这样做。
有人能帮助我吗?
非常感谢!
答案 0 :(得分: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)
}
答案 1 :(得分:2)
另一种选择是使用ggplot2
来获取一个额外的自动图例(因为你应该用基础图添加manullay)。
首先,您将数据放在长格式中。我在这里使用melt
中的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"))