Installing package into...

classic Classic list List threaded Threaded
36 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Re: Plotting in R

Jim Lemon-4
Hi Steven,
A basic plot can be displayed like this:

sydf<-read.table(text="year rate
 1993 0.608
 1994 0.622
 1996 0.623
 1998 0.647
 2000 0.646
 2002 0.625
 2004 0.628
 2006 0.685
 2008 0.679
 2010 0.595
 2012 0.567
 2014 0.599
 2016 0.642
 2018 0.685",
header=TRUE)
plot(sydf$year,sydf$rate,type="b",
xlab="Year",ylab="Rate")

When you add more years and rates to the data frame, the axes will
change to include the years and rates outside the ones in your
example. As some have noted, this is a very basic question.

Jim

On Sat, Jul 6, 2019 at 11:33 PM Steven Yen <[hidden email]> wrote:

>
> I have a data frame containing two variables: year and rate (shown below).
> Which function can I use to plot rate (y-axis) against year (x-axis)?
> There will be more columns of rate later on.
> Thank you.
>
> year rate 1 1993 0.608 2 1994 0.622 3 1996 0.623 4 1998 0.647 5 2000
> 0.646 6 2002 0.625 7 2004 0.628 8 2006 0.685 9 2008 0.679 10 2010 0.595
> 11 2012 0.567 12 2014 0.599 13 2016 0.642 14 2018 0.685
>
>
> --
> [hidden email] (S.T. Yen)
>
>
>
> ---
> This email has been checked for viruses by Avast antivirus software.
> https://www.avast.com/antivirus
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Plotting in R

nstefi
Hi Jim,

Thanks for your email.
My question was: how to change the x axis labels without changing the chart.
Or is that not possible?
Using your example, I added another column:
sydf<-read.table(text="year1 month-day rate
 1993 05-01 0.608
 1994 06-01 0.622
 1996 07-01 0.623
 1998 08-01 0.647
 2000 09-01 0.646
 2002 10-01 0.625
 2004 11-01 0.628
 2006 12-01 0.685
 2008 01-01 0.679
 2010 02-01 0.595
 2012 03-01 0.567
 2014 04-01 0.599
 2016 05-01 0.642
 2018 06-01 0.685",
                 header=TRUE)

How can I show the column "month-day" as labels on the x axis, but still
have the plot showing the chart as rate based on year?
I tried this:
plot(sydf$year,sydf$rate,type="b",
     xlab="month-day",ylab="Rate")

but this only changes the title of the x axis to "month-day". I want the
values on the x axis to show 05-01  06-01, etc.
Is that possible?

Thanks,
Steven

-----Original Message-----
From: R-help <[hidden email]> On Behalf Of Jim Lemon
Sent: Sunday, July 7, 2019 2:59 AM
To: Steven Yen <[hidden email]>; r-help mailing list
<[hidden email]>
Subject: Re: [R] Plotting in R

Hi Steven,
A basic plot can be displayed like this:

sydf<-read.table(text="year rate
 1993 0.608
 1994 0.622
 1996 0.623
 1998 0.647
 2000 0.646
 2002 0.625
 2004 0.628
 2006 0.685
 2008 0.679
 2010 0.595
 2012 0.567
 2014 0.599
 2016 0.642
 2018 0.685",
header=TRUE)
plot(sydf$year,sydf$rate,type="b",
xlab="Year",ylab="Rate")

When you add more years and rates to the data frame, the axes will change to
include the years and rates outside the ones in your example. As some have
noted, this is a very basic question.

Jim

On Sat, Jul 6, 2019 at 11:33 PM Steven Yen <[hidden email]> wrote:

>
> I have a data frame containing two variables: year and rate (shown below).
> Which function can I use to plot rate (y-axis) against year (x-axis)?
> There will be more columns of rate later on.
> Thank you.
>
> year rate 1 1993 0.608 2 1994 0.622 3 1996 0.623 4 1998 0.647 5 2000
> 0.646 6 2002 0.625 7 2004 0.628 8 2006 0.685 9 2008 0.679 10 2010
> 0.595
> 11 2012 0.567 12 2014 0.599 13 2016 0.642 14 2018 0.685
>
>
> --
> [hidden email] (S.T. Yen)
>
>
>
> ---
> This email has been checked for viruses by Avast antivirus software.
> https://www.avast.com/antivirus
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Plotting in R

nstefi
Sorry Jim, I saw that you answered to the other Steven.
I had a question and nobody responded to that yet, I thought you responded
to me.
I searched for mine and your email came up, but I realize the subject line
is different. My question was:
"How to change x axes labels in plot_ly?"

-----Original Message-----
From: [hidden email] <[hidden email]>
Sent: Thursday, July 11, 2019 11:59 AM
To: 'Jim Lemon' <[hidden email]>; 'Steven Yen' <[hidden email]>;
'r-help mailing list' <[hidden email]>
Subject: RE: [R] Plotting in R

Hi Jim,

Thanks for your email.
My question was: how to change the x axis labels without changing the chart.
Or is that not possible?
Using your example, I added another column:
sydf<-read.table(text="year1 month-day rate
 1993 05-01 0.608
 1994 06-01 0.622
 1996 07-01 0.623
 1998 08-01 0.647
 2000 09-01 0.646
 2002 10-01 0.625
 2004 11-01 0.628
 2006 12-01 0.685
 2008 01-01 0.679
 2010 02-01 0.595
 2012 03-01 0.567
 2014 04-01 0.599
 2016 05-01 0.642
 2018 06-01 0.685",
                 header=TRUE)

How can I show the column "month-day" as labels on the x axis, but still
have the plot showing the chart as rate based on year?
I tried this:
plot(sydf$year,sydf$rate,type="b",
     xlab="month-day",ylab="Rate")

but this only changes the title of the x axis to "month-day". I want the
values on the x axis to show 05-01  06-01, etc.
Is that possible?

Thanks,
Steven

-----Original Message-----
From: R-help <[hidden email]> On Behalf Of Jim Lemon
Sent: Sunday, July 7, 2019 2:59 AM
To: Steven Yen <[hidden email]>; r-help mailing list
<[hidden email]>
Subject: Re: [R] Plotting in R

Hi Steven,
A basic plot can be displayed like this:

sydf<-read.table(text="year rate
 1993 0.608
 1994 0.622
 1996 0.623
 1998 0.647
 2000 0.646
 2002 0.625
 2004 0.628
 2006 0.685
 2008 0.679
 2010 0.595
 2012 0.567
 2014 0.599
 2016 0.642
 2018 0.685",
header=TRUE)
plot(sydf$year,sydf$rate,type="b",
xlab="Year",ylab="Rate")

When you add more years and rates to the data frame, the axes will change to
include the years and rates outside the ones in your example. As some have
noted, this is a very basic question.

Jim

On Sat, Jul 6, 2019 at 11:33 PM Steven Yen <[hidden email]> wrote:

>
> I have a data frame containing two variables: year and rate (shown below).
> Which function can I use to plot rate (y-axis) against year (x-axis)?
> There will be more columns of rate later on.
> Thank you.
>
> year rate 1 1993 0.608 2 1994 0.622 3 1996 0.623 4 1998 0.647 5 2000
> 0.646 6 2002 0.625 7 2004 0.628 8 2006 0.685 9 2008 0.679 10 2010
> 0.595
> 11 2012 0.567 12 2014 0.599 13 2016 0.642 14 2018 0.685
>
>
> --
> [hidden email] (S.T. Yen)
>
>
>
> ---
> This email has been checked for viruses by Avast antivirus software.
> https://www.avast.com/antivirus
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Plotting in R

David Carlson
In reply to this post by nstefi
There's a typo in the first column name of your data - "year1" should
be "year". Sometimes R will do partial matching and find it, but not
always. Is this closer to what you're looking for?

plot(rate~year, sydf, type="b", xaxt="n", xlab="Month-Day")
axis(1, sydf$year, sydf$month.day)

This will probably leave out some labels because there is not enough
room. You can print the labels smaller:

axis(1, sydf$year, sydf$month.day, cex.axis=.75)

or rotate them 90 degrees:

axis(1, sydf$year, sydf$month.day, las=2)'


David L Carlson
Department of Anthropology
Texas A&M University

On Thu, Jul 11, 2019 at 10:59 AM <[hidden email]> wrote:

>
> Hi Jim,
>
> Thanks for your email.
> My question was: how to change the x axis labels without changing the chart.
> Or is that not possible?
> Using your example, I added another column:
> sydf<-read.table(text="year1 month-day rate
>  1993 05-01 0.608
>  1994 06-01 0.622
>  1996 07-01 0.623
>  1998 08-01 0.647
>  2000 09-01 0.646
>  2002 10-01 0.625
>  2004 11-01 0.628
>  2006 12-01 0.685
>  2008 01-01 0.679
>  2010 02-01 0.595
>  2012 03-01 0.567
>  2014 04-01 0.599
>  2016 05-01 0.642
>  2018 06-01 0.685",
>                  header=TRUE)
>
> How can I show the column "month-day" as labels on the x axis, but still
> have the plot showing the chart as rate based on year?
> I tried this:
> plot(sydf$year,sydf$rate,type="b",
>      xlab="month-day",ylab="Rate")
>
> but this only changes the title of the x axis to "month-day". I want the
> values on the x axis to show 05-01  06-01, etc.
> Is that possible?
>
> Thanks,
> Steven
>
> -----Original Message-----
> From: R-help <[hidden email]> On Behalf Of Jim Lemon
> Sent: Sunday, July 7, 2019 2:59 AM
> To: Steven Yen <[hidden email]>; r-help mailing list
> <[hidden email]>
> Subject: Re: [R] Plotting in R
>
> Hi Steven,
> A basic plot can be displayed like this:
>
> sydf<-read.table(text="year rate
>  1993 0.608
>  1994 0.622
>  1996 0.623
>  1998 0.647
>  2000 0.646
>  2002 0.625
>  2004 0.628
>  2006 0.685
>  2008 0.679
>  2010 0.595
>  2012 0.567
>  2014 0.599
>  2016 0.642
>  2018 0.685",
> header=TRUE)
> plot(sydf$year,sydf$rate,type="b",
> xlab="Year",ylab="Rate")
>
> When you add more years and rates to the data frame, the axes will change to
> include the years and rates outside the ones in your example. As some have
> noted, this is a very basic question.
>
> Jim
>
> On Sat, Jul 6, 2019 at 11:33 PM Steven Yen <[hidden email]> wrote:
> >
> > I have a data frame containing two variables: year and rate (shown below).
> > Which function can I use to plot rate (y-axis) against year (x-axis)?
> > There will be more columns of rate later on.
> > Thank you.
> >
> > year rate 1 1993 0.608 2 1994 0.622 3 1996 0.623 4 1998 0.647 5 2000
> > 0.646 6 2002 0.625 7 2004 0.628 8 2006 0.685 9 2008 0.679 10 2010
> > 0.595
> > 11 2012 0.567 12 2014 0.599 13 2016 0.642 14 2018 0.685
> >
> >
> > --
> > [hidden email] (S.T. Yen)
> >
> >
> >
> > ---
> > This email has been checked for viruses by Avast antivirus software.
> > https://urldefense.proofpoint.com/v2/url?u=https-3A__www.avast.com_antivirus&d=DwICAg&c=u6LDEWzohnDQ01ySGnxMzg&r=VAaHUElasUXjP9TzIcfIrXdkDpHnJBBZ9Q1u5LcXz9s&m=23NXOUvOhO2PZnfjNak-qt7MYdttWJs3O634wqpLE4A&s=Psw17kUMNN42CxAcroDYQ-fU3UzzQlXgwSdzbvb23Lc&e=
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwICAg&c=u6LDEWzohnDQ01ySGnxMzg&r=VAaHUElasUXjP9TzIcfIrXdkDpHnJBBZ9Q1u5LcXz9s&m=23NXOUvOhO2PZnfjNak-qt7MYdttWJs3O634wqpLE4A&s=uruiNGrA6F82mRo7L3az2sIVhJv58RS7O3bC9JxKfKc&e=
> > PLEASE do read the posting guide
> > https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwICAg&c=u6LDEWzohnDQ01ySGnxMzg&r=VAaHUElasUXjP9TzIcfIrXdkDpHnJBBZ9Q1u5LcXz9s&m=23NXOUvOhO2PZnfjNak-qt7MYdttWJs3O634wqpLE4A&s=Ic4wt1QLwaqf-_Ncfy9aN_dp_fqkFLoZl4zwa2Xj-r4&e=
> > and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwICAg&c=u6LDEWzohnDQ01ySGnxMzg&r=VAaHUElasUXjP9TzIcfIrXdkDpHnJBBZ9Q1u5LcXz9s&m=23NXOUvOhO2PZnfjNak-qt7MYdttWJs3O634wqpLE4A&s=uruiNGrA6F82mRo7L3az2sIVhJv58RS7O3bC9JxKfKc&e=
> PLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwICAg&c=u6LDEWzohnDQ01ySGnxMzg&r=VAaHUElasUXjP9TzIcfIrXdkDpHnJBBZ9Q1u5LcXz9s&m=23NXOUvOhO2PZnfjNak-qt7MYdttWJs3O634wqpLE4A&s=Ic4wt1QLwaqf-_Ncfy9aN_dp_fqkFLoZl4zwa2Xj-r4&e=
> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwICAg&c=u6LDEWzohnDQ01ySGnxMzg&r=VAaHUElasUXjP9TzIcfIrXdkDpHnJBBZ9Q1u5LcXz9s&m=23NXOUvOhO2PZnfjNak-qt7MYdttWJs3O634wqpLE4A&s=uruiNGrA6F82mRo7L3az2sIVhJv58RS7O3bC9JxKfKc&e=
> PLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwICAg&c=u6LDEWzohnDQ01ySGnxMzg&r=VAaHUElasUXjP9TzIcfIrXdkDpHnJBBZ9Q1u5LcXz9s&m=23NXOUvOhO2PZnfjNak-qt7MYdttWJs3O634wqpLE4A&s=Ic4wt1QLwaqf-_Ncfy9aN_dp_fqkFLoZl4zwa2Xj-r4&e=
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Plotting in R

Bert Gunter-2
In reply to this post by nstefi
?axis
-- And note the examples!

*Please* go through one or more of the web tutorials on plotting in R. I
feel that it is unfair of you to ask for such basic tutorials here when
resources are already available to you (others may disagree, of course). If
you have questions *after* reading, then feel free to ask here.

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Thu, Jul 11, 2019 at 8:59 AM <[hidden email]> wrote:

> Hi Jim,
>
> Thanks for your email.
> My question was: how to change the x axis labels without changing the
> chart.
> Or is that not possible?
> Using your example, I added another column:
> sydf<-read.table(text="year1 month-day rate
>  1993 05-01 0.608
>  1994 06-01 0.622
>  1996 07-01 0.623
>  1998 08-01 0.647
>  2000 09-01 0.646
>  2002 10-01 0.625
>  2004 11-01 0.628
>  2006 12-01 0.685
>  2008 01-01 0.679
>  2010 02-01 0.595
>  2012 03-01 0.567
>  2014 04-01 0.599
>  2016 05-01 0.642
>  2018 06-01 0.685",
>                  header=TRUE)
>
> How can I show the column "month-day" as labels on the x axis, but still
> have the plot showing the chart as rate based on year?
> I tried this:
> plot(sydf$year,sydf$rate,type="b",
>      xlab="month-day",ylab="Rate")
>
> but this only changes the title of the x axis to "month-day". I want the
> values on the x axis to show 05-01  06-01, etc.
> Is that possible?
>
> Thanks,
> Steven
>
> -----Original Message-----
> From: R-help <[hidden email]> On Behalf Of Jim Lemon
> Sent: Sunday, July 7, 2019 2:59 AM
> To: Steven Yen <[hidden email]>; r-help mailing list
> <[hidden email]>
> Subject: Re: [R] Plotting in R
>
> Hi Steven,
> A basic plot can be displayed like this:
>
> sydf<-read.table(text="year rate
>  1993 0.608
>  1994 0.622
>  1996 0.623
>  1998 0.647
>  2000 0.646
>  2002 0.625
>  2004 0.628
>  2006 0.685
>  2008 0.679
>  2010 0.595
>  2012 0.567
>  2014 0.599
>  2016 0.642
>  2018 0.685",
> header=TRUE)
> plot(sydf$year,sydf$rate,type="b",
> xlab="Year",ylab="Rate")
>
> When you add more years and rates to the data frame, the axes will change
> to
> include the years and rates outside the ones in your example. As some have
> noted, this is a very basic question.
>
> Jim
>
> On Sat, Jul 6, 2019 at 11:33 PM Steven Yen <[hidden email]> wrote:
> >
> > I have a data frame containing two variables: year and rate (shown
> below).
> > Which function can I use to plot rate (y-axis) against year (x-axis)?
> > There will be more columns of rate later on.
> > Thank you.
> >
> > year rate 1 1993 0.608 2 1994 0.622 3 1996 0.623 4 1998 0.647 5 2000
> > 0.646 6 2002 0.625 7 2004 0.628 8 2006 0.685 9 2008 0.679 10 2010
> > 0.595
> > 11 2012 0.567 12 2014 0.599 13 2016 0.642 14 2018 0.685
> >
> >
> > --
> > [hidden email] (S.T. Yen)
> >
> >
> >
> > ---
> > This email has been checked for viruses by Avast antivirus software.
> > https://www.avast.com/antivirus
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > 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.
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Plotting in R

Jim Lemon-4
In reply to this post by nstefi
Hi Steven,
It is pretty easy, but there are one or two things to watch for.
First, don't use a hyphen in a field name unless you enclose it in
single quotes when extracting it. I've just removed the hyphen in this
example:

