date in qplot()

27 views
Skip to first unread message

Luo Lei

unread,
Oct 22, 2012, 1:23:25 AM10/22/12
to ggp...@googlegroups.com

 

 

Hi there

 

I got a quick question on date format in qplot(). I tried to run the line “qplot(YearQuarter,Value, data=day.m,geom="line")” 

YearQuarter  is a string variable which is like ( 2011December). The qplot doesn’t recognise it as a date format. The graph is like below. I wanted the lines to be horizontal rather than vertical.

 

The first line of the day.m is below

  Provider             Type                                      YearQuarter                       Value

1    Other              PM BUSINESS                    2011 December                 NA

> 

 

How can get qplot() to read YearQuarter as a date variable?

 

 

 

Thanks in advance



Luo Lei
Marketing Insight Analyst
Two Degrees Mobile Limited
==========================
(M) +64 27 361 9166
(P)  +64 9 919 7000
www.2degreesmobile.co.nz

Two Degrees Mobile Limited | 131 Khyber Pass Rd | Newmarket | Auckland | New Zealand |
PO Box 8355 | Symonds Street | Auckland 1150 | New Zealand | Fax +64 9 919 7001

   


Disclaimer
The e-mail and any files transmitted with it are confidential and may contain privileged or copyright information. If you are not the intended recipient you must not copy, distribute, or use this e-mail or the information contained in it for any purpose other than to notify us of the error. If you have received this message in error, please notify the sender immediately, by email or phone (+64 9 919 7000) and delete this email from your system. Any views expressed in this message are those of the individual sender, except where the sender specifically states them to be the views of Two Degrees Mobile Limited. We do not guarantee that this material is free from viruses or any other defects although due care has been taken to minimize the risk








Heike Hofmann

unread,
Oct 22, 2012, 10:05:22 AM10/22/12
to Luo Lei, ggp...@googlegroups.com
Luo,

there's a couple of things that you need to take care of, before your chart will work.
First, you'll have to work on the YearQuarter variable - split it in pieces along the white space (look into strsplit) to get Year and Month out.
Then pieve them together into a proper date (look into the package lubridate)
In your line chart you need to set a different grouping variable. You seem to be grouping by YearQuarter at the moment, hence the vertical lines. Instead, identify what the lines have in common across time (is it Provider or Type or both?). Use the grouping variable for that, and you should be getting closer to what you want to show,

HTH,

Heike



On Oct 22, 2012, at 12:23 AM, Luo Lei wrote:

 
Hi there
 
I got a quick question on date format in qplot(). I tried to run the line “qplot(YearQuarter,Value, data=day.m,geom="line")” 
YearQuarter  is a string variable which is like ( 2011December). The qplot doesn’t recognise it as a date format. The graph is like below. I wanted the lines to be horizontal rather than vertical.
 
The first line of the day.m is below
  Provider             Type                                      YearQuarter                       Value
1    Other              PM BUSINESS                    2011 December                 NA
> 
 
How can get qplot() to read YearQuarter as a date variable?
 
<image001.png>
 
 
Thanks in advance


Luo Lei
Marketing Insight Analyst
Two Degrees Mobile Limited
==========================
(M) +64 27 361 9166
(P)  +64 9 919 7000
www.2degreesmobile.co.nz
<imagea8...@871cfeb6.77dd47e4>
Two Degrees Mobile Limited | 131 Khyber Pass Rd | Newmarket | Auckland | New Zealand |
PO Box 8355 | Symonds Street | Auckland 1150 | New Zealand | Fax +64 9 919 7001
Disclaimer
The e-mail and any files transmitted with it are confidential and may contain privileged or copyright information. If you are not the intended recipient you must not copy, distribute, or use this e-mail or the information contained in it for any purpose other than to notify us of the error. If you have received this message in error, please notify the sender immediately, by email or phone (+64 9 919 7000) and delete this email from your system. Any views expressed in this message are those of the individual sender, except where the sender specifically states them to be the views of Two Degrees Mobile Limited. We do not guarantee that this material is free from viruses or any other defects although due care has been taken to minimize the risk














-- 
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

Luo Lei

unread,
Oct 22, 2012, 11:20:51 PM10/22/12
to Heike Hofmann, ggp...@googlegroups.com

 

Hi Heike

 

Thanks for your email. I would like to plot a simple time serie by using qplot() or ggplot()

