-2

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)
zone1
  • 153
  • 1
  • 1
  • 7
  • 1
    possible duplicate of [Plot two graphs in same plot in R](http://stackoverflow.com/questions/2564258/plot-two-graphs-in-same-plot-in-r) – Gabriel Boya Aug 14 '15 at 13:03

2 Answers2

2

We can use matplot after cbinding the vectors 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)
akrun
  • 874,273
  • 37
  • 540
  • 662
2

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)

the result

Vincent Guillemot
  • 3,394
  • 14
  • 21