sydf<-read.table(text="year1 monthday rate
 1993 05-01 0.608
 1994 06-01 0.622
 1996 07-01 0.623
 1998 08-01 0.647
 2000 09-01 0.646
 2002 10-01 0.625
 2004 11-01 0.628
 2006 12-01 0.685
 2008 01-01 0.679
 2010 02-01 0.595
 2012 03-01 0.567
 2014 04-01 0.599
 2016 05-01 0.642
 2018 06-01 0.685",
 header=TRUE,
 stringsAsFactors=FALSE)
sydf$date<-as.Date(paste(sydf$year1,sydf$monthday),"%Y %m-%d")
library(plotrix)
par(mar=c(6,4,4,2))
plot(sydf$date,sydf$rate,type="b",xaxt="n")
staxlab(1,at=sydf$date,labels=sydf$monthday)

Note that I have also added the stringsAsFactors argument to prevent
monthdate being read as a factor. I expected that the axis labels
would be crowded so I used the plotrix library to stagger the x-axis
labels. Hope this solves your problem.

Jim

On Fri, Jul 12, 2019 at 1:59 AM <[hidden email]> wrote:

>
> Hi Jim,
>
> Thanks for your email.
> My question was: how to change the x axis labels without changing the chart.
> Or is that not possible?
> Using your example, I added another column:
> sydf<-read.table(text="year1 month-day rate
>  1993 05-01 0.608
>  1994 06-01 0.622
>  1996 07-01 0.623
>  1998 08-01 0.647
>  2000 09-01 0.646
>  2002 10-01 0.625
>  2004 11-01 0.628
>  2006 12-01 0.685
>  2008 01-01 0.679
>  2010 02-01 0.595
>  2012 03-01 0.567
>  2014 04-01 0.599
>  2016 05-01 0.642
>  2018 06-01 0.685",
>                  header=TRUE)
>
> How can I show the column "month-day" as labels on the x axis, but still
> have the plot showing the chart as rate based on year?
> I tried this:
> plot(sydf$year,sydf$rate,type="b",
>      xlab="month-day",ylab="Rate")
>
> but this only changes the title of the x axis to "month-day". I want the
> values on the x axis to show 05-01  06-01, etc.
> Is that possible?
>
> Thanks,
> Steven
>
> -----Original Message-----
> From: R-help <[hidden email]> On Behalf Of Jim Lemon
> Sent: Sunday, July 7, 2019 2:59 AM
> To: Steven Yen <[hidden email]>; r-help mailing list
> <[hidden email]>
> Subject: Re: [R] Plotting in R
>
> Hi Steven,
> A basic plot can be displayed like this:
>
> sydf<-read.table(text="year rate
>  1993 0.608
>  1994 0.622
>  1996 0.623
>  1998 0.647
>  2000 0.646
>  2002 0.625
>  2004 0.628
>  2006 0.685
>  2008 0.679
>  2010 0.595
>  2012 0.567
>  2014 0.599
>  2016 0.642
>  2018 0.685",
> header=TRUE)
> plot(sydf$year,sydf$rate,type="b",
> xlab="Year",ylab="Rate")
>
> When you add more years and rates to the data frame, the axes will change to
> include the years and rates outside the ones in your example. As some have
> noted, this is a very basic question.
>
> Jim
>
> On Sat, Jul 6, 2019 at 11:33 PM Steven Yen <[hidden email]> wrote:
> >
> > I have a data frame containing two variables: year and rate (shown below).
> > Which function can I use to plot rate (y-axis) against year (x-axis)?
> > There will be more columns of rate later on.
> > Thank you.
> >
> > year rate 1 1993 0.608 2 1994 0.622 3 1996 0.623 4 1998 0.647 5 2000
> > 0.646 6 2002 0.625 7 2004 0.628 8 2006 0.685 9 2008 0.679 10 2010
> > 0.595
> > 11 2012 0.567 12 2014 0.599 13 2016 0.642 14 2018 0.685
> >
> >
> > --
> > [hidden email] (S.T. Yen)
> >
> >
> >
> > ---
> > This email has been checked for viruses by Avast antivirus software.
> > https://www.avast.com/antivirus
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > 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.
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Plotting in R

nstefi
Thanks Jim, that worked.

> I expected that the axis labels would be crowded so I used the plotrix library to stagger the x-axis labels. Hope this solves your problem.
I liked how that showed, not overlapping on each other. I wasn't aware of the plotrix library.

In my code I was using plot_ly for visualization, because it looked nicer compared to the simple plot() function, and I have the chart in a Shiny dashboard (RMD file).
I found in the meantime today some code example for plot_ly and using the same data it looks like this:
sydf<-read.table(text="year1 monthday rate
 1993 05-01 0.608
 1994 06-01 0.622
 1996 07-01 0.623
 1998 08-01 0.647
 2000 09-01 0.646
 2002 10-01 0.625
 2004 11-01 0.628
 2006 12-01 0.685
 2008 01-01 0.679
 2010 02-01 0.595
 2012 03-01 0.567
 2014 04-01 0.599
 2016 05-01 0.642
 2018 06-01 0.685",
                 header=TRUE,
                 stringsAsFactors=FALSE)

library(plotly)
plot_ly(sydf,
        x = ~year1,
        y = ~rate,
        type = 'scatter', mode = 'lines') %>%
  layout(
    xaxis = list(
      ticktext = sydf$monthday,
      tickvals = sydf$year1,
      tickmode = "array",
      tickangle = 270
    ))

This is where I found about the parameters:
https://plot.ly/r/tick-formatting/

Now my other challenge is that with my data I have a lot more values on the x axis, and they overlap even when turned vertically. I guess there must be a way of taking a number of values evenly from the list of x axis lables, and use that for the "ticktext" parameter.
I thought it must be some variation of the seq(from, to, by= ). Can I use that with a list of strings?

Thanks,
Steven

-----Original Message-----
From: Jim Lemon <[hidden email]>
Sent: Thursday, July 11, 2019 7:46 PM
To: [hidden email]; r-help mailing list <[hidden email]>
Subject: Re: [R] Plotting in R

Hi Steven,
It is pretty easy, but there are one or two things to watch for.
First, don't use a hyphen in a field name unless you enclose it in single quotes when extracting it. I've just removed the hyphen in this
example:

sydf<-read.table(text="year1 monthday rate
 1993 05-01 0.608
 1994 06-01 0.622
 1996 07-01 0.623
 1998 08-01 0.647
 2000 09-01 0.646
 2002 10-01 0.625
 2004 11-01 0.628
 2006 12-01 0.685
 2008 01-01 0.679
 2010 02-01 0.595
 2012 03-01 0.567
 2014 04-01 0.599
 2016 05-01 0.642
 2018 06-01 0.685",
 header=TRUE,
 stringsAsFactors=FALSE)
sydf$date<-as.Date(paste(sydf$year1,sydf$monthday),"%Y %m-%d")
library(plotrix)
par(mar=c(6,4,4,2))
plot(sydf$date,sydf$rate,type="b",xaxt="n")
staxlab(1,at=sydf$date,labels=sydf$monthday)

Note that I have also added the stringsAsFactors argument to prevent monthdate being read as a factor. I expected that the axis labels would be crowded so I used the plotrix library to stagger the x-axis labels. Hope this solves your problem.

Jim

On Fri, Jul 12, 2019 at 1:59 AM <[hidden email]> wrote:

>
> Hi Jim,
>
> Thanks for your email.
> My question was: how to change the x axis labels without changing the chart.
> Or is that not possible?
> Using your example, I added another column:
> sydf<-read.table(text="year1 month-day rate
>  1993 05-01 0.608
>  1994 06-01 0.622
>  1996 07-01 0.623
>  1998 08-01 0.647
>  2000 09-01 0.646
>  2002 10-01 0.625
>  2004 11-01 0.628
>  2006 12-01 0.685
>  2008 01-01 0.679
>  2010 02-01 0.595
>  2012 03-01 0.567
>  2014 04-01 0.599
>  2016 05-01 0.642
>  2018 06-01 0.685",
>                  header=TRUE)
>
> How can I show the column "month-day" as labels on the x axis, but
> still have the plot showing the chart as rate based on year?
> I tried this:
> plot(sydf$year,sydf$rate,type="b",
>      xlab="month-day",ylab="Rate")
>
> but this only changes the title of the x axis to "month-day". I want
> the values on the x axis to show 05-01  06-01, etc.
> Is that possible?
>
> Thanks,
> Steven
>
> -----Original Message-----
> From: R-help <[hidden email]> On Behalf Of Jim Lemon
> Sent: Sunday, July 7, 2019 2:59 AM
> To: Steven Yen <[hidden email]>; r-help mailing list
> <[hidden email]>
> Subject: Re: [R] Plotting in R
>
> Hi Steven,
> A basic plot can be displayed like this:
>
> sydf<-read.table(text="year rate
>  1993 0.608
>  1994 0.622
>  1996 0.623
>  1998 0.647
>  2000 0.646
>  2002 0.625
>  2004 0.628
>  2006 0.685
>  2008 0.679
>  2010 0.595
>  2012 0.567
>  2014 0.599
>  2016 0.642
>  2018 0.685",
> header=TRUE)
> plot(sydf$year,sydf$rate,type="b",
> xlab="Year",ylab="Rate")
>
> When you add more years and rates to the data frame, the axes will
> change to include the years and rates outside the ones in your
> example. As some have noted, this is a very basic question.
>
> Jim
>
> On Sat, Jul 6, 2019 at 11:33 PM Steven Yen <[hidden email]> wrote:
> >
> > I have a data frame containing two variables: year and rate (shown below).
> > Which function can I use to plot rate (y-axis) against year (x-axis)?
> > There will be more columns of rate later on.
> > Thank you.
> >
> > year rate 1 1993 0.608 2 1994 0.622 3 1996 0.623 4 1998 0.647 5 2000
> > 0.646 6 2002 0.625 7 2004 0.628 8 2006 0.685 9 2008 0.679 10 2010
> > 0.595
> > 11 2012 0.567 12 2014 0.599 13 2016 0.642 14 2018 0.685
> >
> >
> > --
> > [hidden email] (S.T. Yen)
> >
> >
> >
> > ---
> > This email has been checked for viruses by Avast antivirus software.
> > https://www.avast.com/antivirus
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > 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.
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Plotting in R

Jim Lemon-4
Hi Steven,
Neat solution. With a lot more values on the time axis, you will be
better off with something like:

yrticks<-as.Date(as.character(seq(min(sydf$year1),max(sydf$year1),by=2)),
 "%Y")
axis(1,at=yrticks,labels=format(yrticks,"%Y"))

You probably won't need staxlab for that.

Jim

On Fri, Jul 12, 2019 at 11:55 AM <[hidden email]> wrote:

>
> Thanks Jim, that worked.
>
> > I expected that the axis labels would be crowded so I used the plotrix library to stagger the x-axis labels. Hope this solves your problem.
> I liked how that showed, not overlapping on each other. I wasn't aware of the plotrix library.
>
> In my code I was using plot_ly for visualization, because it looked nicer compared to the simple plot() function, and I have the chart in a Shiny dashboard (RMD file).
> I found in the meantime today some code example for plot_ly and using the same data it looks like this:
> sydf<-read.table(text="year1 monthday rate
>  1993 05-01 0.608
>  1994 06-01 0.622
>  1996 07-01 0.623
>  1998 08-01 0.647
>  2000 09-01 0.646
>  2002 10-01 0.625
>  2004 11-01 0.628
>  2006 12-01 0.685
>  2008 01-01 0.679
>  2010 02-01 0.595
>  2012 03-01 0.567
>  2014 04-01 0.599
>  2016 05-01 0.642
>  2018 06-01 0.685",
>                  header=TRUE,
>                  stringsAsFactors=FALSE)
>
> library(plotly)
> plot_ly(sydf,
>         x = ~year1,
>         y = ~rate,
>         type = 'scatter', mode = 'lines') %>%
>   layout(
>     xaxis = list(
>       ticktext = sydf$monthday,
>       tickvals = sydf$year1,
>       tickmode = "array",
>       tickangle = 270
>     ))
>
> This is where I found about the parameters:
> https://plot.ly/r/tick-formatting/
>
> Now my other challenge is that with my data I have a lot more values on the x axis, and they overlap even when turned vertically. I guess there must be a way of taking a number of values evenly from the list of x axis lables, and use that for the "ticktext" parameter.
> I thought it must be some variation of the seq(from, to, by= ). Can I use that with a list of strings?
>
> Thanks,
> Steven
>
> -----Original Message-----
> From: Jim Lemon <[hidden email]>
> Sent: Thursday, July 11, 2019 7:46 PM
> To: [hidden email]; r-help mailing list <[hidden email]>
> Subject: Re: [R] Plotting in R
>
> Hi Steven,
> It is pretty easy, but there are one or two things to watch for.
> First, don't use a hyphen in a field name unless you enclose it in single quotes when extracting it. I've just removed the hyphen in this
> example:
>
> sydf<-read.table(text="year1 monthday rate
>  1993 05-01 0.608
>  1994 06-01 0.622
>  1996 07-01 0.623
>  1998 08-01 0.647
>  2000 09-01 0.646
>  2002 10-01 0.625
>  2004 11-01 0.628
>  2006 12-01 0.685
>  2008 01-01 0.679
>  2010 02-01 0.595
>  2012 03-01 0.567
>  2014 04-01 0.599
>  2016 05-01 0.642
>  2018 06-01 0.685",
>  header=TRUE,
>  stringsAsFactors=FALSE)
> sydf$date<-as.Date(paste(sydf$year1,sydf$monthday),"%Y %m-%d")
> library(plotrix)
> par(mar=c(6,4,4,2))
> plot(sydf$date,sydf$rate,type="b",xaxt="n")
> staxlab(1,at=sydf$date,labels=sydf$monthday)
>
> Note that I have also added the stringsAsFactors argument to prevent monthdate being read as a factor. I expected that the axis labels would be crowded so I used the plotrix library to stagger the x-axis labels. Hope this solves your problem.
>
> Jim
>
> On Fri, Jul 12, 2019 at 1:59 AM <[hidden email]> wrote:
> >
> > Hi Jim,
> >
> > Thanks for your email.
> > My question was: how to change the x axis labels without changing the chart.
> > Or is that not possible?
> > Using your example, I added another column:
> > sydf<-read.table(text="year1 month-day rate
> >  1993 05-01 0.608
> >  1994 06-01 0.622
> >  1996 07-01 0.623
> >  1998 08-01 0.647
> >  2000 09-01 0.646
> >  2002 10-01 0.625
> >  2004 11-01 0.628
> >  2006 12-01 0.685
> >  2008 01-01 0.679
> >  2010 02-01 0.595
> >  2012 03-01 0.567
> >  2014 04-01 0.599
> >  2016 05-01 0.642
> >  2018 06-01 0.685",
> >                  header=TRUE)
> >
> > How can I show the column "month-day" as labels on the x axis, but
> > still have the plot showing the chart as rate based on year?
> > I tried this:
> > plot(sydf$year,sydf$rate,type="b",
> >      xlab="month-day",ylab="Rate")
> >
> > but this only changes the title of the x axis to "month-day". I want
> > the values on the x axis to show 05-01  06-01, etc.
> > Is that possible?
> >
> > Thanks,
> > Steven
> >
> > -----Original Message-----
> > From: R-help <[hidden email]> On Behalf Of Jim Lemon
> > Sent: Sunday, July 7, 2019 2:59 AM
> > To: Steven Yen <[hidden email]>; r-help mailing list
> > <[hidden email]>
> > Subject: Re: [R] Plotting in R
> >
> > Hi Steven,
> > A basic plot can be displayed like this:
> >
> > sydf<-read.table(text="year rate
> >  1993 0.608
> >  1994 0.622
> >  1996 0.623
> >  1998 0.647
> >  2000 0.646
> >  2002 0.625
> >  2004 0.628
> >  2006 0.685
> >  2008 0.679
> >  2010 0.595
> >  2012 0.567
> >  2014 0.599
> >  2016 0.642
> >  2018 0.685",
> > header=TRUE)
> > plot(sydf$year,sydf$rate,type="b",
> > xlab="Year",ylab="Rate")
> >
> > When you add more years and rates to the data frame, the axes will
> > change to include the years and rates outside the ones in your
> > example. As some have noted, this is a very basic question.
> >
> > Jim
> >
> > On Sat, Jul 6, 2019 at 11:33 PM Steven Yen <[hidden email]> wrote:
> > >
> > > I have a data frame containing two variables: year and rate (shown below).
> > > Which function can I use to plot rate (y-axis) against year (x-axis)?
> > > There will be more columns of rate later on.
> > > Thank you.
> > >
> > > year rate 1 1993 0.608 2 1994 0.622 3 1996 0.623 4 1998 0.647 5 2000
> > > 0.646 6 2002 0.625 7 2004 0.628 8 2006 0.685 9 2008 0.679 10 2010
> > > 0.595
> > > 11 2012 0.567 12 2014 0.599 13 2016 0.642 14 2018 0.685
> > >
> > >
> > > --
> > > [hidden email] (S.T. Yen)
> > >
> > >
> > >
> > > ---
> > > This email has been checked for viruses by Avast antivirus software.
> > > https://www.avast.com/antivirus
> > >
> > >         [[alternative HTML version deleted]]
> > >
> > > ______________________________________________
> > > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > > 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.
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > 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.
> >
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Plotting in R

nstefi
Thanks Jim.

I am trying to apply this to my version with plot_ly, and couldn't make it to work.
The sydf$year1 field is numeric, so the min() and max() works, but when I tried to use your formula for the sydf$monthday field I get an error:
yrticks <- as.Date(as.character(seq(min(sydf$monthday),max(sydf$monthday),by=2)),
                  "%Y")

Error in seq.default(min(sydf$monthday), max(sydf$monthday), by = 2) :
  'from' must be a finite number
In addition: Warning message:
In seq.default(min(sydf$monthday), max(sydf$monthday), by = 2) :
  NAs introduced by coercion

