# load some libraries
library('ggplot2')
library('reshape2')
d <- data.frame(x=seq(0,10,by=0.05))
d$sinx <- sin(d$x)
d$sin1.5x <- sin(1.5*d$x)
d$cosx <- cos(d$x)
# plot it the hard way
ggplot(data=d) +
geom_line(aes(x=x,y=sinx)) +
geom_line(aes(x=x,y=sin1.5x)) +
geom_line(aes(x=x,y=cosx))
head(d)
## x sinx sin1.5x cosx
## 1 0.00 0.00000000 0.00000000 1.0000000
## 2 0.05 0.04997917 0.07492971 0.9987503
## 3 0.10 0.09983342 0.14943813 0.9950042
## 4 0.15 0.14943813 0.22310636 0.9887711
## 5 0.20 0.19866933 0.29552021 0.9800666
## 6 0.25 0.24740396 0.36627253 0.9689124
# move to long/thin frames to automate stuff
dM <- melt(d,id.vars='x',variable.name='f')
head(dM)
## x f value
## 1 0.00 sinx 0.00000000
## 2 0.05 sinx 0.04997917
## 3 0.10 sinx 0.09983342
## 4 0.15 sinx 0.14943813
## 5 0.20 sinx 0.19866933
## 6 0.25 sinx 0.24740396
head(dM[dM$x %in% c(0.0,0.05),])
## x f value
## 1 0.00 sinx 0.00000000
## 2 0.05 sinx 0.04997917
## 202 0.00 sin1.5x 0.00000000
## 203 0.05 sin1.5x 0.07492971
## 403 0.00 cosx 1.00000000
## 404 0.05 cosx 0.99875026
ggplot(data=dM,aes(x=x,y=value,color=f)) +
geom_point()
# move data the back into wide format other way
dL <- dcast(dM,x~f)
print(head(dL))
## x sinx sin1.5x cosx
## 1 0.00 0.00000000 0.00000000 1.0000000
## 2 0.05 0.04997917 0.07492971 0.9987503
## 3 0.10 0.09983342 0.14943813 0.9950042
## 4 0.15 0.14943813 0.22310636 0.9887711
## 5 0.20 0.19866933 0.29552021 0.9800666
## 6 0.25 0.24740396 0.36627253 0.9689124