setwd("C:\\Users\\aleid\\OneDrive - University of Texas at El Paso\\UTEP\\Spring 2021\\Data visualization\\hw6")
full_corona=read.csv("full_eurcovid.csv")
full_corona$date<-as.Date(full_corona$date, format = "%Y-%m-%d")
head(full_corona$date)
## [1] "2021-01-06" "2021-01-07" "2021-01-08" "2021-01-09" "2021-01-10"
## [6] "2021-01-11"
#Country
nordic.countries<-c('Denmark','Norway', 'Sweden', 'Finland', 'Iceland')
subset_corona<-full_corona
nordic=subset_corona[full_corona$location %in% nordic.countries,]

#remove observations from January because it's only available for Finland
df2<-nordic[!(nordic$date=='2020-01-29' |nordic$date=='2020-01-30'| nordic$date=='2020-01-31'), ]
nordic=df2

df2<-nordic[!(nordic$date=='2020-01-29' |nordic$date=='2020-01-30'| nordic$date=='2020-01-31'), ]
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
df.1 <-nordic%>% 
  mutate(date = as.Date(date)) %>% 
  mutate(ym = format(date, '%Y-%m')) %>% 
  group_by(country=location,ym) %>% 
  summarize(ym_cases = sum(new_cases),
            ym_deaths=sum(new_deaths),
            rate=round(sum(new_deaths)/sum(new_cases),digits=2),
            survival=round((1-rate),digits=2),
            index=mean(stringency_index))
## `summarise()` has grouped output by 'country'. You can override using the `.groups` argument.
#subset dates from febrary 2020 because they're incomplete and lowers the stringemcy index
#table(df.2$ym)
df.2<-df.1[!(df.1$ym=='2020-02'), ]
#country = c("fi", "dk", "is", "no", "se" ),
#colnames(df.rankings)
 df.rankings2=df.2
df.rankings2$country=as.factor(df.rankings2$country)
 
levels(df.rankings2$country)[levels(df.rankings2$country)=="Finland"] <- "fi"
levels(df.rankings2$country)[levels(df.rankings2$country)=="Denmark"]<- "dk"
levels(df.rankings2$country)[levels(df.rankings2$country)=="Iceland"] <- "is"
levels(df.rankings2$country)[levels(df.rankings2$country)=="Norway"] <- "no"
levels(df.rankings2$country)[levels(df.rankings2$country)=="Sweden"]<- "se"
df.rankings2$country=as.character(df.rankings2$country)
table(df.rankings2$country)
## 
## dk fi is no se 
## 13 13 13 13 13
df.rankings <- df.rankings2 %>%
  mutate(flag = ifelse(country %in% c( "dk", "fi", "is", "no", "se"), TRUE, FALSE),
         country_col = if_else(flag == TRUE, country, "zzz"))
country_flags_start <- data.frame(
  x = "2020-03", y = c(52.6, 44.4,  35.7,   46.7,   28.0), #from 45.1 to 44.4
  country = c("dk", "fi", "is","no",  "se"),
  stringsAsFactors = FALSE)

country_flags_end <- data.frame(
  x = "2021-03", y = c(62.6,    52.4,   40.2,   70.1,   68.1), #from 70.1 to 70.4 for display purposes overlap and 69.4 to 68.1
  country = c("dk", "fi", "is","no",  "se"), 
  stringsAsFactors = FALSE)
library(ggplot2)
library(ggflags) 
p<-ggplot(data = df.rankings, aes(x = ym, y = index, group = country)) +
  geom_line(aes(color = country_col), alpha = 1, size = 0.8) +
  geom_point(color = "#FFFFFF", size = 3) +
  geom_point(aes(color = country_col), alpha = 1, size = 3) +
  geom_point(color = "#FFFFFF", size = 1) +theme(axis.line=element_blank())
p

#placig size outside aes avoids including extra legends and setting y axis limits
p1<-p+  geom_flag(data = country_flags_start, aes(x = x, y = y, country = country), size = 3.6) +
  geom_flag(data = country_flags_end, aes(x = x, y = y, country = country), size =3.6)+
  scale_y_continuous(limits = c(0, 80), breaks = seq(0, 80, by = 5))+coord_cartesian(clip = "off")

p1

p2<-p1+scale_x_discrete(labels=c("2020-03"="Mar, 2020", "2020-04"="Apr, 2020", "2020-05"="May, 2020", "2020-06"="Jun, 2020", 
"2020-07"="Jul, 2020", "2020-08"="Aug, 2020", "2020-09"="Sep, 2020", "2020-10"="Oct, 2020", 
"2020-11"="Nov, 2020", "2020-12"="Dec, 2020", "2021-01"="Jan, 2021", "2021-02"="Feb, 2021", 
"2021-03"="Mar, 2021"),expand=c(0,0)) + scale_color_manual(values = c("#034e7b", "#3690c0", "#74a9cf", "#a6bddb", "#d8d6dd"),labels=c("Dinmark", "Finland", "Iceland", "Norway", "Sweden"), name="Country")+theme(axis.text.x = element_text(size = 6.5),axis.text.y = element_text(size = 6.5)) #using guide to avoid overlap
p2

#?scale_x_discrete




p3=p2+labs(x = " ", y="", title = "Stringency index in Nordic countries", subtitle = " ",
       caption = "Source: https://ourworldindata.org/, March 2021")+ theme(plot.caption = element_text(color = "gray", face = "italic", size=10)) +theme(legend.position = "top", legend.direction = "horizontal")
 
p3 

p4=p3+theme(panel.grid = element_blank(), axis.line=element_blank(), axis.ticks=element_blank(),panel.border = element_blank(),panel.background = element_rect(fill = "#f0f0f0")) 
p4

#axis.line=element_line(colour="gray"), axis.ticks.y=element_blank(),panel.border = element_blank())

p4+scale_x_discrete(expand = expansion(add = .6)) #add space for the plot
## Scale for 'x' is already present. Adding another scale for 'x', which will
## replace the existing scale.