So I thought maybe you need to sample the tickvals not the ticktext and it would show the corresponding tick labels in the right palaces. Then I tried the way you had like this:
sydf<-read.table(text="year1 monthday rate
 1993 05-01 0.608
 1994 06-01 0.622
 1996 07-01 0.623
 1998 08-01 0.647
 2000 09-01 0.646
 2002 10-01 0.625
 2004 11-01 0.628
 2006 12-01 0.685
 2008 01-01 0.679
 2010 02-01 0.595
 2012 03-01 0.567
 2014 04-01 0.599
 2016 05-01 0.642
 2018 06-01 0.685",
                 header=TRUE,
                 stringsAsFactors=FALSE)

yrticks <- as.Date(as.character(seq(min(sydf$year1),max(sydf$year1),by=2)),
                  "%Y")

library(plotly)
plot_ly(sydf,
        x = ~year1,
        y = ~rate,
        type = 'scatter', mode = 'lines') %>%
  layout(
    xaxis = list(
      ticktext = sydf$monthday,
      tickvals = sydf$yrticks,
      tickmode = "array",
      tickangle = 270
    ))

But the chart didn't show any tick labels.
I guess I need to sample sydf$monthday, right? Because that's what I want to show as tick labels. But the problem is that monthday is string, and can't use a value for "by=", maybe I need to sample somehow by the index position.

Thanks,
Steven

-----Original Message-----
From: Jim Lemon <[hidden email]>
Sent: Thursday, July 11, 2019 10:41 PM
To: [hidden email]
Cc: r-help mailing list <[hidden email]>
Subject: Re: [R] Plotting in R

Hi Steven,
Neat solution. With a lot more values on the time axis, you will be better off with something like:

yrticks<-as.Date(as.character(seq(min(sydf$year1),max(sydf$year1),by=2)),
 "%Y")
axis(1,at=yrticks,labels=format(yrticks,"%Y"))

You probably won't need staxlab for that.

Jim

On Fri, Jul 12, 2019 at 11:55 AM <[hidden email]> wrote:

>
> Thanks Jim, that worked.
>
> > I expected that the axis labels would be crowded so I used the plotrix library to stagger the x-axis labels. Hope this solves your problem.
> I liked how that showed, not overlapping on each other. I wasn't aware of the plotrix library.
>
> In my code I was using plot_ly for visualization, because it looked nicer compared to the simple plot() function, and I have the chart in a Shiny dashboard (RMD file).
> I found in the meantime today some code example for plot_ly and using the same data it looks like this:
> sydf<-read.table(text="year1 monthday rate
>  1993 05-01 0.608
>  1994 06-01 0.622
>  1996 07-01 0.623
>  1998 08-01 0.647
>  2000 09-01 0.646
>  2002 10-01 0.625
>  2004 11-01 0.628
>  2006 12-01 0.685
>  2008 01-01 0.679
>  2010 02-01 0.595
>  2012 03-01 0.567
>  2014 04-01 0.599
>  2016 05-01 0.642
>  2018 06-01 0.685",
>                  header=TRUE,
>                  stringsAsFactors=FALSE)
>
> library(plotly)
> plot_ly(sydf,
>         x = ~year1,
>         y = ~rate,
>         type = 'scatter', mode = 'lines') %>%
>   layout(
>     xaxis = list(
>       ticktext = sydf$monthday,
>       tickvals = sydf$year1,
>       tickmode = "array",
>       tickangle = 270
>     ))
>
> This is where I found about the parameters:
> https://plot.ly/r/tick-formatting/
>
> Now my other challenge is that with my data I have a lot more values on the x axis, and they overlap even when turned vertically. I guess there must be a way of taking a number of values evenly from the list of x axis lables, and use that for the "ticktext" parameter.
> I thought it must be some variation of the seq(from, to, by= ). Can I use that with a list of strings?
>
> Thanks,
> Steven
>
> -----Original Message-----
> From: Jim Lemon <[hidden email]>
> Sent: Thursday, July 11, 2019 7:46 PM
> To: [hidden email]; r-help mailing list <[hidden email]>
> Subject: Re: [R] Plotting in R
>
> Hi Steven,
> It is pretty easy, but there are one or two things to watch for.
> First, don't use a hyphen in a field name unless you enclose it in
> single quotes when extracting it. I've just removed the hyphen in this
> example:
>
> sydf<-read.table(text="year1 monthday rate
>  1993 05-01 0.608
>  1994 06-01 0.622
>  1996 07-01 0.623
>  1998 08-01 0.647
>  2000 09-01 0.646
>  2002 10-01 0.625
>  2004 11-01 0.628
>  2006 12-01 0.685
>  2008 01-01 0.679
>  2010 02-01 0.595
>  2012 03-01 0.567
>  2014 04-01 0.599
>  2016 05-01 0.642
>  2018 06-01 0.685",
>  header=TRUE,
>  stringsAsFactors=FALSE)
> sydf$date<-as.Date(paste(sydf$year1,sydf$monthday),"%Y %m-%d")
> library(plotrix)
> par(mar=c(6,4,4,2))
> plot(sydf$date,sydf$rate,type="b",xaxt="n")
> staxlab(1,at=sydf$date,labels=sydf$monthday)
>
> Note that I have also added the stringsAsFactors argument to prevent monthdate being read as a factor. I expected that the axis labels would be crowded so I used the plotrix library to stagger the x-axis labels. Hope this solves your problem.
>
> Jim
>
> On Fri, Jul 12, 2019 at 1:59 AM <[hidden email]> wrote:
> >
> > Hi Jim,
> >
> > Thanks for your email.
> > My question was: how to change the x axis labels without changing the chart.
> > Or is that not possible?
> > Using your example, I added another column:
> > sydf<-read.table(text="year1 month-day rate
> >  1993 05-01 0.608
> >  1994 06-01 0.622
> >  1996 07-01 0.623
> >  1998 08-01 0.647
> >  2000 09-01 0.646
> >  2002 10-01 0.625
> >  2004 11-01 0.628
> >  2006 12-01 0.685
> >  2008 01-01 0.679
> >  2010 02-01 0.595
> >  2012 03-01 0.567
> >  2014 04-01 0.599
> >  2016 05-01 0.642
> >  2018 06-01 0.685",
> >                  header=TRUE)
> >
> > How can I show the column "month-day" as labels on the x axis, but
> > still have the plot showing the chart as rate based on year?
> > I tried this:
> > plot(sydf$year,sydf$rate,type="b",
> >      xlab="month-day",ylab="Rate")
> >
> > but this only changes the title of the x axis to "month-day". I want
> > the values on the x axis to show 05-01  06-01, etc.
> > Is that possible?
> >
> > Thanks,
> > Steven
> >
> > -----Original Message-----
> > From: R-help <[hidden email]> On Behalf Of Jim Lemon
> > Sent: Sunday, July 7, 2019 2:59 AM
> > To: Steven Yen <[hidden email]>; r-help mailing list
> > <[hidden email]>
> > Subject: Re: [R] Plotting in R
> >
> > Hi Steven,
> > A basic plot can be displayed like this:
> >
> > sydf<-read.table(text="year rate
> >  1993 0.608
> >  1994 0.622
> >  1996 0.623
> >  1998 0.647
> >  2000 0.646
> >  2002 0.625
> >  2004 0.628
> >  2006 0.685
> >  2008 0.679
> >  2010 0.595
> >  2012 0.567
> >  2014 0.599
> >  2016 0.642
> >  2018 0.685",
> > header=TRUE)
> > plot(sydf$year,sydf$rate,type="b",
> > xlab="Year",ylab="Rate")
> >
> > When you add more years and rates to the data frame, the axes will
> > change to include the years and rates outside the ones in your
> > example. As some have noted, this is a very basic question.
> >
> > Jim
> >
> > On Sat, Jul 6, 2019 at 11:33 PM Steven Yen <[hidden email]> wrote:
> > >
> > > I have a data frame containing two variables: year and rate (shown below).
> > > Which function can I use to plot rate (y-axis) against year (x-axis)?
> > > There will be more columns of rate later on.
> > > Thank you.
> > >
> > > year rate 1 1993 0.608 2 1994 0.622 3 1996 0.623 4 1998 0.647 5
> > > 2000
> > > 0.646 6 2002 0.625 7 2004 0.628 8 2006 0.685 9 2008 0.679 10 2010
> > > 0.595
> > > 11 2012 0.567 12 2014 0.599 13 2016 0.642 14 2018 0.685
> > >
> > >
> > > --
> > > [hidden email] (S.T. Yen)
> > >
> > >
> > >
> > > ---
> > > This email has been checked for viruses by Avast antivirus software.
> > > https://www.avast.com/antivirus
> > >
> > >         [[alternative HTML version deleted]]
> > >
> > > ______________________________________________
> > > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > > 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.
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > 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.
> >
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Plotting in R

Jim Lemon-4
Hi Steven,
year1 is a number (e.g. 1993), monthday (e.g. 05-01) is not.

Jim

On Fri, Jul 12, 2019 at 10:56 PM <[hidden email]> wrote:

>
> Thanks Jim.
>
> I am trying to apply this to my version with plot_ly, and couldn't make it to work.
> The sydf$year1 field is numeric, so the min() and max() works, but when I tried to use your formula for the sydf$monthday field I get an error:
> yrticks <- as.Date(as.character(seq(min(sydf$monthday),max(sydf$monthday),by=2)),
>                   "%Y")
>
> Error in seq.default(min(sydf$monthday), max(sydf$monthday), by = 2) :
>   'from' must be a finite number
> In addition: Warning message:
> In seq.default(min(sydf$monthday), max(sydf$monthday), by = 2) :
>   NAs introduced by coercion
>
> So I thought maybe you need to sample the tickvals not the ticktext and it would show the corresponding tick labels in the right palaces. Then I tried the way you had like this:
> sydf<-read.table(text="year1 monthday rate
>  1993 05-01 0.608
>  1994 06-01 0.622
>  1996 07-01 0.623
>  1998 08-01 0.647
>  2000 09-01 0.646
>  2002 10-01 0.625
>  2004 11-01 0.628
>  2006 12-01 0.685
>  2008 01-01 0.679
>  2010 02-01 0.595
>  2012 03-01 0.567
>  2014 04-01 0.599
>  2016 05-01 0.642
>  2018 06-01 0.685",
>                  header=TRUE,
>                  stringsAsFactors=FALSE)
>
> yrticks <- as.Date(as.character(seq(min(sydf$year1),max(sydf$year1),by=2)),
>                   "%Y")
>
> library(plotly)
> plot_ly(sydf,
>         x = ~year1,
>         y = ~rate,
>         type = 'scatter', mode = 'lines') %>%
>   layout(
>     xaxis = list(
>       ticktext = sydf$monthday,
>       tickvals = sydf$yrticks,
>       tickmode = "array",
>       tickangle = 270
>     ))
>
> But the chart didn't show any tick labels.
> I guess I need to sample sydf$monthday, right? Because that's what I want to show as tick labels. But the problem is that monthday is string, and can't use a value for "by=", maybe I need to sample somehow by the index position.
>
> Thanks,
> Steven
>
> -----Original Message-----
> From: Jim Lemon <[hidden email]>
> Sent: Thursday, July 11, 2019 10:41 PM
> To: [hidden email]
> Cc: r-help mailing list <[hidden email]>
> Subject: Re: [R] Plotting in R
>
> Hi Steven,
> Neat solution. With a lot more values on the time axis, you will be better off with something like:
>
> yrticks<-as.Date(as.character(seq(min(sydf$year1),max(sydf$year1),by=2)),
>  "%Y")
> axis(1,at=yrticks,labels=format(yrticks,"%Y"))
>
> You probably won't need staxlab for that.
>
> Jim
>
> On Fri, Jul 12, 2019 at 11:55 AM <[hidden email]> wrote:
> >
> > Thanks Jim, that worked.
> >
> > > I expected that the axis labels would be crowded so I used the plotrix library to stagger the x-axis labels. Hope this solves your problem.
> > I liked how that showed, not overlapping on each other. I wasn't aware of the plotrix library.
> >
> > In my code I was using plot_ly for visualization, because it looked nicer compared to the simple plot() function, and I have the chart in a Shiny dashboard (RMD file).
> > I found in the meantime today some code example for plot_ly and using the same data it looks like this:
> > sydf<-read.table(text="year1 monthday rate
> >  1993 05-01 0.608
> >  1994 06-01 0.622
> >  1996 07-01 0.623
> >  1998 08-01 0.647
> >  2000 09-01 0.646
> >  2002 10-01 0.625
> >  2004 11-01 0.628
> >  2006 12-01 0.685
> >  2008 01-01 0.679
> >  2010 02-01 0.595
> >  2012 03-01 0.567
> >  2014 04-01 0.599
> >  2016 05-01 0.642
> >  2018 06-01 0.685",
> >                  header=TRUE,
> >                  stringsAsFactors=FALSE)
> >
> > library(plotly)
> > plot_ly(sydf,
> >         x = ~year1,
> >         y = ~rate,
> >         type = 'scatter', mode = 'lines') %>%
> >   layout(
> >     xaxis = list(
> >       ticktext = sydf$monthday,
> >       tickvals = sydf$year1,
> >       tickmode = "array",
> >       tickangle = 270
> >     ))
> >
> > This is where I found about the parameters:
> > https://plot.ly/r/tick-formatting/
> >
> > Now my other challenge is that with my data I have a lot more values on the x axis, and they overlap even when turned vertically. I guess there must be a way of taking a number of values evenly from the list of x axis lables, and use that for the "ticktext" parameter.
> > I thought it must be some variation of the seq(from, to, by= ). Can I use that with a list of strings?
> >
> > Thanks,
> > Steven
> >
> > -----Original Message-----
> > From: Jim Lemon <[hidden email]>
> > Sent: Thursday, July 11, 2019 7:46 PM
> > To: [hidden email]; r-help mailing list <[hidden email]>
> > Subject: Re: [R] Plotting in R
> >
> > Hi Steven,
> > It is pretty easy, but there are one or two things to watch for.
> > First, don't use a hyphen in a field name unless you enclose it in
> > single quotes when extracting it. I've just removed the hyphen in this
> > example:
> >
> > sydf<-read.table(text="year1 monthday rate
> >  1993 05-01 0.608
> >  1994 06-01 0.622
> >  1996 07-01 0.623
> >  1998 08-01 0.647
> >  2000 09-01 0.646
> >  2002 10-01 0.625
> >  2004 11-01 0.628
> >  2006 12-01 0.685
> >  2008 01-01 0.679
> >  2010 02-01 0.595
> >  2012 03-01 0.567
> >  2014 04-01 0.599
> >  2016 05-01 0.642
> >  2018 06-01 0.685",
> >  header=TRUE,
> >  stringsAsFactors=FALSE)
> > sydf$date<-as.Date(paste(sydf$year1,sydf$monthday),"%Y %m-%d")
> > library(plotrix)
> > par(mar=c(6,4,4,2))
> > plot(sydf$date,sydf$rate,type="b",xaxt="n")
> > staxlab(1,at=sydf$date,labels=sydf$monthday)
> >
> > Note that I have also added the stringsAsFactors argument to prevent monthdate being read as a factor. I expected that the axis labels would be crowded so I used the plotrix library to stagger the x-axis labels. Hope this solves your problem.
> >
> > Jim
> >
> > On Fri, Jul 12, 2019 at 1:59 AM <[hidden email]> wrote:
> > >
> > > Hi Jim,
> > >
> > > Thanks for your email.
> > > My question was: how to change the x axis labels without changing the chart.
> > > Or is that not possible?
> > > Using your example, I added another column:
> > > sydf<-read.table(text="year1 month-day rate
> > >  1993 05-01 0.608
> > >  1994 06-01 0.622
> > >  1996 07-01 0.623
> > >  1998 08-01 0.647
> > >  2000 09-01 0.646
> > >  2002 10-01 0.625
> > >  2004 11-01 0.628
> > >  2006 12-01 0.685
> > >  2008 01-01 0.679
> > >  2010 02-01 0.595
> > >  2012 03-01 0.567
> > >  2014 04-01 0.599
> > >  2016 05-01 0.642
> > >  2018 06-01 0.685",
> > >                  header=TRUE)
> > >
> > > How can I show the column "month-day" as labels on the x axis, but
> > > still have the plot showing the chart as rate based on year?
> > > I tried this:
> > > plot(sydf$year,sydf$rate,type="b",
> > >      xlab="month-day",ylab="Rate")
> > >
> > > but this only changes the title of the x axis to "month-day". I want
> > > the values on the x axis to show 05-01  06-01, etc.
> > > Is that possible?
> > >
> > > Thanks,
> > > Steven
> > >
> > > -----Original Message-----
> > > From: R-help <[hidden email]> On Behalf Of Jim Lemon
> > > Sent: Sunday, July 7, 2019 2:59 AM
> > > To: Steven Yen <[hidden email]>; r-help mailing list
> > > <[hidden email]>
> > > Subject: Re: [R] Plotting in R
> > >
> > > Hi Steven,
> > > A basic plot can be displayed like this:
> > >
> > > sydf<-read.table(text="year rate
> > >  1993 0.608
> > >  1994 0.622
> > >  1996 0.623
> > >  1998 0.647
> > >  2000 0.646
> > >  2002 0.625
> > >  2004 0.628
> > >  2006 0.685
> > >  2008 0.679
> > >  2010 0.595
> > >  2012 0.567
> > >  2014 0.599
> > >  2016 0.642
> > >  2018 0.685",
> > > header=TRUE)
> > > plot(sydf$year,sydf$rate,type="b",
> > > xlab="Year",ylab="Rate")
> > >
> > > When you add more years and rates to the data frame, the axes will
> > > change to include the years and rates outside the ones in your
> > > example. As some have noted, this is a very basic question.
> > >
> > > Jim
> > >
> > > On Sat, Jul 6, 2019 at 11:33 PM Steven Yen <[hidden email]> wrote:
> > > >
> > > > I have a data frame containing two variables: year and rate (shown below).
> > > > Which function can I use to plot rate (y-axis) against year (x-axis)?
> > > > There will be more columns of rate later on.
> > > > Thank you.
> > > >
> > > > year rate 1 1993 0.608 2 1994 0.622 3 1996 0.623 4 1998 0.647 5
> > > > 2000
> > > > 0.646 6 2002 0.625 7 2004 0.628 8 2006 0.685 9 2008 0.679 10 2010
> > > > 0.595
> > > > 11 2012 0.567 12 2014 0.599 13 2016 0.642 14 2018 0.685
> > > >
> > > >
> > > > --
> > > > [hidden email] (S.T. Yen)
> > > >
> > > >
> > > >
> > > > ---
> > > > This email has been checked for viruses by Avast antivirus software.
> > > > https://www.avast.com/antivirus
> > > >
> > > >         [[alternative HTML version deleted]]
> > > >
> > > > ______________________________________________
> > > > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > > > 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.
> > >
> > > ______________________________________________
> > > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > > 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.
> > >
> >
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Plotting in R

