about setting date

34 views
Skip to first unread message

lily li

unread,
Jun 11, 2014, 7:02:52 PM6/11/14
to ggp...@googlegroups.com
Hi ggplot users,

I want to ask that when using as.Date, it must include year, month and day, or it can include just year and month?
For example, when I use the code below:
com1$date=as.Date(paste(com1$year,com1$month),'%Y %m')

The result dataset has NAs in com1. 

If use the date, like qplot(com1$date,com1$moisture), it has the warning:
Error in seq.int(0, to0 - from, by) : 'to' must be finite
How to correct it, so that the resulted dataset is 2003-01, 2003-02, etc? Thanks very much for your help.

lily li

unread,
Jun 12, 2014, 11:42:00 AM6/12/14
to ggp...@googlegroups.com
Could anyone help me? I just want to plot monthly data, rather than daily data, thanks.

adam.l...@pnc.com

unread,
Jun 12, 2014, 11:46:24 AM6/12/14
to lily li, ggp...@googlegroups.com
Hi Lily,

Check out:

?scale_x_date

I think this is the most flexible way to format dates in ggplot2.


Adam Loveland




From: lily li <choc...@gmail.com>
To: "ggp...@googlegroups.com" <ggp...@googlegroups.com>
Date: 06/12/14 11:42 AM
Subject: Re: about setting date
Sent by: ggp...@googlegroups.com


--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example:
https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options:
http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
ggplot2+u...@googlegroups.com.
For more options, visit
https://groups.google.com/d/optout.


The contents of this email are the property of PNC. If it was not addressed to you, you have no legal right to read it. If you think you received it in error, please notify the sender. Do not forward or copy without permission of the sender. This message may contain an advertisement of a product or service and thus may constitute a commercial electronic mail message under US Law. The postal address for PNC is 249 Fifth Avenue, Pittsburgh, PA 15222. If you do not wish to receive any additional advertising or promotional messages from PNC at this e-mail address, click here to unsubscribe: http://pages.e.pnc.com/globalunsub/
By unsubscribing to this message, you will be unsubscribed from all advertising or promotional messages from PNC. Removing your e-mail address from this mailing list will not affect your subscription to alerts, e-newsletters or account servicing e-mails.

David Guy

unread,
Jun 12, 2014, 11:55:43 AM6/12/14
to lily li, ggp...@googlegroups.com
Lily,

You want to make a distinction between what as.Date stores and what you want to see printed out. You can format the output from as.Date using format()  to include very simple or very complicated date expressions which are in character form.

scale_x_date can also be configured for special formats. But when you define a character value as.Date you want to give it the Year, month and day as a basic identifier.

HTH

David


--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
David Guy, PhD
R Statistical Consulting Inc.
Flemington, NJ USA
 
Office: 908-237-5107
Cell:    917-941-5890
Fax:    908-284-0356
Skype: david.guy1213


Insaf, Tabassum (HEALTH)

unread,
Jun 12, 2014, 12:08:30 PM6/12/14
to lily li, ggp...@googlegroups.com

Lily,

I agree with David. If your original data just has month and year, set each data value as the first of the month or 15th of the month and then plot it using format().

 

Thanks

Tabassum

 

From: ggp...@googlegroups.com [mailto:ggp...@googlegroups.com] On Behalf Of David Guy
Sent: Thursday, June 12, 2014 11:56 AM
To: lily li
Cc: ggp...@googlegroups.com
Subject: Re: about setting date

 

Lily,

Dennis Murphy

unread,
Jun 12, 2014, 12:14:13 PM6/12/14
to lily li, ggp...@googlegroups.com
Hi:

The lack of response to your question is a function of its ambiguity
as well as a lack of a reproducible example. A noted in another reply,
the date format you want in a plot does not have to be the same as the
date format in the data. If you frame your question more concretely
and provide a minimal reproducible example, you'll get a more accurate
and more expeditious reply.

