I am trying to master plotting skills in R and for the purpose I am using the following code to generate various time series plots all at once.
pl <- as.character()
for (k in (2:ncol(db))){
h <- paste0("ts.plot(cbind(db[,1], y = db[,",k,"]), type = 'b',
gpars = list(col = c('black', 'red')),
main = 'copper & ",colnames(db[,k]),"');")
pl <- paste0(pl, h)
}
eval(parse(text=substr(pl, 1, (nchar(pl)-1))))
The code works fine, however I do not manage to understand something:
here I am always trying to plot the series in
db[,1]
against all the others. How can I plot them into two separate plots one above another usingts.plot
?how can I change the aestetics from points to lines?
I know a better graphical set can be achieved with ggplot2
. However, using the same idea with ggplot2
failed to generate the graphs, either none of them or only the last one (check also what I previously posted here, if you have a better solution please feel free to share: Why is R function parse substituting semicolons with commas?)
In any case, I would be very happy if someone could help with with the two above.
Please, use this db
to reproduce my example (any time series would work)
library(stats)
set.seed(1)
x <- stats::rnorm(n = 100, mean = 0, sd = 1)
y<- stats::rnorm(n = 100, mean = 1, sd = 1)
z <- stats::rnorm(n = 100, mean = 2, sd = 1)
u <- stats::rnorm(n = 100, mean = 3, sd = 1)
db <- cbind(x,y,z,u)