#demo(image)
#demo(graphics)

x <- seq(1,6)
y <- x

plot(x,y, type = 'l')

plot(x,y, type = 'l', col ='red')

types <- c("p","l","o","b","c","s", "h", "n")

par(mfrow= c(2,4))
for(i in types){
  plot(x,y, type = i , main=sprintf('type:%s', i) )
}

par(mfrow= c(2,4))
for(i in types){
  plot(x,y, type = 'n' , main=sprintf('type:%s', i) )
  lines(x,y, type = i)
}

y2 <- c(3,3,3,3,3,3)
plot(x,y, type= 'n')
lines(x,y, type= 'l', col = 'blue')
lines(x,y2, type= 'l', col = 'red')


taipei <- c(92.5,132.6,168.8,159.1,218.7)
tainan <- c(21.2, 30.6, 37.3, 84.6, 184.3)

plot(taipei, type ='o', col = 'blue', ylim = c(0,220), xlab = 'month', ylab = 'rainfall', main = 'Rainfall')
lines(tainan, type ='o', col = 'red')


library(readr)
covid19 <- read_csv('https://raw.githubusercontent.com/ywchiu/cdc_course/master/2020Exam/covid19.csv')
## Warning: Missing column names filled in: 'X1' [1]
## Parsed with column specification:
## cols(
##   X1 = col_double(),
##   `Province/State` = col_character(),
##   `Country/Region` = col_character(),
##   Lat = col_double(),
##   Long = col_double(),
##   Case = col_character(),
##   Date = col_date(format = ""),
##   Case_Number = col_double()
## )
head(covid19)
## # A tibble: 6 x 8
##      X1 `Province/State` `Country/Region`   Lat   Long Case  Date      
##   <dbl> <chr>            <chr>            <dbl>  <dbl> <chr> <date>    
## 1     1 <NA>             Afghanistan       33.9  67.7  conf… 2020-01-22
## 2     2 <NA>             Albania           41.2  20.2  conf… 2020-01-22
## 3     3 <NA>             Algeria           28.0   1.66 conf… 2020-01-22
## 4     4 <NA>             Andorra           42.5   1.52 conf… 2020-01-22
## 5     5 <NA>             Angola           -11.2  17.9  conf… 2020-01-22
## 6     6 <NA>             Antigua and Bar…  17.1 -61.8  conf… 2020-01-22
## # … with 1 more variable: Case_Number <dbl>
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.0.2
## ── Attaching packages ─────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2     ✓ dplyr   1.0.0
## ✓ tibble  3.0.3     ✓ stringr 1.4.0
## ✓ tidyr   1.1.0     ✓ forcats 0.5.0
## ✓ purrr   0.3.4
## Warning: package 'tibble' was built under R version 4.0.2
## ── Conflicts ────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
US <- covid19 %>%
  filter(`Case` == 'confirmed', `Country/Region` == 'US') %>%
  select(Date, Case_Number)

plot(x= US$Date, y = US$Case_Number, type = 'o', col = 'red', xlab = 'Date', ylab = 'Case Number', main = 'US Confirmed Number')

plot( Case_Number~Date, US, col='red', xlab = 'Date', ylab = 'Case Number', main = 'US Confirmed Number' )


US_Confirmed <- covid19 %>%
  filter(`Case` == 'confirmed', `Country/Region` == 'US') %>%
  select(Date, Case_Number)

US_Recovered <- covid19 %>%
  filter(`Case` == 'recovered', `Country/Region` == 'US') %>%
  select(Date, Case_Number)


plot( Case_Number~Date, US_Confirmed, col='red', xlab = 'Date', ylab = 'Case Number', main = 'US Case Number' , type = 'l')
lines( Case_Number~Date, US_Recovered, col='blue', type = 'l' )

# method 1
# use options to force R not render scientific notation
options("scipen"=100, "digits"=4)
plot( Case_Number~Date, US_Confirmed, col='red', xlab = 'Date', ylab = 'Case Number', main = 'US Case Number' , type = 'l')
lines( Case_Number~Date, US_Recovered, col='blue', type = 'l' )

# method 2
plot( Case_Number~Date, US_Confirmed, col='red', xlab = 'Date', ylab = 'Case Number', main = 'US Case Number' , type = 'l', yaxt = 'n')
axis(2, at=seq(0,7000000, 1000000), labels=sprintf("%.f", seq(0,7000000, 1000000)))

plot( Case_Number~Date, US_Confirmed, col='red', xlab = 'Date', ylab = 'Case Number', main = 'US Case Number' , type = 'l', yaxt = 'n')
axis(2, at=seq(0,7000000, 500000), labels=sprintf("%.f", seq(0,7000000, 500000)))

# lty
plot( Case_Number~Date, US_Confirmed, col='red', xlab = 'Date', ylab = 'Case Number', main = 'US Case Number' , type = 'l', yaxt = 'n', lty = 2)
axis(2, at=seq(0,7000000, 500000), labels=sprintf("%.f", seq(0,7000000, 500000)))


plot( Case_Number~Date, US_Confirmed, col='red', xlab = 'Date', ylab = 'Case Number', main = 'US Case Number' , type = 'l', yaxt = 'n', xlim = c(as.Date('2020-01-22'), as.Date('2020-11-01')))
lines( Case_Number~Date, US_Recovered, col='blue', type = 'l' )
axis(2, at=seq(0,7000000, 500000), labels=sprintf("%.f", seq(0,7000000, 500000)))
legend('topleft', c("confirmed","recovered"), lwd=c(2.5,2.5),col=c("red","blue"), title = "Case Type")
text(as.Date('2020-09-30'), 6000000, label = 'Confirmed', col ='red')
text(as.Date('2020-09-30'), 2000000, label = 'Recovered', col ='blue')