Ggplot2 Line Problem

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

Ggplot2 Line Problem

Stephen P. Molnar
I have cobbled together a short script to plot Covid-19 data.

setwd("~/Apps/Models/1-CoronaVirus")

library(tidyverse)
library(lubridate)

datO <- read.csv("https://api.covidtracking.com/v1/states/oh/daily.csv")
datO[ ,1] <- ymd(datO[ ,1])

dfO <- tibble::as_tibble(data.frame(datO[ ,"date"],datO[
,"positive"],datO[ ,"negative"],datO[ ,"total"]))

dfO %>%
   ggplot(aes(x = datO[ ,"date"],y = datO[ ,"positive"]))+
   geom_point(color = 'red', size = 0.025)+
   geom_point(y = datO[ ,"negative"], color = 'blue', size = 0.025)+
   geom_point(y = datO[ ,"total"], color = "green", size = 0.025)+
   theme(axis.text.x = element_text(angle=30, hjust=1))+
   theme_bw()+
   scale_y_continuous(limits = c(0,1750000))+
   labs(x = "Date", y = "Number of Tests")+
   ggtitle("COVID-19 Tests in Ohio \n (8/15/20)")+
   theme(plot.title = element_text(hjust = 0.5))+
   scale_fill_discrete(name = "Test", labels = c("Positive", "Negative",
"Total"))

Here is the plot:




but, if I want lines rather that the code (the aspplicable plines) uis:

ggplot(aes(x = datO[ ,"date"],y = datO[ ,"positive"]))+
   geom_line(linetype = "solid",color = 'red')+
   geom_line(linetype = "dotdash",y = datO[ ,"negative"], color = 'blue')+
   geom_line(linetype = "twodash",y = datO[ ,"total"], color = "green")+




Now two of the plots are reversed. Google has not been a friend in
finding a solution.

Help will be much appreciated.

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: Ggplot2 Line Problem

Rui Barradas
Hello,

This type of problem is almost always a data reshaping problem.
ggplot graphics work better if the data is in the long format and you
have 3 columns for counts, one column for each category. If you reformat
from the current wide format to the long format you will have a date
vector, a categorical variable and a counts variable.

In the code below just change geom_point to geom_line and the problem is
solved.


library(tidyverse)
library(lubridate)

datO <- read.csv("https://api.covidtracking.com/v1/states/oh/daily.csv")
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_point() +
   scale_color_manual(name = "Test",
                      labels = c("Positive", "Negative", "Total"),
                      values = c("red", "blue", "green")) +
   ylim(0, 1750000) +
   labs(x = "Date", y = "Number of Tests")+
   ggtitle("COVID-19 Tests in Ohio \n (8/15/20)")+
   theme_bw() +
   theme(axis.text.x = element_text(angle = 30, hjust = 1),
         plot.title = element_text(hjust = 0.5))



Hope this helps,

Rui Barradas


Às 02:00 de 17/08/20, Stephen P. Molnar escreveu:

> I have cobbled together a short script to plot Covid-19 data.
>
> setwd("~/Apps/Models/1-CoronaVirus")
>
> library(tidyverse)
> library(lubridate)
>
> datO <- read.csv("https://api.covidtracking.com/v1/states/oh/daily.csv")
> datO[ ,1] <- ymd(datO[ ,1])
>
> dfO <- tibble::as_tibble(data.frame(datO[ ,"date"],datO[
> ,"positive"],datO[ ,"negative"],datO[ ,"total"]))
>
> dfO %>%
>    ggplot(aes(x = datO[ ,"date"],y = datO[ ,"positive"]))+
>    geom_point(color = 'red', size = 0.025)+
>    geom_point(y = datO[ ,"negative"], color = 'blue', size = 0.025)+
>    geom_point(y = datO[ ,"total"], color = "green", size = 0.025)+
>    theme(axis.text.x = element_text(angle=30, hjust=1))+
>    theme_bw()+
>    scale_y_continuous(limits = c(0,1750000))+
>    labs(x = "Date", y = "Number of Tests")+
>    ggtitle("COVID-19 Tests in Ohio \n (8/15/20)")+
>    theme(plot.title = element_text(hjust = 0.5))+
>    scale_fill_discrete(name = "Test", labels = c("Positive", "Negative",
> "Total"))
>
> Here is the plot:
>
>
>
>
> but, if I want lines rather that the code (the aspplicable plines) uis:
>
> ggplot(aes(x = datO[ ,"date"],y = datO[ ,"positive"]))+
>    geom_line(linetype = "solid",color = 'red')+
>    geom_line(linetype = "dotdash",y = datO[ ,"negative"], color = 'blue')+
>    geom_line(linetype = "twodash",y = datO[ ,"total"], color = "green")+
>
>
>
>
> Now two of the plots are reversed. Google has not been a friend in
> finding a solution.
>
> Help will be much appreciated.
>
> Thanks in advance
>