Jim Lemon-4
Oh, sorry, I think I see what you have tried to do. You want yearly
ticks but month-day labels. These won't mean much unless you also have
the year. If you ask for a date with just the year, as.Date will give
you a date in the middle of that year:

as.Date("2002","%Y")
[1] "2002-07-13"

So making the labels these mid-year dates as character strings might
do what you want:

yrlabels<-as.character(yrticks)

Jim

On Sat, Jul 13, 2019 at 8:33 AM Jim Lemon <[hidden email]> wrote:

>
> Hi Steven,
> year1 is a number (e.g. 1993), monthday (e.g. 05-01) is not.
>
> Jim
>
> On Fri, Jul 12, 2019 at 10:56 PM <[hidden email]> wrote:
> >
> > Thanks Jim.
> >
> > I am trying to apply this to my version with plot_ly, and couldn't make it to work.
> > The sydf$year1 field is numeric, so the min() and max() works, but when I tried to use your formula for the sydf$monthday field I get an error:
> > yrticks <- as.Date(as.character(seq(min(sydf$monthday),max(sydf$monthday),by=2)),
> >                   "%Y")
> >
> > Error in seq.default(min(sydf$monthday), max(sydf$monthday), by = 2) :
> >   'from' must be a finite number
> > In addition: Warning message:
> > In seq.default(min(sydf$monthday), max(sydf$monthday), by = 2) :
> >   NAs introduced by coercion
> >
> > So I thought maybe you need to sample the tickvals not the ticktext and it would show the corresponding tick labels in the right palaces. Then I tried the way you had like this:
> > sydf<-read.table(text="year1 monthday rate
> >  1993 05-01 0.608
> >  1994 06-01 0.622
> >  1996 07-01 0.623
> >  1998 08-01 0.647
> >  2000 09-01 0.646
> >  2002 10-01 0.625
> >  2004 11-01 0.628
> >  2006 12-01 0.685
> >  2008 01-01 0.679
> >  2010 02-01 0.595
> >  2012 03-01 0.567
> >  2014 04-01 0.599
> >  2016 05-01 0.642
> >  2018 06-01 0.685",
> >                  header=TRUE,
> >                  stringsAsFactors=FALSE)
> >
> > yrticks <- as.Date(as.character(seq(min(sydf$year1),max(sydf$year1),by=2)),
> >                   "%Y")
> >
> > library(plotly)
> > plot_ly(sydf,
> >         x = ~year1,
> >         y = ~rate,
> >         type = 'scatter', mode = 'lines') %>%
> >   layout(
> >     xaxis = list(
> >       ticktext = sydf$monthday,
> >       tickvals = sydf$yrticks,
> >       tickmode = "array",
> >       tickangle = 270
> >     ))
> >
> > But the chart didn't show any tick labels.
> > I guess I need to sample sydf$monthday, right? Because that's what I want to show as tick labels. But the problem is that monthday is string, and can't use a value for "by=", maybe I need to sample somehow by the index position.
> >
> > Thanks,
> > Steven
> >
> > -----Original Message-----
> > From: Jim Lemon <[hidden email]>
> > Sent: Thursday, July 11, 2019 10:41 PM
> > To: [hidden email]
> > Cc: r-help mailing list <[hidden email]>
> > Subject: Re: [R] Plotting in R
> >
> > Hi Steven,
> > Neat solution. With a lot more values on the time axis, you will be better off with something like:
> >
> > yrticks<-as.Date(as.character(seq(min(sydf$year1),max(sydf$year1),by=2)),
> >  "%Y")
> > axis(1,at=yrticks,labels=format(yrticks,"%Y"))
> >
> > You probably won't need staxlab for that.
> >
> > Jim
> >
> > On Fri, Jul 12, 2019 at 11:55 AM <[hidden email]> wrote:
> > >
> > > Thanks Jim, that worked.
> > >
> > > > I expected that the axis labels would be crowded so I used the plotrix library to stagger the x-axis labels. Hope this solves your problem.
> > > I liked how that showed, not overlapping on each other. I wasn't aware of the plotrix library.
> > >
> > > In my code I was using plot_ly for visualization, because it looked nicer compared to the simple plot() function, and I have the chart in a Shiny dashboard (RMD file).
> > > I found in the meantime today some code example for plot_ly and using the same data it looks like this:
> > > sydf<-read.table(text="year1 monthday rate
> > >  1993 05-01 0.608
> > >  1994 06-01 0.622
> > >  1996 07-01 0.623
> > >  1998 08-01 0.647
> > >  2000 09-01 0.646
> > >  2002 10-01 0.625
> > >  2004 11-01 0.628
> > >  2006 12-01 0.685
> > >  2008 01-01 0.679
> > >  2010 02-01 0.595
> > >  2012 03-01 0.567
> > >  2014 04-01 0.599
> > >  2016 05-01 0.642
> > >  2018 06-01 0.685",
> > >                  header=TRUE,
> > >                  stringsAsFactors=FALSE)
> > >
> > > library(plotly)
> > > plot_ly(sydf,
> > >         x = ~year1,
> > >         y = ~rate,
> > >         type = 'scatter', mode = 'lines') %>%
> > >   layout(
> > >     xaxis = list(
> > >       ticktext = sydf$monthday,
> > >       tickvals = sydf$year1,
> > >       tickmode = "array",
> > >       tickangle = 270
> > >     ))
> > >
> > > This is where I found about the parameters:
> > > https://plot.ly/r/tick-formatting/
> > >
> > > Now my other challenge is that with my data I have a lot more values on the x axis, and they overlap even when turned vertically. I guess there must be a way of taking a number of values evenly from the list of x axis lables, and use that for the "ticktext" parameter.
> > > I thought it must be some variation of the seq(from, to, by= ). Can I use that with a list of strings?
> > >
> > > Thanks,
> > > Steven
> > >
> > > -----Original Message-----
> > > From: Jim Lemon <[hidden email]>
> > > Sent: Thursday, July 11, 2019 7:46 PM
> > > To: [hidden email]; r-help mailing list <[hidden email]>
> > > Subject: Re: [R] Plotting in R
> > >
> > > Hi Steven,
> > > It is pretty easy, but there are one or two things to watch for.
> > > First, don't use a hyphen in a field name unless you enclose it in
> > > single quotes when extracting it. I've just removed the hyphen in this
> > > example:
> > >
> > > sydf<-read.table(text="year1 monthday rate
> > >  1993 05-01 0.608
> > >  1994 06-01 0.622
> > >  1996 07-01 0.623
> > >  1998 08-01 0.647
> > >  2000 09-01 0.646
> > >  2002 10-01 0.625
> > >  2004 11-01 0.628
> > >  2006 12-01 0.685
> > >  2008 01-01 0.679
> > >  2010 02-01 0.595
> > >  2012 03-01 0.567
> > >  2014 04-01 0.599
> > >  2016 05-01 0.642
> > >  2018 06-01 0.685",
> > >  header=TRUE,
> > >  stringsAsFactors=FALSE)
> > > sydf$date<-as.Date(paste(sydf$year1,sydf$monthday),"%Y %m-%d")
> > > library(plotrix)
> > > par(mar=c(6,4,4,2))
> > > plot(sydf$date,sydf$rate,type="b",xaxt="n")
> > > staxlab(1,at=sydf$date,labels=sydf$monthday)
> > >
> > > Note that I have also added the stringsAsFactors argument to prevent monthdate being read as a factor. I expected that the axis labels would be crowded so I used the plotrix library to stagger the x-axis labels. Hope this solves your problem.
> > >
> > > Jim
> > >
> > > On Fri, Jul 12, 2019 at 1:59 AM <[hidden email]> wrote:
> > > >
> > > > Hi Jim,
> > > >
> > > > Thanks for your email.
> > > > My question was: how to change the x axis labels without changing the chart.
> > > > Or is that not possible?
> > > > Using your example, I added another column:
> > > > sydf<-read.table(text="year1 month-day rate
> > > >  1993 05-01 0.608
> > > >  1994 06-01 0.622
> > > >  1996 07-01 0.623
> > > >  1998 08-01 0.647
> > > >  2000 09-01 0.646
> > > >  2002 10-01 0.625
> > > >  2004 11-01 0.628
> > > >  2006 12-01 0.685
> > > >  2008 01-01 0.679
> > > >  2010 02-01 0.595
> > > >  2012 03-01 0.567
> > > >  2014 04-01 0.599
> > > >  2016 05-01 0.642
> > > >  2018 06-01 0.685",
> > > >                  header=TRUE)
> > > >
> > > > How can I show the column "month-day" as labels on the x axis, but
> > > > still have the plot showing the chart as rate based on year?
> > > > I tried this:
> > > > plot(sydf$year,sydf$rate,type="b",
> > > >      xlab="month-day",ylab="Rate")
> > > >
> > > > but this only changes the title of the x axis to "month-day". I want
> > > > the values on the x axis to show 05-01  06-01, etc.
> > > > Is that possible?
> > > >
> > > > Thanks,
> > > > Steven
> > > >
> > > > -----Original Message-----
> > > > From: R-help <[hidden email]> On Behalf Of Jim Lemon
> > > > Sent: Sunday, July 7, 2019 2:59 AM
> > > > To: Steven Yen <[hidden email]>; r-help mailing list
> > > > <[hidden email]>
> > > > Subject: Re: [R] Plotting in R
> > > >
> > > > Hi Steven,
> > > > A basic plot can be displayed like this:
> > > >
> > > > sydf<-read.table(text="year rate
> > > >  1993 0.608
> > > >  1994 0.622
> > > >  1996 0.623
> > > >  1998 0.647
> > > >  2000 0.646
> > > >  2002 0.625
> > > >  2004 0.628
> > > >  2006 0.685
> > > >  2008 0.679
> > > >  2010 0.595
> > > >  2012 0.567
> > > >  2014 0.599
> > > >  2016 0.642
> > > >  2018 0.685",
> > > > header=TRUE)
> > > > plot(sydf$year,sydf$rate,type="b",
> > > > xlab="Year",ylab="Rate")
> > > >
> > > > When you add more years and rates to the data frame, the axes will
> > > > change to include the years and rates outside the ones in your
> > > > example. As some have noted, this is a very basic question.
> > > >
> > > > Jim
> > > >
> > > > On Sat, Jul 6, 2019 at 11:33 PM Steven Yen <[hidden email]> wrote:
> > > > >
> > > > > I have a data frame containing two variables: year and rate (shown below).
> > > > > Which function can I use to plot rate (y-axis) against year (x-axis)?
> > > > > There will be more columns of rate later on.
> > > > > Thank you.
> > > > >
> > > > > year rate 1 1993 0.608 2 1994 0.622 3 1996 0.623 4 1998 0.647 5
> > > > > 2000
> > > > > 0.646 6 2002 0.625 7 2004 0.628 8 2006 0.685 9 2008 0.679 10 2010
> > > > > 0.595
> > > > > 11 2012 0.567 12 2014 0.599 13 2016 0.642 14 2018 0.685
> > > > >
> > > > >
> > > > > --
> > > > > [hidden email] (S.T. Yen)
> > > > >
> > > > >
> > > > >
> > > > > ---
> > > > > This email has been checked for viruses by Avast antivirus software.
> > > > > https://www.avast.com/antivirus
> > > > >
> > > > >         [[alternative HTML version deleted]]
> > > > >
> > > > > ______________________________________________
> > > > > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > > > > 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.
> > > >
> > > > ______________________________________________
> > > > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > > > 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.
> > > >
> > >
> >

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Plotting in R

nstefi
Thanks Jim,

Yes, I only want to show the month and day as labels, because on my chart I am actually showing 2 line charts, one from the previous year, and one from this year, to compare them, and the month and day are matching for them, but the year would be different, so it makes sense to show only month and day on the x axis.
So it looks like I got the solution already for that part, the new challenge is that there were too many labels and overlapping on each other.
What is the best way to evenly sample values from a long list of string values?
So let's say my data was this overly simplified:
sydf<-read.table(text="year monthday rate
 1993 05-01 0.608
 1994 06-01 0.622
 1996 07-01 0.623
 1998 08-01 0.647
 2000 09-01 0.646
 2002 10-01 0.625
 2004 11-01 0.628
 2006 12-01 0.685
 2008 01-01 0.679
 2010 02-01 0.595
 2012 03-01 0.567
 2014 04-01 0.599
 2016 05-01 0.642
 2018 06-01 0.685",
                 header=TRUE,
                 stringsAsFactors=FALSE)

Then my x axis would be based on the values of sydf$monthday - In my case there are lots of values. How can I subset this to have only every n-th value? I guess this must be some common operation in R.
So if I have 200 values, and I only want to show 20 of them as labels, I would calculate n = 200/20 = 10 and I want to get every 10th value in the list sydf$monthday. But the list has strings, so I cannot use "by =".

Thank you,
Steven

-----Original Message-----
From: Jim Lemon <[hidden email]>
Sent: Friday, July 12, 2019 6:41 PM
To: [hidden email]
Cc: r-help mailing list <[hidden email]>
Subject: Re: [R] Plotting in R

Oh, sorry, I think I see what you have tried to do. You want yearly ticks but month-day labels. These won't mean much unless you also have the year. If you ask for a date with just the year, as.Date will give you a date in the middle of that year:

as.Date("2002","%Y")
[1] "2002-07-13"

So making the labels these mid-year dates as character strings might do what you want:

yrlabels<-as.character(yrticks)

Jim

On Sat, Jul 13, 2019 at 8:33 AM Jim Lemon <[hidden email]> wrote:

>
> Hi Steven,
> year1 is a number (e.g. 1993), monthday (e.g. 05-01) is not.
>
> Jim
>
> On Fri, Jul 12, 2019 at 10:56 PM <[hidden email]> wrote:
> >
> > Thanks Jim.
> >
> > I am trying to apply this to my version with plot_ly, and couldn't make it to work.
> > The sydf$year1 field is numeric, so the min() and max() works, but when I tried to use your formula for the sydf$monthday field I get an error:
> > yrticks <- as.Date(as.character(seq(min(sydf$monthday),max(sydf$monthday),by=2)),
> >                   "%Y")
> >
> > Error in seq.default(min(sydf$monthday), max(sydf$monthday), by = 2) :
> >   'from' must be a finite number
> > In addition: Warning message:
> > In seq.default(min(sydf$monthday), max(sydf$monthday), by = 2) :
> >   NAs introduced by coercion
> >
> > So I thought maybe you need to sample the tickvals not the ticktext and it would show the corresponding tick labels in the right palaces. Then I tried the way you had like this:
> > sydf<-read.table(text="year1 monthday rate
> >  1993 05-01 0.608
> >  1994 06-01 0.622
> >  1996 07-01 0.623
> >  1998 08-01 0.647
> >  2000 09-01 0.646
> >  2002 10-01 0.625
> >  2004 11-01 0.628
> >  2006 12-01 0.685
> >  2008 01-01 0.679
> >  2010 02-01 0.595
> >  2012 03-01 0.567
> >  2014 04-01 0.599
> >  2016 05-01 0.642
> >  2018 06-01 0.685",
> >                  header=TRUE,
> >                  stringsAsFactors=FALSE)
> >
> > yrticks <- as.Date(as.character(seq(min(sydf$year1),max(sydf$year1),by=2)),
> >                   "%Y")
> >
> > library(plotly)
> > plot_ly(sydf,
> >         x = ~year1,
> >         y = ~rate,
> >         type = 'scatter', mode = 'lines') %>%
> >   layout(
> >     xaxis = list(
> >       ticktext = sydf$monthday,
> >       tickvals = sydf$yrticks,
> >       tickmode = "array",
> >       tickangle = 270
> >     ))
> >
> > But the chart didn't show any tick labels.
> > I guess I need to sample sydf$monthday, right? Because that's what I want to show as tick labels. But the problem is that monthday is string, and can't use a value for "by=", maybe I need to sample somehow by the index position.
> >
> > Thanks,
> > Steven
> >
> > -----Original Message-----
> > From: Jim Lemon <[hidden email]>
> > Sent: Thursday, July 11, 2019 10:41 PM
> > To: [hidden email]
> > Cc: r-help mailing list <[hidden email]>
> > Subject: Re: [R] Plotting in R
> >
> > Hi Steven,
> > Neat solution. With a lot more values on the time axis, you will be better off with something like:
> >
> > yrticks<-as.Date(as.character(seq(min(sydf$year1),max(sydf$year1),by
> > =2)),
> >  "%Y")
> > axis(1,at=yrticks,labels=format(yrticks,"%Y"))
> >
> > You probably won't need staxlab for that.
> >
> > Jim
> >
> > On Fri, Jul 12, 2019 at 11:55 AM <[hidden email]> wrote:
> > >
> > > Thanks Jim, that worked.
> > >
> > > > I expected that the axis labels would be crowded so I used the plotrix library to stagger the x-axis labels. Hope this solves your problem.
> > > I liked how that showed, not overlapping on each other. I wasn't aware of the plotrix library.
> > >
> > > In my code I was using plot_ly for visualization, because it looked nicer compared to the simple plot() function, and I have the chart in a Shiny dashboard (RMD file).
> > > I found in the meantime today some code example for plot_ly and using the same data it looks like this:
> > > sydf<-read.table(text="year1 monthday rate
> > >  1993 05-01 0.608
> > >  1994 06-01 0.622
> > >  1996 07-01 0.623
> > >  1998 08-01 0.647
> > >  2000 09-01 0.646
> > >  2002 10-01 0.625
> > >  2004 11-01 0.628
> > >  2006 12-01 0.685
> > >  2008 01-01 0.679
> > >  2010 02-01 0.595
> > >  2012 03-01 0.567
> > >  2014 04-01 0.599
> > >  2016 05-01 0.642
> > >  2018 06-01 0.685",
> > >                  header=TRUE,
> > >                  stringsAsFactors=FALSE)
> > >
> > > library(plotly)
> > > plot_ly(sydf,
> > >         x = ~year1,
> > >         y = ~rate,
> > >         type = 'scatter', mode = 'lines') %>%
> > >   layout(
> > >     xaxis = list(
> > >       ticktext = sydf$monthday,
> > >       tickvals = sydf$year1,
> > >       tickmode = "array",
> > >       tickangle = 270
> > >     ))
> > >
> > > This is where I found about the parameters:
> > > https://plot.ly/r/tick-formatting/
> > >
> > > Now my other challenge is that with my data I have a lot more values on the x axis, and they overlap even when turned vertically. I guess there must be a way of taking a number of values evenly from the list of x axis lables, and use that for the "ticktext" parameter.
> > > I thought it must be some variation of the seq(from, to, by= ). Can I use that with a list of strings?
> > >
> > > Thanks,
> > > Steven
> > >
> > > -----Original Message-----
> > > From: Jim Lemon <[hidden email]>
> > > Sent: Thursday, July 11, 2019 7:46 PM
> > > To: [hidden email]; r-help mailing list <[hidden email]>
> > > Subject: Re: [R] Plotting in R
> > >
> > > Hi Steven,
> > > It is pretty easy, but there are one or two things to watch for.
> > > First, don't use a hyphen in a field name unless you enclose it in
> > > single quotes when extracting it. I've just removed the hyphen in
> > > this
> > > example:
> > >
> > > sydf<-read.table(text="year1 monthday rate
> > >  1993 05-01 0.608
> > >  1994 06-01 0.622
> > >  1996 07-01 0.623
> > >  1998 08-01 0.647
> > >  2000 09-01 0.646
> > >  2002 10-01 0.625
> > >  2004 11-01 0.628
> > >  2006 12-01 0.685
> > >  2008 01-01 0.679
> > >  2010 02-01 0.595
> > >  2012 03-01 0.567
> > >  2014 04-01 0.599
> > >  2016 05-01 0.642
> > >  2018 06-01 0.685",
> > >  header=TRUE,
> > >  stringsAsFactors=FALSE)
> > > sydf$date<-as.Date(paste(sydf$year1,sydf$monthday),"%Y %m-%d")
> > > library(plotrix)
> > > par(mar=c(6,4,4,2))
> > > plot(sydf$date,sydf$rate,type="b",xaxt="n")
> > > staxlab(1,at=sydf$date,labels=sydf$monthday)
> > >
> > > Note that I have also added the stringsAsFactors argument to prevent monthdate being read as a factor. I expected that the axis labels would be crowded so I used the plotrix library to stagger the x-axis labels. Hope this solves your problem.
> > >
> > > Jim
> > >
> > > On Fri, Jul 12, 2019 at 1:59 AM <[hidden email]> wrote:
> > > >
> > > > Hi Jim,
> > > >
> > > > Thanks for your email.
> > > > My question was: how to change the x axis labels without changing the chart.
> > > > Or is that not possible?
> > > > Using your example, I added another column:
> > > > sydf<-read.table(text="year1 month-day rate
> > > >  1993 05-01 0.608
> > > >  1994 06-01 0.622
> > > >  1996 07-01 0.623
> > > >  1998 08-01 0.647
> > > >  2000 09-01 0.646
> > > >  2002 10-01 0.625
> > > >  2004 11-01 0.628
> > > >  2006 12-01 0.685
> > > >  2008 01-01 0.679
> > > >  2010 02-01 0.595
> > > >  2012 03-01 0.567
> > > >  2014 04-01 0.599
> > > >  2016 05-01 0.642
> > > >  2018 06-01 0.685",
> > > >                  header=TRUE)
> > > >
> > > > How can I show the column "month-day" as labels on the x axis,
> > > > but still have the plot showing the chart as rate based on year?
> > > > I tried this:
> > > > plot(sydf$year,sydf$rate,type="b",
> > > >      xlab="month-day",ylab="Rate")
> > > >
> > > > but this only changes the title of the x axis to "month-day". I
> > > > want the values on the x axis to show 05-01  06-01, etc.
> > > > Is that possible?
> > > >
> > > > Thanks,
> > > > Steven
> > > >
> > > > -----Original Message-----
> > > > From: R-help <[hidden email]> On Behalf Of Jim
> > > > Lemon
> > > > Sent: Sunday, July 7, 2019 2:59 AM
> > > > To: Steven Yen <[hidden email]>; r-help mailing list
> > > > <[hidden email]>
> > > > Subject: Re: [R] Plotting in R
> > > >
> > > > Hi Steven,
> > > > A basic plot can be displayed like this:
> > > >
> > > > sydf<-read.table(text="year rate
> > > >  1993 0.608
> > > >  1994 0.622
> > > >  1996 0.623
> > > >  1998 0.647
> > > >  2000 0.646
> > > >  2002 0.625
> > > >  2004 0.628
> > > >  2006 0.685
> > > >  2008 0.679
> > > >  2010 0.595
> > > >  2012 0.567
> > > >  2014 0.599
> > > >  2016 0.642
> > > >  2018 0.685",
> > > > header=TRUE)
> > > > plot(sydf$year,sydf$rate,type="b",
> > > > xlab="Year",ylab="Rate")
> > > >
> > > > When you add more years and rates to the data frame, the axes
> > > > will change to include the years and rates outside the ones in
> > > > your example. As some have noted, this is a very basic question.
> > > >
> > > > Jim
> > > >
> > > > On Sat, Jul 6, 2019 at 11:33 PM Steven Yen <[hidden email]> wrote:
> > > > >
> > > > > I have a data frame containing two variables: year and rate (shown below).
> > > > > Which function can I use to plot rate (y-axis) against year (x-axis)?
> > > > > There will be more columns of rate later on.
> > > > > Thank you.
> > > > >
> > > > > year rate 1 1993 0.608 2 1994 0.622 3 1996 0.623 4 1998 0.647
> > > > > 5
> > > > > 2000
> > > > > 0.646 6 2002 0.625 7 2004 0.628 8 2006 0.685 9 2008 0.679 10
> > > > > 2010
> > > > > 0.595
> > > > > 11 2012 0.567 12 2014 0.599 13 2016 0.642 14 2018 0.685
> > > > >
> > > > >
> > > > > --
> > > > > [hidden email] (S.T. Yen)
> > > > >
> > > > >
> > > > >
> > > > > ---
> > > > > This email has been checked for viruses by Avast antivirus software.
> > > > > https://www.avast.com/antivirus
> > > > >
> > > > >         [[alternative HTML version deleted]]
> > > > >
> > > > > ______________________________________________
> > > > > [hidden email] mailing list -- To UNSUBSCRIBE and more,
> > > > > see 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.
> > > >
> > > > ______________________________________________
> > > > [hidden email] mailing list -- To UNSUBSCRIBE and more,
> > > > see 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.
> > > >
> > >
> >

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Plotting in R

nstefi
OK, I think I got this:

For example every 3rd element would be:
sydf$monthday[seq(1, length(sydf$monthday), 3)]

Thanks,
Steven

-----Original Message-----
From: [hidden email] <[hidden email]>
Sent: Tuesday, July 16, 2019 9:39 AM
To: 'Jim Lemon' <[hidden email]>
Cc: 'r-help mailing list' <[hidden email]>
Subject: RE: [R] Plotting in R

Thanks Jim,

Yes, I only want to show the month and day as labels, because on my chart I am actually showing 2 line charts, one from the previous year, and one from this year, to compare them, and the month and day are matching for them, but the year would be different, so it makes sense to show only month and day on the x axis.
So it looks like I got the solution already for that part, the new challenge is that there were too many labels and overlapping on each other.
What is the best way to evenly sample values from a long list of string values?
So let's say my data was this overly simplified:
sydf<-read.table(text="year monthday rate
 1993 05-01 0.608
 1994 06-01 0.622
 1996 07-01 0.623
 1998 08-01 0.647
 2000 09-01 0.646
 2002 10-01 0.625
 2004 11-01 0.628
 2006 12-01 0.685
 2008 01-01 0.679
 2010 02-01 0.595
 2012 03-01 0.567
 2014 04-01 0.599
 2016 05-01 0.642
 2018 06-01 0.685",
                 header=TRUE,
                 stringsAsFactors=FALSE)

Then my x axis would be based on the values of sydf$monthday - In my case there are lots of values. How can I subset this to have only every n-th value? I guess this must be some common operation in R.
So if I have 200 values, and I only want to show 20 of them as labels, I would calculate n = 200/20 = 10 and I want to get every 10th value in the list sydf$monthday. But the list has strings, so I cannot use "by =".

Thank you,
Steven

-----Original Message-----
From: Jim Lemon <[hidden email]>
Sent: Friday, July 12, 2019 6:41 PM
To: [hidden email]
Cc: r-help mailing list <[hidden email]>
Subject: Re: [R] Plotting in R

Oh, sorry, I think I see what you have tried to do. You want yearly ticks but month-day labels. These won't mean much unless you also have the year. If you ask for a date with just the year, as.Date will give you a date in the middle of that year:

as.Date("2002","%Y")
[1] "2002-07-13"

So making the labels these mid-year dates as character strings might do what you want:

yrlabels<-as.character(yrticks)

Jim

On Sat, Jul 13, 2019 at 8:33 AM Jim Lemon <[hidden email]> wrote:

>
> Hi Steven,
> year1 is a number (e.g. 1993), monthday (e.g. 05-01) is not.
>
> Jim
>
> On Fri, Jul 12, 2019 at 10:56 PM <[hidden email]> wrote:
> >
> > Thanks Jim.
> >
> > I am trying to apply this to my version with plot_ly, and couldn't make it to work.
> > The sydf$year1 field is numeric, so the min() and max() works, but when I tried to use your formula for the sydf$monthday field I get an error:
> > yrticks <- as.Date(as.character(seq(min(sydf$monthday),max(sydf$monthday),by=2)),
> >                   "%Y")
> >
> > Error in seq.default(min(sydf$monthday), max(sydf$monthday), by = 2) :
> >   'from' must be a finite number
> > In addition: Warning message:
> > In seq.default(min(sydf$monthday), max(sydf$monthday), by = 2) :
> >   NAs introduced by coercion
> >
> > So I thought maybe you need to sample the tickvals not the ticktext and it would show the corresponding tick labels in the right palaces. Then I tried the way you had like this:
> > sydf<-read.table(text="year1 monthday rate
> >  1993 05-01 0.608
> >  1994 06-01 0.622
> >  1996 07-01 0.623
> >  1998 08-01 0.647
> >  2000 09-01 0.646
> >  2002 10-01 0.625
> >  2004 11-01 0.628
> >  2006 12-01 0.685
> >  2008 01-01 0.679
> >  2010 02-01 0.595
> >  2012 03-01 0.567
> >  2014 04-01 0.599
> >  2016 05-01 0.642
> >  2018 06-01 0.685",
> >                  header=TRUE,
> >                  stringsAsFactors=FALSE)
> >
> > yrticks <- as.Date(as.character(seq(min(sydf$year1),max(sydf$year1),by=2)),
> >                   "%Y")
> >
> > library(plotly)
> > plot_ly(sydf,
> >         x = ~year1,
> >         y = ~rate,
> >         type = 'scatter', mode = 'lines') %>%
> >   layout(
> >     xaxis = list(
> >       ticktext = sydf$monthday,
> >       tickvals = sydf$yrticks,
> >       tickmode = "array",
> >       tickangle = 270
> >     ))
> >
> > But the chart didn't show any tick labels.
> > I guess I need to sample sydf$monthday, right? Because that's what I want to show as tick labels. But the problem is that monthday is string, and can't use a value for "by=", maybe I need to sample somehow by the index position.
> >
> > Thanks,
> > Steven
> >
> > -----Original Message-----
> > From: Jim Lemon <[hidden email]>
> > Sent: Thursday, July 11, 2019 10:41 PM
> > To: [hidden email]
> > Cc: r-help mailing list <[hidden email]>
> > Subject: Re: [R] Plotting in R
> >
> > Hi Steven,
> > Neat solution. With a lot more values on the time axis, you will be better off with something like:
> >
> > yrticks<-as.Date(as.character(seq(min(sydf$year1),max(sydf$year1),by
> > =2)),
> >  "%Y")
> > axis(1,at=yrticks,labels=format(yrticks,"%Y"))
> >
> > You probably won't need staxlab for that.
> >
> > Jim
> >
> > On Fri, Jul 12, 2019 at 11:55 AM <[hidden email]> wrote:
> > >
> > > Thanks Jim, that worked.
> > >
> > > > I expected that the axis labels would be crowded so I used the plotrix library to stagger the x-axis labels. Hope this solves your problem.
> > > I liked how that showed, not overlapping on each other. I wasn't aware of the plotrix library.
> > >
> > > In my code I was using plot_ly for visualization, because it looked nicer compared to the simple plot() function, and I have the chart in a Shiny dashboard (RMD file).
> > > I found in the meantime today some code example for plot_ly and using the same data it looks like this:
> > > sydf<-read.table(text="year1 monthday rate
> > >  1993 05-01 0.608
> > >  1994 06-01 0.622
> > >  1996 07-01 0.623
> > >  1998 08-01 0.647
> > >  2000 09-01 0.646
> > >  2002 10-01 0.625
> > >  2004 11-01 0.628
> > >  2006 12-01 0.685
> > >  2008 01-01 0.679
> > >  2010 02-01 0.595
> > >  2012 03-01 0.567
> > >  2014 04-01 0.599
> > >  2016 05-01 0.642
> > >  2018 06-01 0.685",
> > >                  header=TRUE,
> > >                  stringsAsFactors=FALSE)
> > >
> > > library(plotly)
> > > plot_ly(sydf,
> > >         x = ~year1,
> > >         y = ~rate,
> > >         type = 'scatter', mode = 'lines') %>%
> > >   layout(
> > >     xaxis = list(
> > >       ticktext = sydf$monthday,
> > >       tickvals = sydf$year1,
> > >       tickmode = "array",
> > >       tickangle = 270
> > >     ))
> > >
> > > This is where I found about the parameters:
> > > https://plot.ly/r/tick-formatting/
> > >
> > > Now my other challenge is that with my data I have a lot more values on the x axis, and they overlap even when turned vertically. I guess there must be a way of taking a number of values evenly from the list of x axis lables, and use that for the "ticktext" parameter.
> > > I thought it must be some variation of the seq(from, to, by= ). Can I use that with a list of strings?
> > >
> > > Thanks,
> > > Steven
> > >
> > > -----Original Message-----
> > > From: Jim Lemon <[hidden email]>
> > > Sent: Thursday, July 11, 2019 7:46 PM
> > > To: [hidden email]; r-help mailing list <[hidden email]>
> > > Subject: Re: [R] Plotting in R
> > >
> > > Hi Steven,
> > > It is pretty easy, but there are one or two things to watch for.
> > > First, don't use a hyphen in a field name unless you enclose it in
> > > single quotes when extracting it. I've just removed the hyphen in
> > > this
> > > example:
> > >
> > > sydf<-read.table(text="year1 monthday rate
> > >  1993 05-01 0.608
> > >  1994 06-01 0.622
> > >  1996 07-01 0.623
> > >  1998 08-01 0.647
> > >  2000 09-01 0.646
> > >  2002 10-01 0.625
> > >  2004 11-01 0.628
> > >  2006 12-01 0.685
> > >  2008 01-01 0.679
> > >  2010 02-01 0.595
> > >  2012 03-01 0.567
> > >  2014 04-01 0.599
> > >  2016 05-01 0.642
> > >  2018 06-01 0.685",
> > >  header=TRUE,
> > >  stringsAsFactors=FALSE)
> > > sydf$date<-as.Date(paste(sydf$year1,sydf$monthday),"%Y %m-%d")
> > > library(plotrix)
> > > par(mar=c(6,4,4,2))
> > > plot(sydf$date,sydf$rate,type="b",xaxt="n")
> > > staxlab(1,at=sydf$date,labels=sydf$monthday)
> > >
> > > Note that I have also added the stringsAsFactors argument to prevent monthdate being read as a factor. I expected that the axis labels would be crowded so I used the plotrix library to stagger the x-axis labels. Hope this solves your problem.
> > >
> > > Jim
> > >
> > > On Fri, Jul 12, 2019 at 1:59 AM <[hidden email]> wrote:
> > > >
> > > > Hi Jim,
> > > >
> > > > Thanks for your email.
> > > > My question was: how to change the x axis labels without changing the chart.
> > > > Or is that not possible?
> > > > Using your example, I added another column:
> > > > sydf<-read.table(text="year1 month-day rate
> > > >  1993 05-01 0.608
> > > >  1994 06-01 0.622
> > > >  1996 07-01 0.623
> > > >  1998 08-01 0.647
> > > >  2000 09-01 0.646
> > > >  2002 10-01 0.625
> > > >  2004 11-01 0.628
> > > >  2006 12-01 0.685
> > > >  2008 01-01 0.679
> > > >  2010 02-01 0.595
> > > >  2012 03-01 0.567
> > > >  2014 04-01 0.599
> > > >  2016 05-01 0.642
> > > >  2018 06-01 0.685",
> > > >                  header=TRUE)
> > > >
> > > > How can I show the column "month-day" as labels on the x axis,
> > > > but still have the plot showing the chart as rate based on year?
> > > > I tried this:
> > > > plot(sydf$year,sydf$rate,type="b",
> > > >      xlab="month-day",ylab="Rate")
> > > >
> > > > but this only changes the title of the x axis to "month-day". I
> > > > want the values on the x axis to show 05-01  06-01, etc.
> > > > Is that possible?
> > > >
> > > > Thanks,
> > > > Steven
> > > >
> > > > -----Original Message-----
> > > > From: R-help <[hidden email]> On Behalf Of Jim
> > > > Lemon
> > > > Sent: Sunday, July 7, 2019 2:59 AM
> > > > To: Steven Yen <[hidden email]>; r-help mailing list
> > > > <[hidden email]>
> > > > Subject: Re: [R] Plotting in R
> > > >
> > > > Hi Steven,
> > > > A basic plot can be displayed like this:
> > > >
> > > > sydf<-read.table(text="year rate
> > > >  1993 0.608
> > > >  1994 0.622
> > > >  1996 0.623
> > > >  1998 0.647
> > > >  2000 0.646
> > > >  2002 0.625
> > > >  2004 0.628
> > > >  2006 0.685
> > > >  2008 0.679
> > > >  2010 0.595
> > > >  2012 0.567
> > > >  2014 0.599
> > > >  2016 0.642
> > > >  2018 0.685",
> > > > header=TRUE)
> > > > plot(sydf$year,sydf$rate,type="b",
> > > > xlab="Year",ylab="Rate")
> > > >
> > > > When you add more years and rates to the data frame, the axes
> > > > will change to include the years and rates outside the ones in
> > > > your example. As some have noted, this is a very basic question.
> > > >
> > > > Jim
> > > >
> > > > On Sat, Jul 6, 2019 at 11:33 PM Steven Yen <[hidden email]> wrote:
> > > > >
> > > > > I have a data frame containing two variables: year and rate (shown below).
> > > > > Which function can I use to plot rate (y-axis) against year (x-axis)?
> > > > > There will be more columns of rate later on.
> > > > > Thank you.
> > > > >
> > > > > year rate 1 1993 0.608 2 1994 0.622 3 1996 0.623 4 1998 0.647
> > > > > 5
> > > > > 2000
> > > > > 0.646 6 2002 0.625 7 2004 0.628 8 2006 0.685 9 2008 0.679 10
> > > > > 2010
> > > > > 0.595
> > > > > 11 2012 0.567 12 2014 0.599 13 2016 0.642 14 2018 0.685
> > > > >
> > > > >
> > > > > --
> > > > > [hidden email] (S.T. Yen)
> > > > >
> > > > >
> > > > >
> > > > > ---
> > > > > This email has been checked for viruses by Avast antivirus software.
> > > > > https://www.avast.com/antivirus
> > > > >
> > > > >         [[alternative HTML version deleted]]
> > > > >
> > > > > ______________________________________________
> > > > > [hidden email] mailing list -- To UNSUBSCRIBE and more,
> > > > > see 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.
> > > >
> > > > ______________________________________________
> > > > [hidden email] mailing list -- To UNSUBSCRIBE and more,
> > > > see 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.
> > > >
> > >
> >

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Plotting in R