Before I did plot the table, I have to replace the NA with 0? Is this necessary? Can qplot or ggplot recognise NAs?

 

Here is the screen dump for the data in a melted format

 

Then I will try to run “qplot(Quarter,Customers,facets=PROVIDER~.,data=x1.m,geom="line",main="Portouts")” Nothing happens??? I was hoping to see a line in each box.

Could you tell me where I did wrong? Thanks

 



Luo Lei

Marketing Insight Analyst
Two Degrees Mobile Limited
==========================
(M) +64 27 361 9166
(P)  +64 9 919 7000
www.2degreesmobile.co.nz


Two Degrees Mobile Limited | 131 Khyber Pass Rd | Newmarket | Auckland | New Zealand |
PO Box 8355 | Symonds Street | Auckland 1150 | New Zealand | Fax +64 9 919 7001


   


Roman Luštrik

unread,
Oct 23, 2012, 3:36:50 AM10/23/12
to Luo Lei, Heike Hofmann, ggp...@googlegroups.com
I will encourage you for next time to provide some sample data. Until next time, here's a piece of code that works for me. Is this what you're after?

mydf <- data.frame(
        provider = rep(letters[1:4], 20),
        quart = seq(from = as.Date("2011-09-01"), to = as.Date("2011-09-30"), length.out = 20),
        customers = runif(20) * 200)
head(mydf)

library(ggplot2)
ggplot(mydf, aes(x = quart, y = customers)) + geom_line() + facet_wrap(~ provider)


Cheers,
Roman



--
In God we trust, all others bring data.
imageac2256.png@2e9f361d.b46f4e89
image8fcced.png@3cd7ff82.25324f2f
imagea698bf.JPG@9acb4424.21214970
image9f7cb6.png@945b9515.ac2c4695
image002.png
image9c45b5.png@e0520fb0.3bf64fbe
image001.png

Luo Lei

unread,
Oct 23, 2012, 6:08:01 PM10/23/12
to Roman Luštrik, Heike Hofmann, ggp...@googlegroups.com

 

I attached the data this time... I don’t seem to get anything out by running following code

 

ggplot(x1.m, aes(x=Quarter,y=Customers)) +geom_line() +facet_wrap(~PROVIDER)

with (x1.m,qplot(Quarter,Customers,facets=PROVIDER~.,data=x1.m,geom="line",main="Portouts"))

 

I only got a blank screen?

 

ggplot.Rdata

Ben Bond-Lamberty

unread,
Oct 23, 2012, 8:03:30 PM10/23/12
to ggp...@googlegroups.com
You need

ggplot(x1.m, aes(x=Quarter,y=Customers,group=1)) +geom_line()
+facet_wrap(~PROVIDER)

Ben


On Tue, Oct 23, 2012 at 6:08 PM, Luo Lei <Luo...@2degreesmobile.co.nz> wrote:
>
>
>
> I attached the data this time... I don’t seem to get anything out by running following code
>
>
>
> ggplot(x1.m, aes(x=Quarter,y=Customers)) +geom_line() +facet_wrap(~PROVIDER)
>
> with (x1.m,qplot(Quarter,Customers,facets=PROVIDER~.,data=x1.m,geom="line",main="Portouts"))
>
>
>
> I only got a blank screen?
>
>
>
>
>
>
>
>
>
>
>
> From: rom...@gmail.com [mailto:rom...@gmail.com] On Behalf Of Roman Luštrik
> Sent: Tuesday, 23 October 2012 8:37 p.m.
> To: Luo Lei
> Cc: Heike Hofmann; ggp...@googlegroups.com
> Subject: Re: date in qplot()
>
>
>
> I will encourage you for next time to provide some sample data. Until next time, here's a piece of code that works for me. Is this what you're after?
>
>
>
> mydf <- data.frame(
>
> provider = rep(letters[1:4], 20),
>
> quart = seq(from = as.Date("2011-09-01"), to = as.Date("2011-09-30"), length.out = 20),
>
> customers = runif(20) * 200)
>
> head(mydf)
>
>
>
> library(ggplot2)
>
> ggplot(mydf, aes(x = quart, y = customers)) + geom_line() + facet_wrap(~ provider)
>
>
>
>
>
> Cheers,
>
> Roman
>
>
>
>
>
>
>
> On Tue, Oct 23, 2012 at 5:20 AM, Luo Lei <Luo...@2degreesmobile.co.nz> wrote:
>
>
>
> Hi Heike
>
>
>
> Thanks for your email. I would like to plot a simple time serie by using qplot() or ggplot()
>
> Before I did plot the table, I have to replace the NA with 0? Is this necessary? Can qplot or ggplot recognise NAs?
>
>
>
> Here is the screen dump for the data in a melted format
>
>
>
> Then I will try to run “qplot(Quarter,Customers,facets=PROVIDER~.,data=x1.m,geom="line",main="Portouts")” Nothing happens??? I was hoping to see a line in each box.
>
> Could you tell me where I did wrong? Thanks
>
>
>
>
>
>
> Luo Lei
> Marketing Insight Analyst
> Two Degrees Mobile Limited
> ==========================
> (M) +64 27 361 9166
> (P) +64 9 919 7000
> www.2degreesmobile.co.nz
>
> Two Degrees Mobile Limited | 131 Khyber Pass Rd | Newmarket | Auckland | New Zealand |
> PO Box 8355 | Symonds Street | Auckland 1150 | New Zealand | Fax +64 9 919 7001
>
>
>
> Two Degrees Mobile Limited | 131 Khyber Pass Rd | Newmarket | Auckland | New Zealand |
> PO Box 8355 | Symonds Street | Auckland 1150 | New Zealand | Fax +64 9 919 7001
>
>
>

