2020-08-27 10:38:30 -04:00
package main
import (
"path"
)
func rCode ( ) string {
return `
library ( tidyverse )
library ( ggplot2 )
library ( gridExtra )
library ( lubridate )
options ( scipen = 100000 )
d <- read_csv ( ' ` + path.Dir(confFile) + ` / rows . csv ' )
d <- pivot_longer ( d , c ( ' New Positives ' , ' Total Number of Tests Performed ' ) )
d <- transmute ( d , date = mdy ( d $ ' Test Date ' ) , county = County , name = name , value = value )
queens <- subset ( d , county == ' Queens ' )
qrate <- pivot_wider ( queens , names_from = ' name ' ) % > % transmute ( date = date , county = county , name = ' Positive Rate ' , value = ifelse ( ` +" ` "+`New Positives`+" ` "+ ` == 0 , 0 , ` +" ` "+`New Positives`+" ` "+ ` / ` +" ` "+`Total Number of Tests Performed`+" ` "+ ` ) )
totals <- group_by ( d , date , county = ' New York ' , name ) % > % summarize ( value = sum ( value ) ) % > % ungroup
trate <- pivot_wider ( totals , names_from = ' name ' ) % > % transmute ( date = date , county = county , name = ' Positive Rate ' , value = ifelse ( ` +" ` "+`New Positives`+" ` "+ ` == 0 , 0 , ` +" ` "+`New Positives`+" ` "+ ` / ` +" ` "+`Total Number of Tests Performed`+" ` "+ ` ) )
d2 <- rbind ( queens , totals )
2020-08-28 13:51:23 -04:00
p1 <- ggplot ( queens , aes ( x = date , y = value , group = name , color = name ) ) + geom_line ( color = ' black ' , alpha = 1 / 3 ) + scale_y_log10 ( n . breaks = 6 , position = ' right ' ) + labs ( y = ' ' , color = ' ' ) + ggtitle ( ' Queens ' ) + theme ( plot . title = element_text ( hjust = 0.5 ) ) + theme ( legend . position = ' bottom ' ) + geom_smooth ( se = FALSE , method = ' gam ' )
p2 <- ggplot ( totals , aes ( x = date , y = value , group = name , color = name ) ) + geom_line ( color = ' black ' , alpha = 1 / 3 ) + stat_smooth ( geom = ' line ' , linetype = ' dotted ' , method = ' gam ' , se = FALSE ) + scale_y_log10 ( n . breaks = 6 , position = ' right ' ) + labs ( y = ' ' ) + labs ( color = ' ' ) + ggtitle ( ' New York ' ) + theme ( plot . title = element_text ( hjust = 0.5 ) ) + theme ( legend . position = ' bottom ' ) + geom_smooth ( se = FALSE , method = ' gam ' )
2020-08-27 10:38:30 -04:00
2020-12-31 20:16:34 -05:00
pr1 <- ggplot ( qrate , aes ( x = date , y = value ) ) + geom_line ( color = ' black ' , alpha = 1 / 3 ) + geom_smooth ( method = ' gam ' , formula = y ~ s ( x , bs = "cs" ) , se = FALSE ) + scale_y_continuous ( labels = scales : : percent , position = ' right ' ) + labs ( y = ' Positive Rate ' ) + coord_cartesian ( ylim = c ( 0 , 0.10 ) ) + ggtitle ( ' Queens ' ) + theme ( plot . title = element_text ( hjust = 0.5 ) )
pr2 <- ggplot ( trate , aes ( x = date , y = value ) ) + geom_line ( color = ' black ' , alpha = 1 / 3 ) + geom_smooth ( method = ' gam ' , formula = y ~ s ( x , bs = "cs" ) , se = FALSE ) + scale_y_continuous ( labels = scales : : percent , position = ' right ' ) + labs ( y = ' Positive Rate ' ) + coord_cartesian ( ylim = c ( 0 , 0.10 ) ) + ggtitle ( ' New York ' ) + theme ( plot . title = element_text ( hjust = 0.5 ) )
2020-08-27 10:38:30 -04:00
2020-09-04 12:31:33 -04:00
queens <- tail ( queens , n = 32 )
totals <- tail ( totals , n = 32 )
qrate <- tail ( qrate , n = 16 )
trate <- tail ( trate , n = 16 )
2020-08-28 13:51:23 -04:00
p3 <- ggplot ( queens , aes ( x = date , y = value , color = name ) ) + geom_line ( ) + scale_y_log10 ( n . breaks = 12 , position = ' right ' ) + labs ( y = ' ' , color = ' ' ) + ggtitle ( ' Queens ' ) + theme ( plot . title = element_text ( hjust = 0.5 ) ) + theme ( legend . position = ' bottom ' )
p4 <- ggplot ( totals , aes ( x = date , y = value , color = name ) ) + geom_line ( ) + scale_y_log10 ( n . breaks = 12 , position = ' right ' ) + labs ( y = ' ' ) + labs ( color = ' ' ) + ggtitle ( ' New York ' ) + theme ( plot . title = element_text ( hjust = 0.5 ) ) + theme ( legend . position = ' bottom ' )
2020-12-31 20:16:34 -05:00
pr3 <- ggplot ( qrate , aes ( x = date , y = value ) ) + geom_line ( ) + scale_y_continuous ( labels = scales : : percent , position = ' right ' ) + labs ( y = ' Positive Rate ' ) + coord_cartesian ( ylim = c ( 0 , 0.10 ) ) + ggtitle ( ' Queens ' ) + theme ( plot . title = element_text ( hjust = 0.5 ) )
2020-08-28 13:51:23 -04:00
2020-12-31 20:16:34 -05:00
pr4 <- ggplot ( trate , aes ( x = date , y = value ) ) + geom_line ( ) + scale_y_continuous ( labels = scales : : percent , position = ' right ' ) + labs ( y = ' Positive Rate ' ) + coord_cartesian ( ylim = c ( 0 , 0.10 ) ) + ggtitle ( ' New York ' ) + theme ( plot . title = element_text ( hjust = 0.5 ) )
2020-08-28 13:51:23 -04:00
2020-08-29 17:02:51 -04:00
svg ( width = 8 , height = 16 )
2020-08-28 13:51:23 -04:00
grid . arrange ( p1 , p2 , pr1 , pr2 , p3 , p4 , pr3 , pr4 , ncol = 2 , top = format ( d [ [ nrow ( d ) , 1 ] ] , "%B %d %Y" ) )
dev . off ( )
2020-08-27 10:38:30 -04:00
`
}