Anindya Sankar Dey
2013-Mar-06 08:06 UTC
[R] Plotting time data for various countries in same graph
Hi, I've the following kind of data 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 I needed to plot a graph with the x-axis being time,y-axis being he Values and 2 line graph , one for India and one for counry. I don't have great knowledge on graphics in R. I was trying to use, ggplot(data,aes(x=Time,y=Values,colour=Country)) But this does not help. Can anyone help me with this? -- Anindya Sankar Dey [[alternative HTML version deleted]]
Jim Lemon
2013-Mar-06 11:25 UTC
[R] Plotting time data for various countries in same graph
On 03/06/2013 07:06 PM, Anindya Sankar Dey wrote:> Hi, > > I've the following kind of data > > 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 > > > I needed to plot a graph with the x-axis being time,y-axis being he Values > and 2 line graph , one for India and one for counry. > > I don't have great knowledge on graphics in R. > > I was trying to use, ggplot(data,aes(x=Time,y=Values,colour=Country)) > > But this does not help. > > Can anyone help me with this? >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
Rui Barradas
2013-Mar-06 12:18 UTC
[R] Plotting time data for various countries in same graph
Hello,
You've forgot to use a geom.
Also, to have Time be the x axis variable you need to do a conversion.
library(ggplot2)
dat <- 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)
dat$Time <- as.numeric(sub("Q", "\\.", dat$Time))
p <- ggplot(dat,aes(x=Time,y=Values,colour=Country))
p + geom_line()
Hope this helps,
Rui Barradas
Em 06-03-2013 08:06, Anindya Sankar Dey escreveu:> Hi,
>
> I've the following kind of data
>
> 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
>
>
> I needed to plot a graph with the x-axis being time,y-axis being he Values
> and 2 line graph , one for India and one for counry.
>
> I don't have great knowledge on graphics in R.
>
> I was trying to use, ggplot(data,aes(x=Time,y=Values,colour=Country))
>
> But this does not help.
>
> Can anyone help me with this?
>
Tom Keller
2013-Mar-07 17:06 UTC
[R] Plotting time data for various countries in same graph
ggplot returned a ggplot object with no layers as indicated by the error:
"Error: No layers in plot"
You need to add the layers to the object.
So in you example:
p1 <- ggplot(asd.df, aes(x=Time, y=Values, colour=Country))
then add the points (or something else)
p1 + geom_point()
Thomas (Tom) Keller, PhD email: kellert at ohsu.edu<http://ohsu.edu>, ph:
503.494.2442, office: RJH 5333
url: http://www.ohsu.edu/dnaseq/
On Mar 7, 2013, at 3:00 AM,
<r-help-request@r-project.org<mailto:r-help-request@r-project.org>>
wrote:
From: Jim Lemon <jim@bitwrit.com.au<mailto:jim@bitwrit.com.au>>
Subject: Re: [R] Plotting time data for various countries in same graph
Date: March 6, 2013 3:25:55 AM PST
To: Anindya Sankar Dey
<anindya55@gmail.com<mailto:anindya55@gmail.com>>
Cc: "r-help@r-project.org<mailto:r-help@r-project.org>"
<r-help@r-project.org<mailto:r-help@r-project.org>>
On 03/06/2013 07:06 PM, Anindya Sankar Dey wrote:
Hi,
I've the following kind of data
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
I needed to plot a graph with the x-axis being time,y-axis being he Values
and 2 line graph , one for India and one for counry.
I don't have great knowledge on graphics in R.
I was trying to use, ggplot(data,aes(x=Time,y=Values,colour=Country))
But this does not help.
Can anyone help me with this?
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
[[alternative HTML version deleted]]