Luo Lei

unread,
Oct 23, 2012, 10:56:09 PM10/23/12
to Ben Bond-Lamberty, ggp...@googlegroups.com

 

Hi Ben

 

The result is below by running the your code. R still has recognised Quarter as a date variable.....It should be a line.

ggplot(x1.m, aes(x=Quarter,y=Customers,group=1)) +geom_line()

+facet_wrap(~PROVIDER)

 

 



Luo Lei

Marketing Insight Analyst
Two Degrees Mobile Limited
==========================
(M) +64 27 361 9166
(P)  +64 9 919 7000
www.2degreesmobile.co.nz


Two Degrees Mobile Limited | 131 Khyber Pass Rd | Newmarket | Auckland | New Zealand |
PO Box 8355 | Symonds Street | Auckland 1150 | New Zealand | Fax +64 9 919 7001


   


Disclaimer
The e-mail and any files transmitted with it are confidential and may contain privileged or copyright information. If you are not the intended recipient you must not copy, distribute, or use this e-mail or the information contained in it for any purpose other than to notify us of the error. If you have received this message in error, please notify the sender immediately, by email or phone (+64 9 919 7000) and delete this email from your system. Any views expressed in this message are those of the individual sender, except where the sender specifically states them to be the views of Two Degrees Mobile Limited. We do not guarantee that this material is free from viruses or any other defects although due care has been taken to minimize the risk





-----Original Message-----



From: ggp...@googlegroups.com [mailto:ggp...@googlegroups.com] On Behalf Of Ben Bond-Lamberty
Sent: Wednesday, 24 October 2012 1:04 p.m.
To: ggp...@googlegroups.com
Subject: Re: date in qplot()

 

You need

ggplot.Rdata

Ben Bond-Lamberty

unread,
Oct 24, 2012, 6:49:21 AM10/24/12
to Luo Lei, ggp...@googlegroups.com
It doesn't look like you've included the facet_wrap in the output
below...you should get a 2x2 grid of panels (Skinny Mobile, TNZ
Mobile, etc.), with a line in each.

Ben


On Tue, Oct 23, 2012 at 10:56 PM, Luo Lei <Luo...@2degreesmobile.co.nz> wrote:
>
>
>
> Hi Ben
>
>
>
> The result is below by running the your code. R still has recognised Quarter as a date variable.....It should be a line.
>
> ggplot(x1.m, aes(x=Quarter,y=Customers,group=1)) +geom_line()
>
> +facet_wrap(~PROVIDER)
>
>
>
>
>
>
>

Roman Luštrik

unread,
Oct 24, 2012, 8:17:50 AM10/24/12
to Ben Bond-Lamberty, Luo Lei, ggp...@googlegroups.com
Ben's suggestion works for me.

Inline image 1

Cheers,
Roman
ben_facet.jpg
Reply all
Reply to author
Forward
0 new messages