[R] R not ploting lines in the correct order

PIKAL Petr petr.pikal at precheza.cz
Fri Sep 27 16:26:22 CEST 2013


OK

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Shane Carey
> Sent: Friday, September 27, 2013 4:06 PM
> To: Sarah Goslee
> Cc: r-help at r-project.org
> Subject: Re: [R] R not ploting lines in the correct order
> 
> Ah ok, Here we go:
> 
> structure(list(DATE = structure(c(1348477980, 1350386220, 1351114980,
> 1347388440, 1345650300, 1344558600, 1344955920, 1348489140, 1351169100,
> 1352297220, 1350471060, 1352303640, 1351797660, 1351772580, 1349213100,
> 1354275600, 1351257660, 1345728480, 1355914440, 1344552300, 1347442740,
> 1349954880, 1344874500, 1347274860, 1345546500, 1354269780, 1351271280,
> 1349268300), class = c("POSIXct", "POSIXt"), tzone = "GMT"),
>     DATA = c(5.61, 5.8, 6.01, 5.65, 5.27, 5.67, 5.66, 5.81, 5.67,
>     5.46, 5.81, 5.86, 5.7, 5.33, 5.99, 5.7, 6.06, 5.47, 6.42,
>     6.05, 5.49, 5.45, 5.48, 5.61, 5.96, 5.37, 5.77, 5.81)), .Names =
> c("DATE",
> "DATA"), row.names = c(NA, -28L), class = "data.frame")
> 
> The wrong points are being joined by lines. If I use qplot from the
> ggplot2
> library it plots fine, but I want to use plot as it is easier to
> customise.

Well, it is a matter of taste, ggplots are maybe more complicated but they allow you to do fairly broader customisation quite easily if you find how.

Now you shall tell us what is your plotting command.

this can not work as you are plotting X and Y objects
> > > plot(X$Data,Y$Date,yaxt="n", xaxt="n",type="o")
> > > lines(X$Data,Y$Date,pch=21, col="blue",type="o")

If I name your data frame to data and change plotting commands to 

plot(data$DATA,data$DATE,yaxt="n", xaxt="n",type="o")
lines(data$DATA,data$DATE,pch=21, col="blue",type="o")

I get some mess due to the fact that DATE is not sorted (and I believe that your intention is to have date values on x axis).

so making your data.frame ordered

ooo<-order(data$DATE)
data.o<-data[ooo,]
plot(data.o$DATE,data.o$DATA,yaxt="n", xaxt="n",type="o")
lines(data.o$DATE,data.o$DATA,pch=21, col="blue",type="o")

gives me quite reasonable result.

Regards
Petr