______________________________________________
[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: Ggplot2 Line Problem

Rui Barradas
Hello,

Sorry, I forgot you also want the line type changed.
Remove color and linetype from the initial call to ggplot and include
aes(color = cases, linetype = cases) in geom_line. Then add a layer
scale_linetype_manual with the same name and labels to merge it with the
color legend.



dfO %>%
   pivot_longer(
     cols = -date,
     names_to = "cases",
     values_to = "count"
   ) %>%
   mutate(cases = factor(cases, levels = c("positive", "negative",
"total"))) %>%
   ggplot(aes(date, count)) +
   #geom_point() +
   geom_line(aes(color = cases, linetype = cases)) +
   scale_color_manual(name = "Test",
                      labels = c("Positive", "Negative", "Total"),
                      values = c("red", "blue", "green")) +
   scale_linetype_manual(name = "Test",
                         labels = c("Positive", "Negative", "Total"),
                         values = c("solid", "dotdash", "twodash")) +
   ylim(0, 1750000) +
   labs(x = "Date", y = "Number of Tests")+
   ggtitle("COVID-19 Tests in Ohio \n (8/15/20)")+
   theme_bw() +
   theme(axis.text.x = element_text(angle = 30, hjust = 1),
         plot.title = element_text(hjust = 0.5))


Hope this helps,

Rui Barradas

Às 06:49 de 17/08/20, Rui Barradas escreveu:

> Hello,
>
> This type of problem is almost always a data reshaping problem.
> ggplot graphics work better if the data is in the long format and you
> have 3 columns for counts, one column for each category. If you reformat
> from the current wide format to the long format you will have a date
> vector, a categorical variable and a counts variable.
>
> In the code below just change geom_point to geom_line and the problem is
> solved.
>
>
> library(tidyverse)
> library(lubridate)
>
> datO <- read.csv("https://api.covidtracking.com/v1/states/oh/daily.csv")
> 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_point() +
>    scale_color_manual(name = "Test",
>                       labels = c("Positive", "Negative", "Total"),
>                       values = c("red", "blue", "green")) +
>    ylim(0, 1750000) +
>    labs(x = "Date", y = "Number of Tests")+
>    ggtitle("COVID-19 Tests in Ohio \n (8/15/20)")+
>    theme_bw() +
>    theme(axis.text.x = element_text(angle = 30, hjust = 1),
>          plot.title = element_text(hjust = 0.5))
>
>
>
> Hope this helps,
>
> Rui Barradas
>
>
> Às 02:00 de 17/08/20, Stephen P. Molnar escreveu:
>> I have cobbled together a short script to plot Covid-19 data.
>>
>> setwd("~/Apps/Models/1-CoronaVirus")
>>
>> library(tidyverse)
>> library(lubridate)
>>
>> datO <- read.csv("https://api.covidtracking.com/v1/states/oh/daily.csv")
>> datO[ ,1] <- ymd(datO[ ,1])
>>
>> dfO <- tibble::as_tibble(data.frame(datO[ ,"date"],datO[
>> ,"positive"],datO[ ,"negative"],datO[ ,"total"]))
>>
>> dfO %>%
>>    ggplot(aes(x = datO[ ,"date"],y = datO[ ,"positive"]))+
>>    geom_point(color = 'red', size = 0.025)+
>>    geom_point(y = datO[ ,"negative"], color = 'blue', size = 0.025)+
>>    geom_point(y = datO[ ,"total"], color = "green", size = 0.025)+
>>    theme(axis.text.x = element_text(angle=30, hjust=1))+
>>    theme_bw()+
>>    scale_y_continuous(limits = c(0,1750000))+
>>    labs(x = "Date", y = "Number of Tests")+
>>    ggtitle("COVID-19 Tests in Ohio \n (8/15/20)")+
>>    theme(plot.title = element_text(hjust = 0.5))+
>>    scale_fill_discrete(name = "Test", labels = c("Positive",
>> "Negative", "Total"))
>>
>> Here is the plot:
>>
>>
>>
>>
>> but, if I want lines rather that the code (the aspplicable plines) uis:
>>
>> ggplot(aes(x = datO[ ,"date"],y = datO[ ,"positive"]))+
>>    geom_line(linetype = "solid",color = 'red')+
>>    geom_line(linetype = "dotdash",y = datO[ ,"negative"], color =
>> 'blue')+
>>    geom_line(linetype = "twodash",y = datO[ ,"total"], color = "green")+
>>
>>
>>
>>
>> Now two of the plots are reversed. Google has not been a friend in
>> finding a solution.
>>
>> Help will be much appreciated.
>>
>> Thanks in advance
>>
>
> ______________________________________________
> [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: Ggplot2 Line Problem

Stephen P. Molnar
In reply to this post by Rui Barradas
Many thanks. That solved the problem.

On 08/17/2020 01:49 AM, Rui Barradas wrote:

> Hello,
>
> This type of problem is almost always a data reshaping problem.
> ggplot graphics work better if the data is in the long format and you
> have 3 columns for counts, one column for each category. If you
> reformat from the current wide format to the long format you will have
> a date vector, a categorical variable and a counts variable.
>
> In the code below just change geom_point to geom_line and the problem
> is solved.
>
>
> library(tidyverse)
> library(lubridate)
>
> datO <- read.csv("https://api.covidtracking.com/v1/states/oh/daily.csv")
> 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_point() +
>   scale_color_manual(name = "Test",
>                      labels = c("Positive", "Negative", "Total"),
>                      values = c("red", "blue", "green")) +
>   ylim(0, 1750000) +
>   labs(x = "Date", y = "Number of Tests")+
>   ggtitle("COVID-19 Tests in Ohio \n (8/15/20)")+
>   theme_bw() +
>   theme(axis.text.x = element_text(angle = 30, hjust = 1),
>         plot.title = element_text(hjust = 0.5))
>
>
>
> Hope this helps,
>
> Rui Barradas
>
>
> ??s 02:00 de 17/08/20, Stephen P. Molnar escreveu:
>> I have cobbled together a short script to plot Covid-19 data.
>>
>> setwd("~/Apps/Models/1-CoronaVirus")
>>
>> library(tidyverse)
>> library(lubridate)
>>
>> datO <- read.csv("https://api.covidtracking.com/v1/states/oh/daily.csv")
>> datO[ ,1] <- ymd(datO[ ,1])
>>
>> dfO <- tibble::as_tibble(data.frame(datO[ ,"date"],datO[
>> ,"positive"],datO[ ,"negative"],datO[ ,"total"]))
>>
>> dfO %>%
>>    ggplot(aes(x = datO[ ,"date"],y = datO[ ,"positive"]))+
>>    geom_point(color = 'red', size = 0.025)+
>>    geom_point(y = datO[ ,"negative"], color = 'blue', size = 0.025)+
>>    geom_point(y = datO[ ,"total"], color = "green", size = 0.025)+
>>    theme(axis.text.x = element_text(angle=30, hjust=1))+
>>    theme_bw()+
>>    scale_y_continuous(limits = c(0,1750000))+
>>    labs(x = "Date", y = "Number of Tests")+
>>    ggtitle("COVID-19 Tests in Ohio \n (8/15/20)")+
>>    theme(plot.title = element_text(hjust = 0.5))+
>>    scale_fill_discrete(name = "Test", labels = c("Positive",
>> "Negative", "Total"))
>>
>> Here is the plot:
>>
>>
>>
>>
>> but, if I want lines rather that the code (the aspplicable plines) uis:
>>
>> ggplot(aes(x = datO[ ,"date"],y = datO[ ,"positive"]))+
>>    geom_line(linetype = "solid",color = 'red')+
>>    geom_line(linetype = "dotdash",y = datO[ ,"negative"], color =
>> 'blue')+
>>    geom_line(linetype = "twodash",y = datO[ ,"total"], color = "green")+
>>
>>
>>
>>
>> Now two of the plots are reversed. Google has not been a friend in
>> finding a solution.
>>
>> Help will be much appreciated.
>>
>> 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.