Jasperovic
Jasperovic

Reputation: 107

R create a row graph

I've got a question regarding a graph made from rows.

Data

testdata = structure(c(38, 38, 38, 38, 33, 32, 33, 31, 25, 26, 25, 23, 10, 
                 9, 9, 8, 5, 4, 3, 5, 0, 0, 0, 0), .Dim = c(4L, 6L), .Dimnames = list(NULL, c("V1", "V2", "V3", "V4", "V5", "V6")))

Output

     V1 V2 V3 V4 V5 V6
[1,] 38 33 25 10  5  0
[2,] 38 32 26  9  4  0
[3,] 38 33 25  9  3  0
[4,] 38 31 23  8  5  0

This matrix contains 4 samples from 6 different time ticks(V1-V6). I basically want a scatterplot showing 4 different lines with their value on the Y-axis and on the X-axis the 6 "time ticks". For this I just want the dots and the "average line" of the dots and trust bands. I've done this before but I somehow can't manage it with each sample on a different row.

Upvotes: 2

Views: 86

Answers (2)

Cath
Cath

Reputation: 24074

To get something like the excel plot example you gave, you can do:

matplot(t(testdata), t="b", pch=c(18, 15, 17, 4), lty=1, col=c("blue", "red", "green", "purple"), lwd=2)

enter image description here

Following @nico advice, you can also use type="o" if you want continuous lines:

matplot(t(testdata), t="o", pch=c(18, 15, 17, 4), lty=1, col=c("blue", "red", "green", "purple"), lwd=2)

enter image description here

Upvotes: 5

Ruthger Righart
Ruthger Righart

Reputation: 4921

Another option in the base plotting system:

ndat<-as.data.frame(testdata)

plot(t(ndat[1,]), type="l", col="red", lwd=2, xlab="x-axis", ylab="values")
lines(t(ndat[2,]), type="l", col="yellow", lwd=2)
lines(t(ndat[3,]), type="l", col="green", lwd=2)
lines(t(ndat[4,]), type="l", col="blue", lwd=2)

enter image description here

Upvotes: 4

Related Questions