[R] plotting multiple lines on single graph ggplot2

John Kane jrkrideau at yahoo.ca
Mon Nov 28 18:42:09 CET 2011



--- On Mon, 11/28/11, Abdul Wahid Memon <engrwahidmemon at gmail.com> wrote:

> From: Abdul Wahid Memon <engrwahidmemon at gmail.com>
> Subject: Re: [R] plotting multiple lines on single graph ggplot2
> To: "Ben Bolker" <bbolker at gmail.com>
> Cc: r-help at stat.math.ethz.ch
> Received: Monday, November 28, 2011, 11:56 AM
> Its very much simple.
> 
> Simply, if we do like the following
> x<-c(100,200,300,400,500)
> y<-c(1.1, 1.2, 1.3, 1.4, 1.5)
> a<-c(600, 700, 800, 900, 1000)
> b<-(1.5, 1.7, 1.9, 2.1, 2.3)
> plot(x,y)
> points(a,b)
> 
> As you can see the call to points() function will
> superimpose a new
> curve (with some new points on x-axis) on the existing
> plot.
> 
> This is what I want to achieve with qplot or ggplot
> functions
> 
Have you run that code?  Unless you specify the xllm() values it does not work.  

As Ben Bolker and I point out the modified data.frame I supplied does work either with ggplot() or qplot()




> Regards
> On Mon, Nov 28, 2011 at 5:31 PM, Ben Bolker <bbolker at gmail.com>
> wrote:
> > John Kane <jrkrideau <at> yahoo.ca>
> writes:
> >
> >>
> >> There are probably lots of better aproaches but
> this seems to work.
> >>   I just combined the lines into one vector
> >> and assighed a dummy variable to mark the
> diffferent lines
> >>
> >> ibrary(ggplot2)
> >> mydata <- data.frame(xrange <- c(100, 200,
> 300, 400, 500, 600,
> >>         700, 800, 900, 1000),
> >>         yrange = c( 1.1, 1.2, 1.3, 1.4, 1.5,
> 1.5, 1.7, 1.9, 2.0, 2.4),
> >>         mark = c(rep("a",5), rep("b", 5)))
> >>
> >> p <- ggplot(mydata, aes( xrange, yrange,
> colour= mark))
> >>
> >> p <- p + geom_line()
> >>
> >> p
> >
> >  Yes, or
> qplot(xrange,yrange,colour=mark,data="mydata")
> >
> > This was cross-posted to stack overflow: please don't
> crosspost.
> > (I didn't understand the question until just now, it
> was simpler
> > than I thought -- I thought the OP wanted *four* lines
> on the final
> > plot (not "I have four lines of data").

Same here for a while until I suddenly realised the x range did not make sense.



More information about the R-help mailing list