[R] Plotting in R
@steii m@iii@g oii gm@ii@com
@steii m@iii@g oii gm@ii@com
Fri Jul 12 14:56:16 CEST 2019
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 <drjimlemon using gmail.com>
Sent: Thursday, July 11, 2019 10:41 PM
To: nstefi using gmail.com
Cc: r-help mailing list <r-help using r-project.org>
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 <nstefi using gmail.com> 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 <drjimlemon using gmail.com>
> Sent: Thursday, July 11, 2019 7:46 PM
> To: nstefi using gmail.com; r-help mailing list <r-help using r-project.org>
> 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 <nstefi using gmail.com> 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 <r-help-bounces using r-project.org> On Behalf Of Jim Lemon
> > Sent: Sunday, July 7, 2019 2:59 AM
> > To: Steven Yen <styen using ntu.edu.tw>; r-help mailing list
> > <r-help using r-project.org>
> > 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 <styen using ntu.edu.tw> 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
> > >
> > >
> > > --
> > > styen using ntu.edu.tw (S.T. Yen)
> > >
> > >
> > >
> > > ---
> > > This email has been checked for viruses by Avast antivirus software.
> > > https://www.avast.com/antivirus
> > >
> > > [[alternative HTML version deleted]]
> > >
> > > ______________________________________________
> > > R-help using r-project.org 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.
> >
> > ______________________________________________
> > R-help using r-project.org 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.
> >
>
More information about the R-help
mailing list