nstefi
Sorry, this still doesn't work.

When I use that expression in "ticktext" parameter, it shows those every 3rd label in the beginning, and after they finished, it shows some remaining values from "year" column.
When I tried with my real data, it was showing the original x axis values as lables, it seems like it ignored the "ticktext" parameter.
This is my short example:
sydf<-read.table(text="year monthday rate
 1993 05-01 0.608
 1994 06-01 0.622
 1996 07-01 0.623
 1998 08-01 0.647
 2000 09-01 0.646
 2002 10-01 0.625
 2004 11-01 0.628
 2006 12-01 0.685
 2008 01-01 0.679
 2010 02-01 0.595
 2012 03-01 0.567
 2014 04-01 0.599
 2016 05-01 0.642
 2018 06-01 0.685",
                 header=TRUE,
                 stringsAsFactors=FALSE)

library(plotly)
plot_ly(sydf,
        x = ~year,
        y = ~rate,
        type = 'scatter', mode = 'lines') %>%
  layout(
    xaxis = list(
      ticktext = sydf$monthday[seq(1, length(sydf$monthday), 3)],
      tickvals = sydf$year,
      tickmode = "array",
      tickangle = 270
    ))

Any ideas?

Thanks,
Steven

-----Original Message-----
From: [hidden email] <[hidden email]>
Sent: Tuesday, July 16, 2019 9:55 AM
To: 'Jim Lemon' <[hidden email]>
Cc: 'r-help mailing list' <[hidden email]>
Subject: RE: [R] Plotting in R

OK, I think I got this:

For example every 3rd element would be:
sydf$monthday[seq(1, length(sydf$monthday), 3)]

Thanks,
Steven

-----Original Message-----
From: [hidden email] <[hidden email]>
Sent: Tuesday, July 16, 2019 9:39 AM
To: 'Jim Lemon' <[hidden email]>
Cc: 'r-help mailing list' <[hidden email]>
Subject: RE: [R] Plotting in R

Thanks Jim,

Yes, I only want to show the month and day as labels, because on my chart I am actually showing 2 line charts, one from the previous year, and one from this year, to compare them, and the month and day are matching for them, but the year would be different, so it makes sense to show only month and day on the x axis.
So it looks like I got the solution already for that part, the new challenge is that there were too many labels and overlapping on each other.
What is the best way to evenly sample values from a long list of string values?
So let's say my data was this overly simplified:
sydf<-read.table(text="year monthday rate
 1993 05-01 0.608
 1994 06-01 0.622
 1996 07-01 0.623
 1998 08-01 0.647
 2000 09-01 0.646
 2002 10-01 0.625
 2004 11-01 0.628
 2006 12-01 0.685
 2008 01-01 0.679
 2010 02-01 0.595
 2012 03-01 0.567
 2014 04-01 0.599
 2016 05-01 0.642
 2018 06-01 0.685",
                 header=TRUE,
                 stringsAsFactors=FALSE)

Then my x axis would be based on the values of sydf$monthday - In my case there are lots of values. How can I subset this to have only every n-th value? I guess this must be some common operation in R.
So if I have 200 values, and I only want to show 20 of them as labels, I would calculate n = 200/20 = 10 and I want to get every 10th value in the list sydf$monthday. But the list has strings, so I cannot use "by =".

Thank you,
Steven

-----Original Message-----
From: Jim Lemon <[hidden email]>
Sent: Friday, July 12, 2019 6:41 PM
To: [hidden email]
Cc: r-help mailing list <[hidden email]>
Subject: Re: [R] Plotting in R

Oh, sorry, I think I see what you have tried to do. You want yearly ticks but month-day labels. These won't mean much unless you also have the year. If you ask for a date with just the year, as.Date will give you a date in the middle of that year:

as.Date("2002","%Y")
[1] "2002-07-13"

So making the labels these mid-year dates as character strings might do what you want:

yrlabels<-as.character(yrticks)

Jim

On Sat, Jul 13, 2019 at 8:33 AM Jim Lemon <[hidden email]> wrote:

>
> Hi Steven,
> year1 is a number (e.g. 1993), monthday (e.g. 05-01) is not.
>
> Jim
>
> On Fri, Jul 12, 2019 at 10:56 PM <[hidden email]> wrote:
> >
> > Thanks Jim.
> >
> > I am trying to apply this to my version with plot_ly, and couldn't make it to work.
> > The sydf$year1 field is numeric, so the min() and max() works, but when I tried to use your formula for the sydf$monthday field I get an error:
> > yrticks <- as.Date(as.character(seq(min(sydf$monthday),max(sydf$monthday),by=2)),
> >                   "%Y")
> >
> > Error in seq.default(min(sydf$monthday), max(sydf$monthday), by = 2) :
> >   'from' must be a finite number
> > In addition: Warning message:
> > In seq.default(min(sydf$monthday), max(sydf$monthday), by = 2) :
> >   NAs introduced by coercion
> >
> > So I thought maybe you need to sample the tickvals not the ticktext and it would show the corresponding tick labels in the right palaces. Then I tried the way you had like this:
> > sydf<-read.table(text="year1 monthday rate
> >  1993 05-01 0.608
> >  1994 06-01 0.622
> >  1996 07-01 0.623
> >  1998 08-01 0.647
> >  2000 09-01 0.646
> >  2002 10-01 0.625
> >  2004 11-01 0.628
> >  2006 12-01 0.685
> >  2008 01-01 0.679
> >  2010 02-01 0.595
> >  2012 03-01 0.567
> >  2014 04-01 0.599
> >  2016 05-01 0.642
> >  2018 06-01 0.685",
> >                  header=TRUE,
> >                  stringsAsFactors=FALSE)
> >
> > yrticks <- as.Date(as.character(seq(min(sydf$year1),max(sydf$year1),by=2)),
> >                   "%Y")
> >
> > library(plotly)
> > plot_ly(sydf,
> >         x = ~year1,
> >         y = ~rate,
> >         type = 'scatter', mode = 'lines') %>%
> >   layout(
> >     xaxis = list(
> >       ticktext = sydf$monthday,
> >       tickvals = sydf$yrticks,
> >       tickmode = "array",
> >       tickangle = 270
> >     ))
> >
> > But the chart didn't show any tick labels.
> > I guess I need to sample sydf$monthday, right? Because that's what I want to show as tick labels. But the problem is that monthday is string, and can't use a value for "by=", maybe I need to sample somehow by the index position.
> >
> > Thanks,
> > Steven
> >
> > -----Original Message-----
> > From: Jim Lemon <[hidden email]>
> > Sent: Thursday, July 11, 2019 10:41 PM
> > To: [hidden email]
> > Cc: r-help mailing list <[hidden email]>
> > Subject: Re: [R] Plotting in R
> >
> > Hi Steven,
> > Neat solution. With a lot more values on the time axis, you will be better off with something like:
> >
> > yrticks<-as.Date(as.character(seq(min(sydf$year1),max(sydf$year1),by
> > =2)),
> >  "%Y")
> > axis(1,at=yrticks,labels=format(yrticks,"%Y"))
> >
> > You probably won't need staxlab for that.
> >
> > Jim
> >
> > On Fri, Jul 12, 2019 at 11:55 AM <[hidden email]> wrote:
> > >
> > > Thanks Jim, that worked.
> > >
> > > > I expected that the axis labels would be crowded so I used the plotrix library to stagger the x-axis labels. Hope this solves your problem.
> > > I liked how that showed, not overlapping on each other. I wasn't aware of the plotrix library.
> > >
> > > In my code I was using plot_ly for visualization, because it looked nicer compared to the simple plot() function, and I have the chart in a Shiny dashboard (RMD file).
> > > I found in the meantime today some code example for plot_ly and using the same data it looks like this:
> > > sydf<-read.table(text="year1 monthday rate
> > >  1993 05-01 0.608
> > >  1994 06-01 0.622
> > >  1996 07-01 0.623
> > >  1998 08-01 0.647
> > >  2000 09-01 0.646
> > >  2002 10-01 0.625
> > >  2004 11-01 0.628
> > >  2006 12-01 0.685
> > >  2008 01-01 0.679
> > >  2010 02-01 0.595
> > >  2012 03-01 0.567
> > >  2014 04-01 0.599
> > >  2016 05-01 0.642
> > >  2018 06-01 0.685",
> > >                  header=TRUE,
> > >                  stringsAsFactors=FALSE)
> > >
> > > library(plotly)
> > > plot_ly(sydf,
> > >         x = ~year1,
> > >         y = ~rate,
> > >         type = 'scatter', mode = 'lines') %>%
> > >   layout(
> > >     xaxis = list(
> > >       ticktext = sydf$monthday,
> > >       tickvals = sydf$year1,
> > >       tickmode = "array",
> > >       tickangle = 270
> > >     ))
> > >
> > > This is where I found about the parameters:
> > > https://plot.ly/r/tick-formatting/
> > >
> > > Now my other challenge is that with my data I have a lot more values on the x axis, and they overlap even when turned vertically. I guess there must be a way of taking a number of values evenly from the list of x axis lables, and use that for the "ticktext" parameter.
> > > I thought it must be some variation of the seq(from, to, by= ). Can I use that with a list of strings?
> > >
> > > Thanks,
> > > Steven
> > >
> > > -----Original Message-----
> > > From: Jim Lemon <[hidden email]>
> > > Sent: Thursday, July 11, 2019 7:46 PM
> > > To: [hidden email]; r-help mailing list <[hidden email]>
> > > Subject: Re: [R] Plotting in R
> > >
> > > Hi Steven,
> > > It is pretty easy, but there are one or two things to watch for.
> > > First, don't use a hyphen in a field name unless you enclose it in
> > > single quotes when extracting it. I've just removed the hyphen in
> > > this
> > > example:
> > >
> > > sydf<-read.table(text="year1 monthday rate
> > >  1993 05-01 0.608
> > >  1994 06-01 0.622
> > >  1996 07-01 0.623
> > >  1998 08-01 0.647
> > >  2000 09-01 0.646
> > >  2002 10-01 0.625
> > >  2004 11-01 0.628
> > >  2006 12-01 0.685
> > >  2008 01-01 0.679
> > >  2010 02-01 0.595
> > >  2012 03-01 0.567
> > >  2014 04-01 0.599
> > >  2016 05-01 0.642
> > >  2018 06-01 0.685",
> > >  header=TRUE,
> > >  stringsAsFactors=FALSE)
> > > sydf$date<-as.Date(paste(sydf$year1,sydf$monthday),"%Y %m-%d")
> > > library(plotrix)
> > > par(mar=c(6,4,4,2))
> > > plot(sydf$date,sydf$rate,type="b",xaxt="n")
> > > staxlab(1,at=sydf$date,labels=sydf$monthday)
> > >
> > > Note that I have also added the stringsAsFactors argument to prevent monthdate being read as a factor. I expected that the axis labels would be crowded so I used the plotrix library to stagger the x-axis labels. Hope this solves your problem.
> > >
> > > Jim
> > >
> > > On Fri, Jul 12, 2019 at 1:59 AM <[hidden email]> wrote:
> > > >
> > > > Hi Jim,
> > > >
> > > > Thanks for your email.
> > > > My question was: how to change the x axis labels without changing the chart.
> > > > Or is that not possible?
> > > > Using your example, I added another column:
> > > > sydf<-read.table(text="year1 month-day rate
> > > >  1993 05-01 0.608
> > > >  1994 06-01 0.622
> > > >  1996 07-01 0.623
> > > >  1998 08-01 0.647
> > > >  2000 09-01 0.646
> > > >  2002 10-01 0.625
> > > >  2004 11-01 0.628
> > > >  2006 12-01 0.685
> > > >  2008 01-01 0.679
> > > >  2010 02-01 0.595
> > > >  2012 03-01 0.567
> > > >  2014 04-01 0.599
> > > >  2016 05-01 0.642
> > > >  2018 06-01 0.685",
> > > >                  header=TRUE)
> > > >
> > > > How can I show the column "month-day" as labels on the x axis,
> > > > but still have the plot showing the chart as rate based on year?
> > > > I tried this:
> > > > plot(sydf$year,sydf$rate,type="b",
> > > >      xlab="month-day",ylab="Rate")
> > > >
> > > > but this only changes the title of the x axis to "month-day". I
> > > > want the values on the x axis to show 05-01  06-01, etc.
> > > > Is that possible?
> > > >
> > > > Thanks,
> > > > Steven
> > > >
> > > > -----Original Message-----
> > > > From: R-help <[hidden email]> On Behalf Of Jim
> > > > Lemon
> > > > Sent: Sunday, July 7, 2019 2:59 AM
> > > > To: Steven Yen <[hidden email]>; r-help mailing list
> > > > <[hidden email]>
> > > > Subject: Re: [R] Plotting in R
> > > >
> > > > Hi Steven,
> > > > A basic plot can be displayed like this:
> > > >
> > > > sydf<-read.table(text="year rate
> > > >  1993 0.608
> > > >  1994 0.622
> > > >  1996 0.623
> > > >  1998 0.647
> > > >  2000 0.646
> > > >  2002 0.625
> > > >  2004 0.628
> > > >  2006 0.685
> > > >  2008 0.679
> > > >  2010 0.595
> > > >  2012 0.567
> > > >  2014 0.599
> > > >  2016 0.642
> > > >  2018 0.685",
> > > > header=TRUE)
> > > > plot(sydf$year,sydf$rate,type="b",
> > > > xlab="Year",ylab="Rate")
> > > >
> > > > When you add more years and rates to the data frame, the axes
> > > > will change to include the years and rates outside the ones in
> > > > your example. As some have noted, this is a very basic question.
> > > >
> > > > Jim
> > > >
> > > > On Sat, Jul 6, 2019 at 11:33 PM Steven Yen <[hidden email]> wrote:
> > > > >
> > > > > I have a data frame containing two variables: year and rate (shown below).
> > > > > Which function can I use to plot rate (y-axis) against year (x-axis)?
> > > > > There will be more columns of rate later on.
> > > > > Thank you.
> > > > >
> > > > > year rate 1 1993 0.608 2 1994 0.622 3 1996 0.623 4 1998 0.647
> > > > > 5
> > > > > 2000
> > > > > 0.646 6 2002 0.625 7 2004 0.628 8 2006 0.685 9 2008 0.679 10
> > > > > 2010
> > > > > 0.595
> > > > > 11 2012 0.567 12 2014 0.599 13 2016 0.642 14 2018 0.685
> > > > >
> > > > >
> > > > > --
> > > > > [hidden email] (S.T. Yen)
> > > > >
> > > > >
> > > > >
> > > > > ---
> > > > > This email has been checked for viruses by Avast antivirus software.
> > > > > https://www.avast.com/antivirus
> > > > >
> > > > >         [[alternative HTML version deleted]]
> > > > >
> > > > > ______________________________________________
> > > > > [hidden email] mailing list -- To UNSUBSCRIBE and more,
> > > > > see 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.
> > > >
> > > > ______________________________________________
> > > > [hidden email] mailing list -- To UNSUBSCRIBE and more,
> > > > see 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.
> > > >
> > >
> >

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Plotting in R