> 
> Thanks again.
> 
> 
> On Fri, Sep 27, 2013 at 2:36 PM, Sarah Goslee
> <sarah.goslee at gmail.com>wrote:
> 
> > If you use dput() correctly, those of us following along via email
> can
> > create an exact duplicate of your R object. All you need to do is:
> > dput(X)
> > and paste the resulting output into email.
> >
> > You see, if I look at your object as you pasted it in, I can't tell
> if
> > your Date column is a date format, or character, or factor, or who
> > knows what, and that may matter for answering your question.
> >
> > Also, what are "the wrong points"? What points do you expect to be
> > connected?
> >
> > Sarah
> >
> >
> > On Fri, Sep 27, 2013 at 8:13 AM, Shane Carey <careyshan at gmail.com>
> wrote:
> > > Hi Sarah,
> > >
> > > thanks for your reply. Im not sure how to use dput? If I create an
> > object of
> > > my data, does that mean you can read it in from your side or
> something?
> > >
> > > Here is my code:
> > > X
> > > Data         Date
> > > 5.61 24/09/2012 09:13
> > > 5.80 16/10/2012 11:17
> > > 6.01 24/10/2012 21:43
> > > 5.65 11/09/2012 18:34
> > > 5.27 22/08/2012 15:45
> > > 5.67 10/08/2012 00:30
> > > 5.66 14/08/2012 14:52
> > > 5.81 24/09/2012 12:19
> > > 5.67 25/10/2012 12:45
> > > 5.46 07/11/2012 14:07
> > > 5.81 17/10/2012 10:51
> > > 5.86 07/11/2012 15:54
> > > 5.70 01/11/2012 19:21
> > > 5.33 01/11/2012 12:23
> > > 5.99 02/10/2012 21:25
> > > 5.70 30/11/2012 11:40
> > > 6.06 26/10/2012 13:21
> > > 5.47 23/08/2012 13:28
> > > 6.42 19/12/2012 10:54
> > > 6.05 09/08/2012 22:45
> > > 5.49 12/09/2012 09:39
> > > 5.45 11/10/2012 11:28
> > > 5.48 13/08/2012 16:15
> > > 5.61 10/09/2012 11:01
> > > 5.96 21/08/2012 10:55
> > > 5.37 30/11/2012 10:03
> > > 5.77 26/10/2012 17:08
> > > 5.81 03/10/2012 12:45
> > > dput(X, "X")
> > > plot(X$Data,Y$Date,yaxt="n", xaxt="n",type="o")
> > > lines(X$Data,Y$Date,pch=21, col="blue",type="o")
> > >
> > >
> > > Thanks again
> > >
> > >
> > > On Thu, Sep 26, 2013 at 7:23 PM, Sarah Goslee
> <sarah.goslee at gmail.com>
> > > wrote:
> > >>
> > >> Hi Shane,
> > >>
> > >> Please use dput() to provide your data, rather than pasting it in
> so
> > >> that we can work from the same R object you are. Please also
> provide
> > >> the code you're using to make the graph.
> > >>
> > >> Sarah
> > >>
> > >> On Thu, Sep 26, 2013 at 1:56 PM, Shane Carey <careyshan at gmail.com>
> > wrote:
> > >> > Hi,
> > >> >
> > >> > I have a set of x, y points where x represents dates and y
> actual
> > >> > values. I
> > >> > am trying to plot a line graph of the data with points on top,
> but R
> > is
> > >> > connecting the wrong points with lines. Does anyone know how I
> can
> > >> > rectify
> > >> > this. Please see sample below:
> > >> >
> > >> > x=
> > >> >  24/09/2009 09:13  16/10/2009 11:17  24/10/2009 21:43
> 11/09/2009
> > >> > 18:34  22/08/2009
> > >> > 15:45  10/08/2009 00:30  14/08/2009 14:52  24/09/2009 12:19
> > >> > 25/10/2009 12:45  07/11/2009 14:07  17/10/2009 10:51  07/11/2009
> 15:54
> > >> > 01/11/2009 19:21  01/11/2009 12:23  02/10/2009 21:25  30/11/2009
> 11:40
> > >> > 26/10/2009 13:21  23/08/2009 13:28  19/12/2009 10:54  09/08/2009
> 22:45
> > >> >  12/09/2009
> > >> > 09:39  11/10/2009 11:28  13/08/2009 16:15  10/09/2009 11:01
> >  21/08/2009
> > >> > 10:55  30/11/2009 10:03  26/10/2009 17:08  03/10/2009 12:45
> > >> >
> > >> > y=
> > >> >  4.2537264  4.397792  4.5570224  4.284056  3.9959248  4.2992208
> > >> > 4.2916384
> > >> > 4.4053744  4.2992208  4.1399904  4.4053744  4.4432864  4.321968
> > >> > 4.0414192
> > >> > 4.5418576  4.321968  4.5949344  4.1475728  4.8679008  4.587352
> > >> > 4.1627376
> > >> > 4.132408  4.1551552  4.2537264  4.5191104  4.0717488  4.3750448
> > >> > 4.4053744
> > >> > Thanks
> > >> > --
> > >> > Shane
> > >> >
> > >>
> > >> --
> > >> Sarah Goslee
> > >> http://www.functionaldiversity.org
> > >
> > >
> > >
> > >
> > > --
> > > Shane
> >
> 
> 
> 
> --
> Shane
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list