1

I have the following script:

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")))

Which in clear format the table looks like this:

           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

Here the column heads are the Tube.numbers and the row titels are the Measurement.locations.

I would like to plot each tube number (so 6 lines) into one graph with on the x axis the Measurement locations and the y axis the Standard deviation (The calculated values in the table)

I tried with matplot but I dont seem to be able to go around the necessity of having to have equal number of rows and columns. I would prefer to do it in a loop.

Can anybody help me?

Much appreciated!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
r.j.mendel
  • 65
  • 1
  • 5
  • It might help to provide the [exact output](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) object you get by calling `dput(SDmeasurement)` and copying its output into your question. – MattLBeck Oct 16 '13 at 14:20
  • 1
    ... or by providing some code that creates a toy example for us to use. Also, in R, loops are mostly used as a last resort. You definitely don't need one here. – Richie Cotton Oct 16 '13 at 14:24
  • Sorry guys, still a beginner at this, does this help to solve my problem? Thanks for your repsonses! – r.j.mendel Oct 16 '13 at 14:29
  • Please note that most of your Tube numbers have the same-ish SD value for the different Measurement locations, and the corresponding lines will be plotted on top of each other. Only the last line added will be visible in the final plot. – Henrik Oct 16 '13 at 15:31

2 Answers2

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)
}

enter image description here

Backlin
  • 14,612
  • 2
  • 49
  • 81
  • Hey Backlin thanks for your response, but with my extra data given, is there a more simpler way to formulate this now? I dont quite understand your script... – r.j.mendel Oct 16 '13 at 14:34
  • Is it better now? The first paragraph was just to parse the data. The point remains though: make an empty plot first with `plot(..., type="n")`, and then add the lines one by one excluding the `NA` values. – Backlin Oct 16 '13 at 14:43
2

Another option is use ggplot2 to get an automatic legend as an extra ( since you should add it manullay with base plot). First you put your data in the long format. I am using here using melt from reshape2. Then you should remove missing values. One option is to use complete.cases. Here my code:

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"))

enter image description here

agstudy
  • 119,832
  • 17
  • 199
  • 261