Jim Lemon-4
Hi Steven,
I caved in and installed plotly. Not an easy task. When I tried your
example, I got a blank HTML page displayed. I then created a plot with
your data above showing every third monthday label. If this is what
you want, maybe the way I have coded it will work in plotly.

sydf$date<-as.Date(paste(sydf$year,sydf$monthday),"%Y %m-%d")
plot(sydf$date,sydf$rate,type="l",xaxt="n")
every3rd<-seq(1,length(sydf$date),3)
axis(1,at=sydf$date[every3rd],labels=sydf$monthday[every3rd])

Jim

On Wed, Jul 17, 2019 at 4:58 AM <[hidden email]> wrote:

>
> Sorry, this still doesn't work.
>
> When I use that expression in "ticktext" parameter, it shows those every 3rd label in the beginning, and after they finished, it shows some remaining values from "year" column.
> When I tried with my real data, it was showing the original x axis values as lables, it seems like it ignored the "ticktext" parameter.
> This is my short example:
> sydf<-read.table(text="year monthday rate
>  1993 05-01 0.608
>  1994 06-01 0.622
>  1996 07-01 0.623
>  1998 08-01 0.647
>  2000 09-01 0.646
>  2002 10-01 0.625
>  2004 11-01 0.628
>  2006 12-01 0.685
>  2008 01-01 0.679
>  2010 02-01 0.595
>  2012 03-01 0.567
>  2014 04-01 0.599
>  2016 05-01 0.642
>  2018 06-01 0.685",
>                  header=TRUE,
>                  stringsAsFactors=FALSE)
>
> library(plotly)
> plot_ly(sydf,
>         x = ~year,
>         y = ~rate,
>         type = 'scatter', mode = 'lines') %>%
>   layout(
>     xaxis = list(
>       ticktext = sydf$monthday[seq(1, length(sydf$monthday), 3)],
>       tickvals = sydf$year,
>       tickmode = "array",
>       tickangle = 270
>     ))
>
> Any ideas?
>
> Thanks,
> Steven
>
> -----Original Message-----
> From: [hidden email] <[hidden email]>
> Sent: Tuesday, July 16, 2019 9:55 AM
> To: 'Jim Lemon' <[hidden email]>
> Cc: 'r-help mailing list' <[hidden email]>
> Subject: RE: [R] Plotting in R
>
> OK, I think I got this:
>
> For example every 3rd element would be:
> sydf$monthday[seq(1, length(sydf$monthday), 3)]
>
> Thanks,
> Steven
>
> -----Original Message-----
> From: [hidden email] <[hidden email]>
> Sent: Tuesday, July 16, 2019 9:39 AM
> To: 'Jim Lemon' <[hidden email]>
> Cc: 'r-help mailing list' <[hidden email]>
> Subject: RE: [R] Plotting in R
>
> Thanks Jim,
>
> Yes, I only want to show the month and day as labels, because on my chart I am actually showing 2 line charts, one from the previous year, and one from this year, to compare them, and the month and day are matching for them, but the year would be different, so it makes sense to show only month and day on the x axis.
> So it looks like I got the solution already for that part, the new challenge is that there were too many labels and overlapping on each other.
> What is the best way to evenly sample values from a long list of string values?
> So let's say my data was this overly simplified:
> sydf<-read.table(text="year monthday rate
>  1993 05-01 0.608
>  1994 06-01 0.622
>  1996 07-01 0.623
>  1998 08-01 0.647
>  2000 09-01 0.646
>  2002 10-01 0.625
>  2004 11-01 0.628
>  2006 12-01 0.685
>  2008 01-01 0.679
>  2010 02-01 0.595
>  2012 03-01 0.567
>  2014 04-01 0.599
>  2016 05-01 0.642
>  2018 06-01 0.685",
>                  header=TRUE,
>                  stringsAsFactors=FALSE)
>
> Then my x axis would be based on the values of sydf$monthday - In my case there are lots of values. How can I subset this to have only every n-th value? I guess this must be some common operation in R.
> So if I have 200 values, and I only want to show 20 of them as labels, I would calculate n = 200/20 = 10 and I want to get every 10th value in the list sydf$monthday. But the list has strings, so I cannot use "by =".
>
> Thank you,
> Steven
>
> -----Original Message-----
> From: Jim Lemon <[hidden email]>
> Sent: Friday, July 12, 2019 6:41 PM
> To: [hidden email]
> Cc: r-help mailing list <[hidden email]>
> Subject: Re: [R] Plotting in R
>
> Oh, sorry, I think I see what you have tried to do. You want yearly ticks but month-day labels. These won't mean much unless you also have the year. If you ask for a date with just the year, as.Date will give you a date in the middle of that year:
>
> as.Date("2002","%Y")
> [1] "2002-07-13"
>
> So making the labels these mid-year dates as character strings might do what you want:
>
> yrlabels<-as.character(yrticks)
>
> Jim
>
> On Sat, Jul 13, 2019 at 8:33 AM Jim Lemon <[hidden email]> wrote:
> >
> > Hi Steven,
> > year1 is a number (e.g. 1993), monthday (e.g. 05-01) is not.
> >
> > Jim
> >
> > On Fri, Jul 12, 2019 at 10:56 PM <[hidden email]> wrote:
> > >
> > > Thanks Jim.
> > >
> > > I am trying to apply this to my version with plot_ly, and couldn't make it to work.
> > > The sydf$year1 field is numeric, so the min() and max() works, but when I tried to use your formula for the sydf$monthday field I get an error:
> > > yrticks <- as.Date(as.character(seq(min(sydf$monthday),max(sydf$monthday),by=2)),
> > >                   "%Y")
> > >
> > > Error in seq.default(min(sydf$monthday), max(sydf$monthday), by = 2) :
> > >   'from' must be a finite number
> > > In addition: Warning message:
> > > In seq.default(min(sydf$monthday), max(sydf$monthday), by = 2) :
> > >   NAs introduced by coercion
> > >
> > > So I thought maybe you need to sample the tickvals not the ticktext and it would show the corresponding tick labels in the right palaces. Then I tried the way you had like this:
> > > sydf<-read.table(text="year1 monthday rate
> > >  1993 05-01 0.608
> > >  1994 06-01 0.622
> > >  1996 07-01 0.623
> > >  1998 08-01 0.647
> > >  2000 09-01 0.646
> > >  2002 10-01 0.625
> > >  2004 11-01 0.628
> > >  2006 12-01 0.685
> > >  2008 01-01 0.679
> > >  2010 02-01 0.595
> > >  2012 03-01 0.567
> > >  2014 04-01 0.599
> > >  2016 05-01 0.642
> > >  2018 06-01 0.685",
> > >                  header=TRUE,
> > >                  stringsAsFactors=FALSE)
> > >
> > > yrticks <- as.Date(as.character(seq(min(sydf$year1),max(sydf$year1),by=2)),
> > >                   "%Y")
> > >
> > > library(plotly)
> > > plot_ly(sydf,
> > >         x = ~year1,
> > >         y = ~rate,
> > >         type = 'scatter', mode = 'lines') %>%
> > >   layout(
> > >     xaxis = list(
> > >       ticktext = sydf$monthday,
> > >       tickvals = sydf$yrticks,
> > >       tickmode = "array",
> > >       tickangle = 270
> > >     ))
> > >
> > > But the chart didn't show any tick labels.
> > > I guess I need to sample sydf$monthday, right? Because that's what I want to show as tick labels. But the problem is that monthday is string, and can't use a value for "by=", maybe I need to sample somehow by the index position.
> > >
> > > Thanks,
> > > Steven
> > >
> > > -----Original Message-----
> > > From: Jim Lemon <[hidden email]>
> > > Sent: Thursday, July 11, 2019 10:41 PM
> > > To: [hidden email]
> > > Cc: r-help mailing list <[hidden email]>
> > > Subject: Re: [R] Plotting in R
> > >
> > > Hi Steven,
> > > Neat solution. With a lot more values on the time axis, you will be better off with something like:
> > >
> > > yrticks<-as.Date(as.character(seq(min(sydf$year1),max(sydf$year1),by
> > > =2)),
> > >  "%Y")
> > > axis(1,at=yrticks,labels=format(yrticks,"%Y"))
> > >
> > > You probably won't need staxlab for that.
> > >
> > > Jim
> > >
> > > On Fri, Jul 12, 2019 at 11:55 AM <[hidden email]> wrote:
> > > >
> > > > Thanks Jim, that worked.
> > > >
> > > > > I expected that the axis labels would be crowded so I used the plotrix library to stagger the x-axis labels. Hope this solves your problem.
> > > > I liked how that showed, not overlapping on each other. I wasn't aware of the plotrix library.
> > > >
> > > > In my code I was using plot_ly for visualization, because it looked nicer compared to the simple plot() function, and I have the chart in a Shiny dashboard (RMD file).
> > > > I found in the meantime today some code example for plot_ly and using the same data it looks like this:
> > > > sydf<-read.table(text="year1 monthday rate
> > > >  1993 05-01 0.608
> > > >  1994 06-01 0.622
> > > >  1996 07-01 0.623
> > > >  1998 08-01 0.647
> > > >  2000 09-01 0.646
> > > >  2002 10-01 0.625
> > > >  2004 11-01 0.628
> > > >  2006 12-01 0.685
> > > >  2008 01-01 0.679
> > > >  2010 02-01 0.595
> > > >  2012 03-01 0.567
> > > >  2014 04-01 0.599
> > > >  2016 05-01 0.642
> > > >  2018 06-01 0.685",
> > > >                  header=TRUE,
> > > >                  stringsAsFactors=FALSE)
> > > >
> > > > library(plotly)
> > > > plot_ly(sydf,
> > > >         x = ~year1,
> > > >         y = ~rate,
> > > >         type = 'scatter', mode = 'lines') %>%
> > > >   layout(
> > > >     xaxis = list(
> > > >       ticktext = sydf$monthday,
> > > >       tickvals = sydf$year1,
> > > >       tickmode = "array",
> > > >       tickangle = 270
> > > >     ))
> > > >
> > > > This is where I found about the parameters:
> > > > https://plot.ly/r/tick-formatting/
> > > >
> > > > Now my other challenge is that with my data I have a lot more values on the x axis, and they overlap even when turned vertically. I guess there must be a way of taking a number of values evenly from the list of x axis lables, and use that for the "ticktext" parameter.
> > > > I thought it must be some variation of the seq(from, to, by= ). Can I use that with a list of strings?
> > > >
> > > > Thanks,
> > > > Steven
> > > >
> > > > -----Original Message-----
> > > > From: Jim Lemon <[hidden email]>
> > > > Sent: Thursday, July 11, 2019 7:46 PM
> > > > To: [hidden email]; r-help mailing list <[hidden email]>
> > > > Subject: Re: [R] Plotting in R
> > > >
> > > > Hi Steven,
> > > > It is pretty easy, but there are one or two things to watch for.
> > > > First, don't use a hyphen in a field name unless you enclose it in
> > > > single quotes when extracting it. I've just removed the hyphen in
> > > > this
> > > > example:
> > > >
> > > > sydf<-read.table(text="year1 monthday rate
> > > >  1993 05-01 0.608
> > > >  1994 06-01 0.622
> > > >  1996 07-01 0.623
> > > >  1998 08-01 0.647
> > > >  2000 09-01 0.646
> > > >  2002 10-01 0.625
> > > >  2004 11-01 0.628
> > > >  2006 12-01 0.685
> > > >  2008 01-01 0.679
> > > >  2010 02-01 0.595
> > > >  2012 03-01 0.567
> > > >  2014 04-01 0.599
> > > >  2016 05-01 0.642
> > > >  2018 06-01 0.685",
> > > >  header=TRUE,
> > > >  stringsAsFactors=FALSE)
> > > > sydf$date<-as.Date(paste(sydf$year1,sydf$monthday),"%Y %m-%d")
> > > > library(plotrix)
> > > > par(mar=c(6,4,4,2))
> > > > plot(sydf$date,sydf$rate,type="b",xaxt="n")
> > > > staxlab(1,at=sydf$date,labels=sydf$monthday)
> > > >
> > > > Note that I have also added the stringsAsFactors argument to prevent monthdate being read as a factor. I expected that the axis labels would be crowded so I used the plotrix library to stagger the x-axis labels. Hope this solves your problem.
> > > >
> > > > Jim
> > > >
> > > > On Fri, Jul 12, 2019 at 1:59 AM <[hidden email]> wrote:
> > > > >
> > > > > Hi Jim,
> > > > >
> > > > > Thanks for your email.
> > > > > My question was: how to change the x axis labels without changing the chart.
> > > > > Or is that not possible?
> > > > > Using your example, I added another column:
> > > > > sydf<-read.table(text="year1 month-day rate
> > > > >  1993 05-01 0.608
> > > > >  1994 06-01 0.622
> > > > >  1996 07-01 0.623
> > > > >  1998 08-01 0.647
> > > > >  2000 09-01 0.646
> > > > >  2002 10-01 0.625
> > > > >  2004 11-01 0.628
> > > > >  2006 12-01 0.685
> > > > >  2008 01-01 0.679
> > > > >  2010 02-01 0.595
> > > > >  2012 03-01 0.567
> > > > >  2014 04-01 0.599
> > > > >  2016 05-01 0.642
> > > > >  2018 06-01 0.685",
> > > > >                  header=TRUE)
> > > > >
> > > > > How can I show the column "month-day" as labels on the x axis,
> > > > > but still have the plot showing the chart as rate based on year?
> > > > > I tried this:
> > > > > plot(sydf$year,sydf$rate,type="b",
> > > > >      xlab="month-day",ylab="Rate")
> > > > >
> > > > > but this only changes the title of the x axis to "month-day". I
> > > > > want the values on the x axis to show 05-01  06-01, etc.
> > > > > Is that possible?
> > > > >
> > > > > Thanks,
> > > > > Steven
> > > > >
> > > > > -----Original Message-----
> > > > > From: R-help <[hidden email]> On Behalf Of Jim
> > > > > Lemon
> > > > > Sent: Sunday, July 7, 2019 2:59 AM
> > > > > To: Steven Yen <[hidden email]>; r-help mailing list
> > > > > <[hidden email]>
> > > > > Subject: Re: [R] Plotting in R
> > > > >
> > > > > Hi Steven,
> > > > > A basic plot can be displayed like this:
> > > > >
> > > > > sydf<-read.table(text="year rate
> > > > >  1993 0.608
> > > > >  1994 0.622
> > > > >  1996 0.623
> > > > >  1998 0.647
> > > > >  2000 0.646
> > > > >  2002 0.625
> > > > >  2004 0.628
> > > > >  2006 0.685
> > > > >  2008 0.679
> > > > >  2010 0.595
> > > > >  2012 0.567
> > > > >  2014 0.599
> > > > >  2016 0.642
> > > > >  2018 0.685",
> > > > > header=TRUE)
> > > > > plot(sydf$year,sydf$rate,type="b",
> > > > > xlab="Year",ylab="Rate")
> > > > >
> > > > > When you add more years and rates to the data frame, the axes
> > > > > will change to include the years and rates outside the ones in
> > > > > your example. As some have noted, this is a very basic question.
> > > > >
> > > > > Jim
> > > > >
> > > > > On Sat, Jul 6, 2019 at 11:33 PM Steven Yen <[hidden email]> wrote:
> > > > > >
> > > > > > I have a data frame containing two variables: year and rate (shown below).
> > > > > > Which function can I use to plot rate (y-axis) against year (x-axis)?
> > > > > > There will be more columns of rate later on.
> > > > > > Thank you.
> > > > > >
> > > > > > year rate 1 1993 0.608 2 1994 0.622 3 1996 0.623 4 1998 0.647
> > > > > > 5
> > > > > > 2000
> > > > > > 0.646 6 2002 0.625 7 2004 0.628 8 2006 0.685 9 2008 0.679 10
> > > > > > 2010
> > > > > > 0.595
> > > > > > 11 2012 0.567 12 2014 0.599 13 2016 0.642 14 2018 0.685
> > > > > >
> > > > > >
> > > > > > --
> > > > > > [hidden email] (S.T. Yen)
> > > > > >
> > > > > >
> > > > > >
> > > > > > ---
> > > > > > This email has been checked for viruses by Avast antivirus software.
> > > > > > https://www.avast.com/antivirus
> > > > > >
> > > > > >         [[alternative HTML version deleted]]
> > > > > >
> > > > > > ______________________________________________
> > > > > > [hidden email] mailing list -- To UNSUBSCRIBE and more,
> > > > > > see 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.
> > > > >
> > > > > ______________________________________________
> > > > > [hidden email] mailing list -- To UNSUBSCRIBE and more,
> > > > > see 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.
> > > > >
> > > >
> > >
>
>
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Plotting in R

nstefi
Thanks Jim,

I appreciate that you spend so much time helping me on this.
I translated your code to use my plot_ly function this way, I'm not sure if this is correct, but tried to match your x and y axis and the labels. Here is the complete code including data below, but it seems like my code "ticktext =" is ignored, and it still shows the years in the x axis as labels:

