stochastiker
stochastiker

Reputation: 25

Multiple plots in ggplot

I have a combined data set of 8826 Obs and 4 variables. My column names are tVec , yVec, tVec, yVec.

I need to plot the 2 yVec against the x axis as single tVec with legends. I tried the below but plots only one plot.

plotnew <- ggplot(data=combined, aes(x=tVec, y= yVec, colour='variable')) + geom_line()

Plot looks like this: Plot looks like this

Any ides on this. Have tired many examples. Just not getting it right.

Thanks.

Upvotes: 0

Views: 93

Answers (1)

B.Gees
B.Gees

Reputation: 1155

You need to format your input data.frame:

combined = data.frame(tVec=1:100,yVec=rnorm(100),tVec=101:200,yVec=rnorm(100))

df = rbind(data.frame(x=combined$tVec,y=combined$yVec,label="first"),
           data.frame(x=combined$tVec.1,y=combined$yVec.1,label="second"))

library(ggplot2)
plotnew <- ggplot(data=df, aes(x, y, colour=label))+
  geom_line()

enter image description here

Or

df = rbind(data.frame(x=combined$tVec,y=combined$yVec,label="first"),
           data.frame(x=combined$tVec,y=combined$yVec.1,label="second"))

library(ggplot2)
plotnew <- ggplot(data=df, aes(x, y, colour=label))+
  geom_line()

enter image description here

Hope that help

Upvotes: 1

Related Questions