You can see some examples of date formats here:
http://cloud.github.com/downloads/hadley/ggplot2/guide-col.pdf

See section 7.1 and pay attention to the labels argument in the examples.

Dennis

lily li

unread,
Jun 12, 2014, 1:18:02 PM6/12/14
to Dennis Murphy, ggp...@googlegroups.com
Thanks all for your help.

My dataset is as follows:
year  month  moist
2003  01       53.7875
2003  02       55.9037
2003  03       65.9075
2003  04       71.9154

My problem is: how to convert the year and month to another column 'date', which is in the format of '2003-01', '2003-02', etc? I tried to use as.Date but failed.
And about another dataset, the time is like 1-Jan-2000, 2-Jan-2000, how to convert this to 2000-01-01, 2000-01-02? Then plot using the latter format. 

Thanks, Dennis. I read the link you sent out, and see the geom_map function. One input example is file='worldmap.Rdata', how to convert a vector or raster file to this .Rdata file?

Thanks again for your help.

Dennis Murphy

unread,
Jun 12, 2014, 5:06:23 PM6/12/14
to lily li, ggp...@googlegroups.com
Hi:

This is a fairly simple solution to your immediate problem:

DF <- read.table(header = TRUE, text = "
year month moist
2003 01 53.7875
2003 02 55.9037
2003 03 65.9075
2003 04 71.9154")

# Create a variable with a Date format - I arbitrarily set the
# day of the month to be the 1st
DF$date <- with(DF, as.Date(paste(year, month, "01", sep = "-")))

# Once you have a variable of class Date, you can use the function
# scale_x_date() to format the date axis; the date_format() function
# below comes from the scales package, so it needs to be loaded
# (and installed)

library(ggplot2)
library(scales)

# Two different ways to format the date axis
ggplot(DF, aes(x = date, y = moist)) +
geom_line() + geom_point() +
scale_x_date(breaks = DF$date, labels = date_format("%Y-%m"))

ggplot(DF, aes(x = date, y = moist)) +
geom_line() + geom_point() +
scale_x_date(breaks = DF$date, labels = date_format("%b\n%Y"))

The possible formats you can use are listed in ?strptime.

More inline....

On Thu, Jun 12, 2014 at 10:17 AM, lily li <choc...@gmail.com> wrote:
> Thanks all for your help.
>
> My dataset is as follows:
> year month moist
> 2003 01 53.7875
> 2003 02 55.9037
> 2003 03 65.9075
> 2003 04 71.9154
>
> My problem is: how to convert the year and month to another column 'date',
> which is in the format of '2003-01', '2003-02', etc? I tried to use as.Date
> but failed.
> And about another dataset, the time is like 1-Jan-2000, 2-Jan-2000, how to
> convert this to 2000-01-01, 2000-01-02? Then plot using the latter format.
>
> Thanks, Dennis. I read the link you sent out, and see the geom_map function.
> One input example is file='worldmap.Rdata', how to convert a vector or
> raster file to this .Rdata file?

A .Rdata file is an R binary file. You load it into a session with

load("myfile.Rdata")

To use geom_map() in ggplot2, the input file to the function must be a
data frame.
See http://docs.ggplot2.org/current/fortify.sp.html if the file is of
a class generated
by the sp package - that shows you how to convert a spatial data set to a data
frame readable by ggplot2.

If your input file is of some class not associated with the sp package, you'll
have to do some web searching unless some kind soul provides the links to you.
There is information on the web pertaining to plotting maps of various
formats in
ggplot2, but I'm not aware of any document or web site that has put it
all together.

You might also want to look into the ggmap package, which provides more
flexibility in map formats than does ggplot2 but uses ggplot2 to
produce the graphics.
See http://journal.r-project.org/archive/2013-1/kahle-wickham.pdf for
an overview of
the package's capabilities.

Dennis
Reply all
Reply to author
Forward
0 new messages