############  Japan  

# data uptake
 JP <- read.table(file="JP.txt", sep="\t", header=TRUE)
   JP<- as.matrix(JP)
   number_patients <- as.numeric(JP[,5])
   dates<- as.Date(JP[,1])

plot(dates, number_patients)

   number_patients_av<- number_patients+NA
   for(i in 5:1131){
   number_patients_av[i] <- mean(number_patients[-4:4+i]) }
   
#### population: 1.257E8 (2021) 

  japan<- number_patients_av/1.257E8*1e4 

 as.numeric(JP[1135,6])/1.257E8
[1] 0.2615722

 as.numeric(JP[1135,8])/1.257E8*1e3
[1] 0.5583532

# finding the values
#### K 
   K <- number_patients_av+NA
   K[1:1128+4] <- (log2(number_patients_av[1:1128+7] )- log2( number_patients_av[1:1128]))/7
   K[is.nan(K)] <- NA
   K[is.infinite(K)] <- NA
   K_av <- K*NA
   
for(i in 5:1131){
  K_av[i] <- mean(K[-4:4+i], na.rm=T) }

   dKdt<- number_patients+NA
   dKdt[1:1128+4]<- (K_av[1:1128+7]-K_av[1:1128])/7
   
# dKdt
   dKdt_av<- dKdt*0
   for(i in 5:1131){
   dKdt_av[i] <- mean(dKdt[-4:4+i], na.rm=T) }

max(dKdt_av, na.rm=T)
min(dKdt_av, na.rm=T)

max(K_av, na.rm=T)
min(K_av, na.rm=T)

#death
   number_death <- as.numeric(JP[,7])

   number_death_av<- number_death+NA  # moving average

   for(i in 5:1131){
   number_death_av[i] <- mean(number_death[-4:4+i]) }
   
# death rate
  death_rate <- number_death*NA

   for(i in 1:1117+18){   # change the range of i appropriately 
   death_rate[i] <- mean((number_death_av[1:1117+18]/number_patients_av[1:1117])[-4:4+i], na.rm=T) }

#### making figures

## K
    ylim=c(quantile(number_patients_av, 0.1, na.rm=T), quantile(number_patients_av, 1, na.rm=T))+0.1

  png(width=1000, height=1000, pointsize = 36,  file="Japan.png")
	 par(lwd=2, mex=0.7, cex=1, mai=c(2,2,1,2))

   plot(dates+3.5 ,  number_patients_av, pch=NA, xlim=c(as.Date("2021-01-01"), as.Date("2023-02-5")), type="l", col="blue",lty=1, axes=F, ylab="", xlab="Date (Month/Year)", main="Japan" , ylim=ylim, log="y") 
   axis.Date(1, at = c("2020-01-01", "2020-07-01","2021-01-01","2021-07-01","2022-01-01","2022-07-01","2023-01-01"),format = "%m/%y")
      
   par(new=T)
   box()
   #abline(v=as.Date("2021-07-23"), lwd=2, col="green3")
   axis(4, col.axis="blue")
   par(new=T)
   mtext("Confirmed cases", side=4, line=2.5, col="#d3381c")
   par(new=T)
   
   plot(dates+3.5, dKdt_av*5, axes=F, type="l", col="#d3381c", xlab="", ylab="", xlim=c(as.Date("2021-01-01"), as.Date("2023-02-5")), ylim=c(-0.25, +0.45))
   axis(2, col.axis="black")
   #abline(h=0, col="red")
   
   par(new=T)
   
   plot(dates+3.5 ,   K_av, type="l", xlim=c(as.Date("2021-01-01"), as.Date("2023-02-5")), ylim=c(-0.25, +0.45), axes=F, xlab="",ylab="")
   
   mtext(expression(paste(italic(K), " and " )), side=2, line=3,  at=0.05)
   mtext(expression(paste(  italic(K), "'")), side=2, line=3, at=0.12, col="#d3381c")
   
   abline(h=0, lty=3, lwd=1)
      
   legend(as.Date("2021-02-20"), 0.4, legend=c(  "cases", expression(italic(K)) ,expression(paste(italic(K), "' x5"))),lty=c( 1,1),lwd=4, col=c(  "blue", "black" ,"#d3381c" ), cex=.6, box.lwd = 1)
  dev.off()
   
   
## death
   ylim=c(quantile(number_death_av, 0.1, na.rm=T), quantile(number_patients_av, 1, na.rm=T))+0.1
	
  png(width=1000, height=1000, pointsize = 36,  file="JapanD.png")
	 par(lwd=2, mex=0.7, cex=1, mai=c(2,2,1,2))

   plot(dates+3.5 ,  number_patients_av, pch=NA, xlim=c(as.Date("2021-01-01"), as.Date("2023-02-5")), type="l", col="blue",lty=1, axes=F, ylab="", xlab="Date (Month/Year)", main="Japan" , ylim=ylim, log="y") 
   
   par(new=T)
   
   plot(dates+3.5 ,  number_death_av, pch=NA, xlim=c(as.Date("2021-01-01"), as.Date("2023-02-5")), type="l", col="#d3381c",lty=1, axes=F, ylab="", xlab="Date (Month/Year)", main="Japan" , ylim=ylim, log="y") 
   axis.Date(1, at = c("2020-01-01", "2020-07-01","2021-01-01","2021-07-01","2022-01-01","2022-07-01","2023-01-01"),format = "%m/%y")
   
   axis(4, col.axis="#d3381c")
   mtext("Confirmed cases", side=4, line=2.5, col="#d3381c")
   
   par(new=T)
   plot(dates[1:1:1117+18]+3.5 , death_rate[1:1:1117], pch=NA, xlim=c(as.Date("2021-01-01"), as.Date("2023-02-5")), type="l", col="black",lty=1, axes=F, ylab="", xlab="", main="" ,  log="y", ylim=c(2E-4, 0.2)) 
   
   axis(2)
   mtext("Mortality rate", side=2, line=3,  at=6e-3)
   legend(as.Date("2021-02-20"), 0.19, legend=c(  "Cases", "Death","Rate"),lty=c( 1,1),lwd=4, col=c(  "blue", "#d3381c" ,"black" ), cex=.6, box.lwd = 1)
   box()
   
  dev.off()
   

