R Script Modification Questions

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

R Script Modification Questions

Stephen P. Molnar
Thanks to the kind folks on this list, this is an elegant replacement
for the clumsy R script that I that I wrote.




However, I do have a few changes that I would like to make. The problem
is that while I know how to make changes in Python, I am still bumbling
around in R Code.

The day-to-day changes in the data are in the cvs file downloaded in
line 11 of the code.

What I would like to do is use linetype, rather than color, in line 27.

The date in the title of the plot , line 33, is the max value of the
date in in line 14 and I would like to use that rather than edit the
Script every time the date changes.

I'd appreciate assistance in making these changes.

Thanks in advance,.

--
Stephen P. Molnar, Ph.D.
www.molecular-modeling.net
614.312.7528 (c)
Skype:  smolnar1

______________________________________________
[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: R Script Modification Questions

Bert Gunter-2
1. Generally you should make your posts self-contained -- I see no line
14's or 33's or whatever. Note that you did not continue the old thread
either. I have no desire to go poking around in previous threads (others
certainly may!).

2. Re: max of dates.

 max(as.Date(c("1976-04-07","1982-02-22")))
[1] "1982-02-22"

You seem to be asking basic questions that indicate a need to spend some
time with R tutorials, in this case S3 methods and dates. See ?"date-time"
for a terse but dense explanation; numerous web tutorials are available
that are more suitable for most of us learning new R or R package features.

Cheers,
Bert

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 Tue, Aug 18, 2020 at 10:10 AM Stephen P. Molnar <[hidden email]>
wrote:

> Thanks to the kind folks on this list, this is an elegant replacement
> for the clumsy R script that I that I wrote.
>
>
>
>
> However, I do have a few changes that I would like to make. The problem
> is that while I know how to make changes in Python, I am still bumbling
> around in R Code.
>
> The day-to-day changes in the data are in the cvs file downloaded in
> line 11 of the code.
>
> What I would like to do is use linetype, rather than color, in line 27.
>
> The date in the title of the plot , line 33, is the max value of the
> date in in line 14 and I would like to use that rather than edit the
> Script every time the date changes.
>
> I'd appreciate assistance in making these changes.
>
> Thanks in advance,.
>
> --
> Stephen P. Molnar, Ph.D.
> www.molecular-modeling.net
> 614.312.7528 (c)
> Skype:  smolnar1
>
> ______________________________________________
> [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: R Script Modification Questions

Rasmus Liland-3
Dear Stephen,  I reply you inline:

On 2020-08-18 11:09 -0700, Bert Gunter wrote:
| On Tue, Aug 18, 2020 at 10:10 AM Stephen P. Molnar <[hidden email]> wrote:
| |
| | Thanks to the kind folks on this
| | list, this is an elegant replacement
| | for the clumsy R script that I that
| | I wrote.
| |
| |
| |
| |
|
| 1. Generally you should make your
|    posts self-contained -- I see no
|    line 14's or 33's or whatever. Note
|    that you did not continue the old
|    thread either. I have no desire to
|    go poking around in previous
|    threads (others certainly may!).

There were a bunch of blank lines in
this area.  Maybe you put your script
here in a picture or something which
disappeared somewhere in the guts of the
stat.ethz.ch mailfilter.

| | However, I do have a few changes
| | that I would like to make. The
| | problem is that while I know how to
| | make changes in Python, I am still
| | bumbling around in R Code.

I came from python to R some years ago,
and it is easier I think, if you spend
some time on it.

| | The day-to-day changes in the data
| | are in the cvs file downloaded in
| | line 11 of the code.

Good, now provide line 11.  

| | What I would like to do is use
| | linetype, rather than color, in line
| | 27.

You can specify linetype as an argument
to ggplot2::aes instead of color.  
Perhaps try this, if it is what you did.  
This should also be easy in normal plot,
see ?par.

| | The date in the title of the plot ,
| | line 33, is the max value of the
| | date in in line 14 and I would like
| | to use that rather than edit the
| | Script every time the date changes.
|
| 2. Re: max of dates.
|
|  max(as.Date(c("1976-04-07","1982-02-22")))
| [1] "1982-02-22"
|
| You seem to be asking basic questions
| that indicate a need to spend some
| time with R tutorials, in this case S3
| methods and dates. See ?"date-time"
| for a terse but dense explanation;
| numerous web tutorials are available
| that are more suitable for most of us
| learning new R or R package features.

Best,
Rasmus

______________________________________________
[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.

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: R Script Modification Questions

Jim Lemon-4
In reply to this post by Stephen P. Molnar
Okay, I can't help much with the ggplot stuff so forget the "lty="
argument for that is base graphics. However, you may get away with

ggtitle(paste0("COVID-19 Tests in Ohio \n(",date[length(date]),")"))+

I don't know whether the tidy* stuff handles indexing in the same way as base R.

Jim

On Wed, Aug 19, 2020 at 12:20 PM Stephen P. Molnar
<[hidden email]> wrote:

>
> Jim
>
> Thanks for your note.
>
> This is what didn't make it through:
>
> library(tidyverse)
> library(lubridate)
>
> datO <- read.csv("https://api.covidtracking.com/v1/states/oh/daily.csv")
> <- line 14
> datO[ ,1] <- ymd(datO[ ,1])
>
> dfO <- tibble::as_tibble(data.frame(date = datO[ ,"date"],
>                                      positive = datO[ ,"positive"],
>                                      negative = datO[ ,"negative"],
>                                      total = datO[ ,"total"]))
>
> dfO %>%
>    pivot_longer(
>      cols = -date,
>      names_to = "cases",
>      values_to = "count"
>    ) %>%
>    mutate(cases = factor(cases, levels = c("positive", "negative",
> "total"))) %>%
>    ggplot(aes(date, count, color = cases)) +
>    geom_line() +
>    scale_color_manual(name = "Test",
>                       labels = c("Positive", "Negative", "Total"),
>                       values = c("red", "blue", "green")) +
>    ylim(0, 2000000) +
>    labs(x = "Date", y = "Number of Tests")+
>    ggtitle("COVID-19 Tests in Ohio \n (8/17/20)")+   <- line 33
>    theme_bw() +
>    theme(axis.text.x = element_text(angle = 30, hjust = 1),
>          plot.title = element_text(hjust = 0.5))
>
>              Steve
>
> On 08/18/2020 07:55 PM, Jim Lemon wrote:
> > Hi Stephen,
> > I think something went amiss with your email as there are a few blank
> > lines where I think you meant to paste a code fragment. Line type is
> > specified by the "lty" argument and to stick things together for your
> > plot title, you may want something like this:
> >
> > ..
> > main<-paste("My plot title for",dates[length(dates)]).
> > ..
> >
> > Jim
> >
> > On Wed, Aug 19, 2020 at 3:09 AM Stephen P. Molnar
> > <[hidden email]> wrote:
> >> Thanks to the kind folks on this list, this is an elegant replacement
> >> for the clumsy R script that I that I wrote.
> >>
> >>
> >>
> >>
> >> However, I do have a few changes that I would like to make. The problem
> >> is that while I know how to make changes in Python, I am still bumbling
> >> around in R Code.
> >>
> >> The day-to-day changes in the data are in the cvs file downloaded in
> >> line 11 of the code.
> >>
> >> What I would like to do is use linetype, rather than color, in line 27.
> >>
> >> The date in the title of the plot , line 33, is the max value of the
> >> date in in line 14 and I would like to use that rather than edit the
> >> Script every time the date changes.
> >>
> >> I'd appreciate assistance in making these changes.
> >>
> >> Thanks in advance,.
> >>
> >> --
> >> Stephen P. Molnar, Ph.D.
> >> www.molecular-modeling.net
> >> 614.312.7528 (c)
> >> Skype:  smolnar1
> >>
> >> ______________________________________________
> >> [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.
>
> --
> Stephen P. Molnar, Ph.D.
> www.molecular-modeling.net
> 614.312.7528 (c)
> Skype:  smolnar1
>

______________________________________________
[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: R Script Modification Questions

Rasmus Liland-3
Dear Stephen,

I answer inline:

On 2020-08-19 12:57 +1000, Jim Lemon wrote:
| On Wed, Aug 19, 2020 at 3:09 AM Stephen P. Molnar <[hidden email]> wrote:
| |
| | What I would like to do is use
| | linetype, rather than color, in line
| | 27.

You need to specify linetype instead of
color in ggplot::aes, like so

        ggplot2::aes(
          x=date,
          y=count,
          linetype=cases)

and change
scale_color_manual to
scale_linetype_manual and its values
like so

          ggplot2::scale_linetype_manual(
            name = "Test",
            labels = levels,
            values =
              c("dotted",
                "dashed",
                "solid"))

| | The date in the title of the plot ,
| | line 33, is the max value of the
| | date in in line 14 and I would like
| | to use that rather than edit the
| | Script every time the date changes.
|
| Okay, I can't help much with the
| ggplot stuff so forget the "lty="
| argument for that is base graphics.
| However, you may get away with
|
| ggtitle(paste0("COVID-19 Tests in Ohio \n(",date[length(date]),")"))+
|
| I don't know whether the tidy* stuff
| handles indexing in the same way as
| base R.

I added a format, which converts it to
the date format you mentioned (but with
a zero in the month part ...):

          ggplot2::ggtitle(
            paste0("COVID-19 Tests in Ohio \n(",
                   format(max(dfO$date), "%m/%d/%y"), ")")) +

Here is the whole script:

        datO <- read.csv("https://api.covidtracking.com/v1/states/oh/daily.csv")
       
        cases <- c("positive", "negative", "total")
        levels <- paste0(
          toupper(substr(cases, 1, 1)),
          substr(cases, 2, nchar(cases)))
       
        dfO <- data.frame(
          date=
            rep(
              x=lubridate::ymd(datO$date),
              each=length(cases)),
          cases=levels,
          count=as.vector(t(datO[,cases])))
        dfO$cases <- factor(dfO$cases, levels=levels)
       
        file <- "/tmp/stephen.pdf"
        res <- .5
        width <- 9*res
        height <- 5*res
        pdf(file=file, width=width, height=height)
       
        mapping <- ggplot2::aes(
          x=date,
          y=count,
          linetype=cases)
       
        p <-
          ggplot2::ggplot(
            data=dfO,
            mapping=mapping) +
          ggplot2::geom_line() +
          ggplot2::scale_linetype_manual(
            name = "Test",
            labels = levels,
            values =
              c("dotted",
                "dashed",
                "solid")) +
          ggplot2::ylim(0, 2e6) +
          ggplot2::labs(
            x = "Date",
            y = "Number of Tests") +
          ggplot2::ggtitle(
            paste0("COVID-19 Tests in Ohio \n(",
                   format(max(dfO$date), "%m/%d/%y"), ")")) +
          ggplot2::theme_bw() +
          ggplot2::theme(
            axis.text.x =
              ggplot2::element_text(
                angle = 30,
                hjust = 1),
            plot.title =
              ggplot2::element_text(
                hjust = 0.5))
       
        p
       
        dev.off()

Best,
Rasmus

______________________________________________
[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.

signature.asc (849 bytes) Download Attachment