Skip to content
Prev 318838 / 398502 Next

Plotting time data for various countries in same graph

On 03/06/2013 07:06 PM, Anindya Sankar Dey wrote:
Hi Anindya,
This might be a start for you:

asd.df<-read.table(
  text="Time                      Country                 Values
  2010Q1                    India                       5
  2010Q2                    India                       7
  2010Q3                    India                       5
  2010Q4                    India                       9
  2010Q1                    China                     10
  2010Q2                    China                      6
  2010Q3                    China                      9
  2010Q4                     China                     14
  ",header=TRUE)
# Time is read as a factor, so it can be used directly in plotting
as.numeric(asd.df$Time)
[1] 1 2 3 4 1 2 3 4
plot(as.numeric(asd.df$Time)[asd.df$Country == "India"],
  asd.df$Values[asd.df$Country == "India"],
  type="l",col=4,lwd=2,xaxt="n",xlab="Financial Quarter",
  ylab="Value",ylim=c(0,14))
lines(as.numeric(asd.df$Time)[asd.df$Country == "China"],
  asd.df$Values[asd.df$Country == "China"],
  col=2,lwd=2)
axis(1,at=1:4,labels=paste("Q",1:4,sep=""))
legend(2,2,c("India","China"),lty=1,lwd=2,col=c(4,2))

Jim