sydf<-read.table(text="year monthday rate
 1993 05-01 0.608
 1994 06-01 0.622
 1996 07-01 0.623
 1998 08-01 0.647
 2000 09-01 0.646
 2002 10-01 0.625
 2004 11-01 0.628
 2006 12-01 0.685
 2008 01-01 0.679
 2010 02-01 0.595
 2012 03-01 0.567
 2014 04-01 0.599
 2016 05-01 0.642
 2018 06-01 0.685",
                 header=TRUE,
                 stringsAsFactors=FALSE)

sydf$date<-as.Date(paste(sydf$year,sydf$monthday),"%Y %m-%d")
every3rd<-seq(1,length(sydf$date),3)

library(plotly)
plot_ly(sydf,
        x = ~date,
        y = ~rate,
        type = 'scatter', mode = 'lines') %>%
  layout(
    xaxis = list(
      ticktext = sydf$monthday[every3rd],
      tickvals = sydf$year,
      tickmode = "array",
      tickangle = 270
    ))


Thanks,
Steven

-----Original Message-----
From: Jim Lemon <[hidden email]>
Sent: Thursday, July 18, 2019 5:25 AM
To: [hidden email]
Cc: r-help mailing list <[hidden email]>
Subject: Re: [R] Plotting in R

Hi Steven,
I caved in and installed plotly. Not an easy task. When I tried your example, I got a blank HTML page displayed. I then created a plot with your data above showing every third monthday label. If this is what you want, maybe the way I have coded it will work in plotly.

sydf$date<-as.Date(paste(sydf$year,sydf$monthday),"%Y %m-%d")
plot(sydf$date,sydf$rate,type="l",xaxt="n")
every3rd<-seq(1,length(sydf$date),3)
axis(1,at=sydf$date[every3rd],labels=sydf$monthday[every3rd])

Jim

On Wed, Jul 17, 2019 at 4:58 AM <[hidden email]> wrote:

>
> Sorry, this still doesn't work.
>
> When I use that expression in "ticktext" parameter, it shows those every 3rd label in the beginning, and after they finished, it shows some remaining values from "year" column.
> When I tried with my real data, it was showing the original x axis values as lables, it seems like it ignored the "ticktext" parameter.
> This is my short example:
> sydf<-read.table(text="year monthday rate
>  1993 05-01 0.608
>  1994 06-01 0.622
>  1996 07-01 0.623
>  1998 08-01 0.647
>  2000 09-01 0.646
>  2002 10-01 0.625
>  2004 11-01 0.628
>  2006 12-01 0.685
>  2008 01-01 0.679
>  2010 02-01 0.595
>  2012 03-01 0.567
>  2014 04-01 0.599
>  2016 05-01 0.642
>  2018 06-01 0.685",
>                  header=TRUE,
>                  stringsAsFactors=FALSE)
>
> library(plotly)
> plot_ly(sydf,
>         x = ~year,
>         y = ~rate,
>         type = 'scatter', mode = 'lines') %>%
>   layout(
>     xaxis = list(
>       ticktext = sydf$monthday[seq(1, length(sydf$monthday), 3)],
>       tickvals = sydf$year,
>       tickmode = "array",
>       tickangle = 270
>     ))
>
> Any ideas?
>
> Thanks,
> Steven
>
> -----Original Message-----
> From: [hidden email] <[hidden email]>
> Sent: Tuesday, July 16, 2019 9:55 AM
> To: 'Jim Lemon' <[hidden email]>
> Cc: 'r-help mailing list' <[hidden email]>
> Subject: RE: [R] Plotting in R
>
> OK, I think I got this:
>
> For example every 3rd element would be:
> sydf$monthday[seq(1, length(sydf$monthday), 3)]
>
> Thanks,
> Steven
>
> -----Original Message-----
> From: [hidden email] <[hidden email]>
> Sent: Tuesday, July 16, 2019 9:39 AM
> To: 'Jim Lemon' <[hidden email]>
> Cc: 'r-help mailing list' <[hidden email]>
> Subject: RE: [R] Plotting in R
>
> Thanks Jim,
>
> Yes, I only want to show the month and day as labels, because on my chart I am actually showing 2 line charts, one from the previous year, and one from this year, to compare them, and the month and day are matching for them, but the year would be different, so it makes sense to show only month and day on the x axis.
> So it looks like I got the solution already for that part, the new challenge is that there were too many labels and overlapping on each other.
> What is the best way to evenly sample values from a long list of string values?
> So let's say my data was this overly simplified:
> sydf<-read.table(text="year monthday rate
>  1993 05-01 0.608
>  1994 06-01 0.622
>  1996 07-01 0.623
>  1998 08-01 0.647
>  2000 09-01 0.646
>  2002 10-01 0.625
>  2004 11-01 0.628
>  2006 12-01 0.685
>  2008 01-01 0.679
>  2010 02-01 0.595
>  2012 03-01 0.567
>  2014 04-01 0.599
>  2016 05-01 0.642
>  2018 06-01 0.685",
>                  header=TRUE,
>                  stringsAsFactors=FALSE)
>
> Then my x axis would be based on the values of sydf$monthday - In my case there are lots of values. How can I subset this to have only every n-th value? I guess this must be some common operation in R.
> So if I have 200 values, and I only want to show 20 of them as labels, I would calculate n = 200/20 = 10 and I want to get every 10th value in the list sydf$monthday. But the list has strings, so I cannot use "by =".
>
> Thank you,
> Steven
>
> -----Original Message-----
> From: Jim Lemon <[hidden email]>
> Sent: Friday, July 12, 2019 6:41 PM
> To: [hidden email]
> Cc: r-help mailing list <[hidden email]>
> Subject: Re: [R] Plotting in R
>
> Oh, sorry, I think I see what you have tried to do. You want yearly ticks but month-day labels. These won't mean much unless you also have the year. If you ask for a date with just the year, as.Date will give you a date in the middle of that year:
>
> as.Date("2002","%Y")
> [1] "2002-07-13"
>
> So making the labels these mid-year dates as character strings might do what you want:
>
> yrlabels<-as.character(yrticks)
>
> Jim
>
> On Sat, Jul 13, 2019 at 8:33 AM Jim Lemon <[hidden email]> wrote:
> >
> > Hi Steven,
> > year1 is a number (e.g. 1993), monthday (e.g. 05-01) is not.
> >
> > Jim
> >
> > On Fri, Jul 12, 2019 at 10:56 PM <[hidden email]> wrote:
> > >
> > > Thanks Jim.
> > >
> > > I am trying to apply this to my version with plot_ly, and couldn't make it to work.
> > > The sydf$year1 field is numeric, so the min() and max() works, but when I tried to use your formula for the sydf$monthday field I get an error:
> > > yrticks <- as.Date(as.character(seq(min(sydf$monthday),max(sydf$monthday),by=2)),
> > >                   "%Y")
> > >
> > > Error in seq.default(min(sydf$monthday), max(sydf$monthday), by = 2) :
> > >   'from' must be a finite number
> > > In addition: Warning message:
> > > In seq.default(min(sydf$monthday), max(sydf$monthday), by = 2) :
> > >   NAs introduced by coercion
> > >
> > > So I thought maybe you need to sample the tickvals not the ticktext and it would show the corresponding tick labels in the right palaces. Then I tried the way you had like this:
> > > sydf<-read.table(text="year1 monthday rate
> > >  1993 05-01 0.608
> > >  1994 06-01 0.622
> > >  1996 07-01 0.623
> > >  1998 08-01 0.647
> > >  2000 09-01 0.646
> > >  2002 10-01 0.625
> > >  2004 11-01 0.628
> > >  2006 12-01 0.685
> > >  2008 01-01 0.679
> > >  2010 02-01 0.595
> > >  2012 03-01 0.567
> > >  2014 04-01 0.599
> > >  2016 05-01 0.642
> > >  2018 06-01 0.685",
> > >                  header=TRUE,
> > >                  stringsAsFactors=FALSE)
> > >
> > > yrticks <- as.Date(as.character(seq(min(sydf$year1),max(sydf$year1),by=2)),
> > >                   "%Y")
> > >
> > > library(plotly)
> > > plot_ly(sydf,
> > >         x = ~year1,
> > >         y = ~rate,
> > >         type = 'scatter', mode = 'lines') %>%
> > >   layout(
> > >     xaxis = list(
> > >       ticktext = sydf$monthday,
> > >       tickvals = sydf$yrticks,
> > >       tickmode = "array",
> > >       tickangle = 270
> > >     ))
> > >
> > > But the chart didn't show any tick labels.
> > > I guess I need to sample sydf$monthday, right? Because that's what I want to show as tick labels. But the problem is that monthday is string, and can't use a value for "by=", maybe I need to sample somehow by the index position.
> > >
> > > Thanks,
> > > Steven
> > >
> > > -----Original Message-----
> > > From: Jim Lemon <[hidden email]>
> > > Sent: Thursday, July 11, 2019 10:41 PM
> > > To: [hidden email]
> > > Cc: r-help mailing list <[hidden email]>
> > > Subject: Re: [R] Plotting in R
> > >
> > > Hi Steven,
> > > Neat solution. With a lot more values on the time axis, you will be better off with something like:
> > >
> > > yrticks<-as.Date(as.character(seq(min(sydf$year1),max(sydf$year1),
> > > by
> > > =2)),
> > >  "%Y")
> > > axis(1,at=yrticks,labels=format(yrticks,"%Y"))
> > >
> > > You probably won't need staxlab for that.
> > >
> > > Jim
> > >
> > > On Fri, Jul 12, 2019 at 11:55 AM <[hidden email]> wrote:
> > > >
> > > > Thanks Jim, that worked.
> > > >
> > > > > I expected that the axis labels would be crowded so I used the plotrix library to stagger the x-axis labels. Hope this solves your problem.
> > > > I liked how that showed, not overlapping on each other. I wasn't aware of the plotrix library.
> > > >
> > > > In my code I was using plot_ly for visualization, because it looked nicer compared to the simple plot() function, and I have the chart in a Shiny dashboard (RMD file).
> > > > I found in the meantime today some code example for plot_ly and using the same data it looks like this:
> > > > sydf<-read.table(text="year1 monthday rate
> > > >  1993 05-01 0.608
> > > >  1994 06-01 0.622
> > > >  1996 07-01 0.623
> > > >  1998 08-01 0.647
> > > >  2000 09-01 0.646
> > > >  2002 10-01 0.625
> > > >  2004 11-01 0.628
> > > >  2006 12-01 0.685
> > > >  2008 01-01 0.679
> > > >  2010 02-01 0.595
> > > >  2012 03-01 0.567
> > > >  2014 04-01 0.599
> > > >  2016 05-01 0.642
> > > >  2018 06-01 0.685",
> > > >                  header=TRUE,
> > > >                  stringsAsFactors=FALSE)
> > > >
> > > > library(plotly)
> > > > plot_ly(sydf,
> > > >         x = ~year1,
> > > >         y = ~rate,
> > > >         type = 'scatter', mode = 'lines') %>%
> > > >   layout(
> > > >     xaxis = list(
> > > >       ticktext = sydf$monthday,
> > > >       tickvals = sydf$year1,
> > > >       tickmode = "array",
> > > >       tickangle = 270
> > > >     ))
> > > >
> > > > This is where I found about the parameters:
> > > > https://plot.ly/r/tick-formatting/
> > > >
> > > > Now my other challenge is that with my data I have a lot more values on the x axis, and they overlap even when turned vertically. I guess there must be a way of taking a number of values evenly from the list of x axis lables, and use that for the "ticktext" parameter.
> > > > I thought it must be some variation of the seq(from, to, by= ). Can I use that with a list of strings?
> > > >
> > > > Thanks,
> > > > Steven
> > > >
> > > > -----Original Message-----
> > > > From: Jim Lemon <[hidden email]>
> > > > Sent: Thursday, July 11, 2019 7:46 PM
> > > > To: [hidden email]; r-help mailing list <[hidden email]>
> > > > Subject: Re: [R] Plotting in R
> > > >
> > > > Hi Steven,
> > > > It is pretty easy, but there are one or two things to watch for.
> > > > First, don't use a hyphen in a field name unless you enclose it
> > > > in single quotes when extracting it. I've just removed the
> > > > hyphen in this
> > > > example:
> > > >
> > > > sydf<-read.table(text="year1 monthday rate
> > > >  1993 05-01 0.608
> > > >  1994 06-01 0.622
> > > >  1996 07-01 0.623
> > > >  1998 08-01 0.647
> > > >  2000 09-01 0.646
> > > >  2002 10-01 0.625
> > > >  2004 11-01 0.628
> > > >  2006 12-01 0.685
> > > >  2008 01-01 0.679
> > > >  2010 02-01 0.595
> > > >  2012 03-01 0.567
> > > >  2014 04-01 0.599
> > > >  2016 05-01 0.642
> > > >  2018 06-01 0.685",
> > > >  header=TRUE,
> > > >  stringsAsFactors=FALSE)
> > > > sydf$date<-as.Date(paste(sydf$year1,sydf$monthday),"%Y %m-%d")
> > > > library(plotrix)
> > > > par(mar=c(6,4,4,2))
> > > > plot(sydf$date,sydf$rate,type="b",xaxt="n")
> > > > staxlab(1,at=sydf$date,labels=sydf$monthday)
> > > >
> > > > Note that I have also added the stringsAsFactors argument to prevent monthdate being read as a factor. I expected that the axis labels would be crowded so I used the plotrix library to stagger the x-axis labels. Hope this solves your problem.
> > > >
> > > > Jim
> > > >
> > > > On Fri, Jul 12, 2019 at 1:59 AM <[hidden email]> wrote:
> > > > >
> > > > > Hi Jim,
> > > > >
> > > > > Thanks for your email.
> > > > > My question was: how to change the x axis labels without changing the chart.
> > > > > Or is that not possible?
> > > > > Using your example, I added another column:
> > > > > sydf<-read.table(text="year1 month-day rate
> > > > >  1993 05-01 0.608
> > > > >  1994 06-01 0.622
> > > > >  1996 07-01 0.623
> > > > >  1998 08-01 0.647
> > > > >  2000 09-01 0.646
> > > > >  2002 10-01 0.625
> > > > >  2004 11-01 0.628
> > > > >  2006 12-01 0.685
> > > > >  2008 01-01 0.679
> > > > >  2010 02-01 0.595
> > > > >  2012 03-01 0.567
> > > > >  2014 04-01 0.599
> > > > >  2016 05-01 0.642
> > > > >  2018 06-01 0.685",
> > > > >                  header=TRUE)
> > > > >
> > > > > How can I show the column "month-day" as labels on the x axis,
> > > > > but still have the plot showing the chart as rate based on year?
> > > > > I tried this:
> > > > > plot(sydf$year,sydf$rate,type="b",
> > > > >      xlab="month-day",ylab="Rate")
> > > > >
> > > > > but this only changes the title of the x axis to "month-day".
> > > > > I want the values on the x axis to show 05-01  06-01, etc.
> > > > > Is that possible?
> > > > >
> > > > > Thanks,
> > > > > Steven
> > > > >
> > > > > -----Original Message-----
> > > > > From: R-help <[hidden email]> On Behalf Of Jim
> > > > > Lemon
> > > > > Sent: Sunday, July 7, 2019 2:59 AM
> > > > > To: Steven Yen <[hidden email]>; r-help mailing list
> > > > > <[hidden email]>
> > > > > Subject: Re: [R] Plotting in R
> > > > >
> > > > > Hi Steven,
> > > > > A basic plot can be displayed like this:
> > > > >
> > > > > sydf<-read.table(text="year rate
> > > > >  1993 0.608
> > > > >  1994 0.622
> > > > >  1996 0.623
> > > > >  1998 0.647
> > > > >  2000 0.646
> > > > >  2002 0.625
> > > > >  2004 0.628
> > > > >  2006 0.685
> > > > >  2008 0.679
> > > > >  2010 0.595
> > > > >  2012 0.567
> > > > >  2014 0.599
> > > > >  2016 0.642
> > > > >  2018 0.685",
> > > > > header=TRUE)
> > > > > plot(sydf$year,sydf$rate,type="b",
> > > > > xlab="Year",ylab="Rate")
> > > > >
> > > > > When you add more years and rates to the data frame, the axes
> > > > > will change to include the years and rates outside the ones in
> > > > > your example. As some have noted, this is a very basic question.
> > > > >
> > > > > Jim
> > > > >
> > > > > On Sat, Jul 6, 2019 at 11:33 PM Steven Yen <[hidden email]> wrote:
> > > > > >
> > > > > > I have a data frame containing two variables: year and rate (shown below).
> > > > > > Which function can I use to plot rate (y-axis) against year (x-axis)?
> > > > > > There will be more columns of rate later on.
> > > > > > Thank you.
> > > > > >
> > > > > > year rate 1 1993 0.608 2 1994 0.622 3 1996 0.623 4 1998
> > > > > > 0.647
> > > > > > 5
> > > > > > 2000
> > > > > > 0.646 6 2002 0.625 7 2004 0.628 8 2006 0.685 9 2008 0.679 10
> > > > > > 2010
> > > > > > 0.595
> > > > > > 11 2012 0.567 12 2014 0.599 13 2016 0.642 14 2018 0.685
> > > > > >
> > > > > >
> > > > > > --
> > > > > > [hidden email] (S.T. Yen)
> > > > > >
> > > > > >
> > > > > >
> > > > > > ---
> > > > > > This email has been checked for viruses by Avast antivirus software.
> > > > > > https://www.avast.com/antivirus
> > > > > >
> > > > > >         [[alternative HTML version deleted]]
> > > > > >
> > > > > > ______________________________________________
> > > > > > [hidden email] mailing list -- To UNSUBSCRIBE and
> > > > > > more, see 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.
> > > > >
> > > > > ______________________________________________
> > > > > [hidden email] mailing list -- To UNSUBSCRIBE and more,
> > > > > see 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.
> > > > >
> > > >
> > >
>
>
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
12