How to draw one line chart with 3 lines in R?
min<-c(1,1,4,5)
max<-c(8,9,8,10)
d<-c(-2,3,4,3)
We can use matplot
after cbind
ing the vector
s to create a matrix
matplot(cbind(min, max, d), type='l')
To change the 'x axis' labels, we can plot with xaxt=n
and change the labels
with axis
matplot(cbind(min, max, d), type='l', xaxt='n', col=2:4)
axis(1, at=1:4, labels=letters[1:4])
legend('topright', legend=c('min', 'max', 'd'), col=2:4, pch=1)
Another solution to complete @akrun's very good answer, and based on this page:
require(ggplot2)
require(reshape2)
require(directlabels)
min <- c( 1, 1, 4, 5)
max <- c( 8, 9, 8, 10)
d <- c(-2, 3, 4, 3)
df <- data.frame(min=min, max=max, d=d, x=1:4)
df.m <- melt(df,id.vars="x")
p <- ggplot(df.m, aes(x=x, y=value, color=variable)) + geom_line()
direct.label(p)