inst/Paper/paper_script.R

#POl Comp Paper
rm(list=ls())
library(tidyverse)
library(readxl)
library(ggplot2)
require(maps)
require(viridis)
library(gridExtra)
# Reliability and Validity----
# Compare with previous measures (density & scatter plots, by distribution but also contry specific)

# USA~CHN

#Plot World Countries CHN USA / RUS USA / USA FRA----

load("inst/extdata/unpc_final.RData")

  ## CHN USA----
unpc_USA_CHN<-unpc_final[unpc_final$competitor1=="USA"&unpc_final$competitor2=="CHN",]


unpc <- unpc_USA_CHN %>%
  select(competed, pi,year) %>%
  rename(ISO = competed, Competition = pi)

codes<- read_excel("inst/extdata/country_codes.xlsx", sheet = 1,skip=1)
codes<-codes[,c("official_name_en","ISO3166-1-Alpha-3")]
codes$region<-codes$official_name_en
codes$ISO<-codes$`ISO3166-1-Alpha-3`
x <- left_join(unpc,codes, by = "ISO")
world_map <- map_data("world")
life.exp.map <- left_join(x, world_map, by = "region")

b <- c(0,0.5,1)
gg18<- ggplot(life.exp.map[life.exp.map$year==2018,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.8),na.value = "grey")+
  ggtitle("2018")+
  theme_light()

gg18<- gg18 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
          axis.text.y=element_blank(),axis.ticks=element_blank(),
          axis.title.x=element_blank(),
          axis.title.y=element_blank(),
          panel.background=element_blank(),panel.grid.major=element_blank())


gg10<- ggplot(life.exp.map[life.exp.map$year==2010,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.8),na.value = "grey")+
  ggtitle("2010")+
  theme_light()

gg10<- gg10 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())

gg00<- ggplot(life.exp.map[life.exp.map$year==2000,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.8),na.value = "grey")+
  ggtitle("2000")+
  theme_light()

gg00<- gg00 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())

gg90<- ggplot(life.exp.map[life.exp.map$year==1990,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.8),na.value = "grey")+
  ggtitle("1990")+
  theme_light()

gg90<- gg90 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())

gg80<- ggplot(life.exp.map[life.exp.map$year==1980,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.8),na.value = "grey")+
  ggtitle("1980")+
  theme_light()

gg80<- gg80 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())

gg70<- ggplot(life.exp.map[life.exp.map$year==1970,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.8),na.value = "grey")+
  ggtitle("1970")+
  theme_light()

gg70<- gg70 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())



gg<-ggarrange(gg18,gg10,gg00,gg90,gg80,ncol = 2, nrow = 3,legend="bottom",common.legend = T)
gg<-annotate_figure(gg,top="Competition between USA and China")


ggsave(filename="inst/Paper/UNPC_Map_CHN_USA.jpeg",
       plot=gg,
       pointsize = 24,
       width = 18 ,
       height = 16,
       scale = 0.5,
       dpi = 1200)



  ## RUSUSA----
unpc_USA_RUS<-unpc_final[unpc_final$competitor1=="USA"&unpc_final$competitor2=="RUS",]


unpc <- unpc_USA_RUS %>%
  select(competed, pi,year) %>%
  rename(ISO = competed, Competition = pi)

codes<- read_excel("inst/extdata/country_codes.xlsx", sheet = 1,skip=1)
codes<-codes[,c("official_name_en","ISO3166-1-Alpha-3")]
codes$region<-codes$official_name_en
codes$ISO<-codes$`ISO3166-1-Alpha-3`
x <- left_join(unpc,codes, by = "ISO")
world_map <- map_data("world")
life.exp.map <- left_join(x, world_map, by = "region")

b <- c(0,0.5,1)
gg18<- ggplot(life.exp.map[life.exp.map$year==2018,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.8),na.value = "grey")+
  ggtitle("2018")+
  theme_light()

gg18<- gg18 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())


gg10<- ggplot(life.exp.map[life.exp.map$year==2010,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.8),na.value = "grey")+
  ggtitle("2010")+
  theme_light()

gg10<- gg10 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())

gg00<- ggplot(life.exp.map[life.exp.map$year==2000,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.8),na.value = "grey")+
  ggtitle("2000")+
  theme_light()

gg00<- gg00 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())

gg90<- ggplot(life.exp.map[life.exp.map$year==1990,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.8),na.value = "grey")+
  ggtitle("1990")+
  theme_light()

gg90<- gg90 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())

gg80<- ggplot(life.exp.map[life.exp.map$year==1980,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.8),na.value = "grey")+
  ggtitle("1980")+
  theme_light()

gg80<- gg80 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())

gg70<- ggplot(life.exp.map[life.exp.map$year==1970,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.8),na.value = "grey")+
  ggtitle("1970")+
  theme_light()

gg70<- gg70 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())


gg<-ggarrange(gg18,gg10,gg00,gg90,gg80,gg70,ncol = 2, nrow = 3,legend="bottom",common.legend = T)
gg<-annotate_figure(gg,top="Competition between USA and Russia")

ggsave(filename="inst/Paper/UNPC_Map_RUS_USA.jpeg",
       plot=gg,
       pointsize = 24,
       width = 18 ,
       height = 16,
       scale = 0.5,
       dpi = 1200)


  ## FRAUSA----
unpc_USA_FRA<-unpc_final[unpc_final$competitor1=="USA"&unpc_final$competitor2=="FRA",]


unpc <- unpc_USA_FRA %>%
  select(competed, pi,year) %>%
  rename(ISO = competed, Competition = pi)

codes<- read_excel("inst/extdata/country_codes.xlsx", sheet = 1,skip=1)
codes<-codes[,c("official_name_en","ISO3166-1-Alpha-3")]
codes$region<-codes$official_name_en
codes$ISO<-codes$`ISO3166-1-Alpha-3`
x <- left_join(unpc,codes, by = "ISO")
world_map <- map_data("world")
life.exp.map <- left_join(x, world_map, by = "region")

b <- c(0,0.5,1)


gg18<- ggplot(life.exp.map[life.exp.map$year==2018,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.8),na.value = "grey")+
  ggtitle("2018")+
  theme_light()

gg18

gg18<- gg18 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())


gg10<- ggplot(life.exp.map[life.exp.map$year==2010,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.8),na.value = "grey")+
  ggtitle("2010")+
  theme_light()

gg10<- gg10 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())

gg00<- ggplot(life.exp.map[life.exp.map$year==2000,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.8),na.value = "grey")+
  ggtitle("2000")+
  theme_light()

gg00<- gg00 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())

gg90<- ggplot(life.exp.map[life.exp.map$year==1990,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.8),na.value = "grey")+
  ggtitle("1990")+
  theme_light()

gg90<- gg90 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())

gg80<- ggplot(life.exp.map[life.exp.map$year==1980,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.8),na.value = "grey")+
  ggtitle("1980")+
  theme_light()

gg80<- gg80 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())

gg70<- ggplot(life.exp.map[life.exp.map$year==1970,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.8),na.value = "grey")+
  ggtitle("1970")+
  theme_light()

gg70<- gg70 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())


gg<-ggarrange(gg18,gg10,gg00,gg90,gg80,gg70,ncol = 2, nrow = 3,legend="bottom",common.legend = T)
gg<-annotate_figure(gg,top="Competition between USA and France")

ggsave(filename="inst/Paper/UNPC_Map_FRA_USA.jpeg",
       plot=gg,
       pointsize = 24,
       width = 18 ,
       height = 16,
       scale = 0.5,
       dpi = 1200)


#Plot Sb-Saharan Africa Countries CHN USA / RUS USA / USA FRA----

load("inst/extdata/unpc_final.RData")

  ## CHN USA----
unpc_USA_CHN<-unpc_final[unpc_final$competitor1=="USA"&unpc_final$competitor2=="CHN",]


unpc <- unpc_USA_CHN %>%
  select(competed, pi,year) %>%
  rename(ISO = competed, Competition = pi)

codes<- read_excel("inst/extdata/country_codes.xlsx", sheet = 1,skip=1)
codes<-codes[,c("official_name_en","ISO3166-1-Alpha-3","Sub-region Name")]
codes$region<-codes$official_name_en
codes$ISO<-codes$`ISO3166-1-Alpha-3`
codes$sub_region<-codes$`Sub-region Name`
x <- left_join(unpc,codes, by = "ISO")
world_map <- map_data("world")
life.exp.map <- left_join(x, world_map, by = "region")
life.exp.map <- life.exp.map[life.exp.map$sub_region=="Sub-Saharan Africa",]

b <- c(0,0.5,1)
gg18<- ggplot(life.exp.map[life.exp.map$year==2018,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.6),na.value = "grey")+
  ggtitle("2018")+
  theme_light()

gg18<- gg18 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())


gg10<- ggplot(life.exp.map[life.exp.map$year==2010,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.6),na.value = "grey")+
  ggtitle("2010")+
  theme_light()

gg10<- gg10 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())

gg00<- ggplot(life.exp.map[life.exp.map$year==2000,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.6),na.value = "grey")+
  ggtitle("2000")+
  theme_light()

gg00<- gg00 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())

gg90<- ggplot(life.exp.map[life.exp.map$year==1990,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.6),na.value = "grey")+
  ggtitle("1990")+
  theme_light()

gg90<- gg90 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())

gg80<- ggplot(life.exp.map[life.exp.map$year==1980,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.6),na.value = "grey")+
  ggtitle("1980")+
  theme_light()

gg80<- gg80 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())

gg70<- ggplot(life.exp.map[life.exp.map$year==1970,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.6),na.value = "grey")+
  ggtitle("1970")+
  theme_light()

gg70<- gg70 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())



gg<-ggarrange(gg18,gg10,gg00,gg90,gg80,ncol = 2, nrow = 3,legend="bottom",common.legend = T)
gg<-annotate_figure(gg,top="Competition USA vs. China")


ggsave(filename="inst/Paper/UNPC_Map_CHN_USA_SSA.jpeg",
       plot=gg,
       pointsize = 24,
       width = 10 ,
       height = 16,
       scale = 0.5,
       dpi = 1200)



  ## RUSUSA----
unpc_USA_RUS<-unpc_final[unpc_final$competitor1=="USA"&unpc_final$competitor2=="RUS",]


unpc <- unpc_USA_RUS %>%
  select(competed, pi,year) %>%
  rename(ISO = competed, Competition = pi)

codes<- read_excel("inst/extdata/country_codes.xlsx", sheet = 1,skip=1)
codes<-codes[,c("official_name_en","ISO3166-1-Alpha-3","Sub-region Name")]
codes$region<-codes$official_name_en
codes$ISO<-codes$`ISO3166-1-Alpha-3`
codes$sub_region<-codes$`Sub-region Name`
x <- left_join(unpc,codes, by = "ISO")
world_map <- map_data("world")
life.exp.map <- left_join(x, world_map, by = "region")
life.exp.map <- life.exp.map[life.exp.map$sub_region=="Sub-Saharan Africa",]


b <- c(0,0.5,1)
gg18<- ggplot(life.exp.map[life.exp.map$year==2018,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.6),na.value = "grey")+
  ggtitle("2018")+
  theme_light()

gg18<- gg18 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())


gg10<- ggplot(life.exp.map[life.exp.map$year==2010,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.6),na.value = "grey")+
  ggtitle("2010")+
  theme_light()

gg10<- gg10 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())

gg00<- ggplot(life.exp.map[life.exp.map$year==2000,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.6),na.value = "grey")+
  ggtitle("2000")+
  theme_light()

gg00<- gg00 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())

gg90<- ggplot(life.exp.map[life.exp.map$year==1990,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.6),na.value = "grey")+
  ggtitle("1990")+
  theme_light()

gg90<- gg90 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())

gg80<- ggplot(life.exp.map[life.exp.map$year==1980,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.6),na.value = "grey")+
  ggtitle("1980")+
  theme_light()

gg80<- gg80 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())

gg70<- ggplot(life.exp.map[life.exp.map$year==1970,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.6),na.value = "grey")+
  ggtitle("1970")+
  theme_light()

gg70<- gg70 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())


gg<-ggarrange(gg18,gg10,gg00,gg90,gg80,gg70,ncol = 2, nrow = 3,legend="bottom",common.legend = T)
gg<-annotate_figure(gg,top="Competition USA vs. Russia")

ggsave(filename="inst/Paper/UNPC_Map_RUS_USA_SSA.jpeg",
       plot=gg,
       pointsize = 24,
       width = 10 ,
       height = 16,
       scale = 0.5,
       dpi = 1200)


  ## FRAUSA----
unpc_USA_FRA<-unpc_final[unpc_final$competitor1=="USA"&unpc_final$competitor2=="FRA",]


unpc <- unpc_USA_FRA %>%
  select(competed, pi,year) %>%
  rename(ISO = competed, Competition = pi)

codes<- read_excel("inst/extdata/country_codes.xlsx", sheet = 1,skip=1)
codes<-codes[,c("official_name_en","ISO3166-1-Alpha-3","Sub-region Name")]
codes$region<-codes$official_name_en
codes$ISO<-codes$`ISO3166-1-Alpha-3`
codes$sub_region<-codes$`Sub-region Name`
x <- left_join(unpc,codes, by = "ISO")
world_map <- map_data("world")
life.exp.map <- left_join(x, world_map, by = "region")
life.exp.map <- life.exp.map[life.exp.map$sub_region=="Sub-Saharan Africa",]



gg18<- ggplot(life.exp.map[life.exp.map$year==2018,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.6),na.value = "grey")+
  ggtitle("2018")+
  theme_light()

gg18

gg18<- gg18 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())


gg10<- ggplot(life.exp.map[life.exp.map$year==2010,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.6),na.value = "grey")+
  ggtitle("2010")+
  theme_light()

gg10<- gg10 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())

gg00<- ggplot(life.exp.map[life.exp.map$year==2000,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.6),na.value = "grey")+
  ggtitle("2000")+
  theme_light()

gg00<- gg00 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())

gg90<- ggplot(life.exp.map[life.exp.map$year==1990,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.6),na.value = "grey")+
  ggtitle("1990")+
  theme_light()

gg90<- gg90 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())

gg80<- ggplot(life.exp.map[life.exp.map$year==1980,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.6),na.value = "grey")+
  ggtitle("1980")+
  theme_light()

gg80<- gg80 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())

gg70<- ggplot(life.exp.map[life.exp.map$year==1970,], aes(long, lat, group = group)) +
  geom_polygon(aes(fill = Competition ), color = "white")+
  scale_fill_gradientn(colors = viridis_pal(option="B")(8), limits=c(0, 0.6),na.value = "grey")+
  ggtitle("1970")+
  theme_light()

gg70<- gg70 + theme(axis.line=element_blank(),axis.text.x=element_blank(),
                    axis.text.y=element_blank(),axis.ticks=element_blank(),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank(),
                    panel.background=element_blank(),panel.grid.major=element_blank())


gg<-ggarrange(gg18,gg10,gg00,gg90,gg80,gg70,ncol = 2, nrow = 3,legend="bottom",common.legend = T)
gg<-annotate_figure(gg,top="Competition USA vs. France")

ggsave(filename="inst/Paper/UNPC_Map_FRA_USA_SSA.jpeg",
       plot=gg,
       pointsize = 24,
       width = 10,
       height = 16,
       scale = 0.5,
       dpi = 1200)



#Plot (epsilon und Delta):----


unpc_plot<-unpc_final

unpc <- unpc_plot %>%
  select(competed, pi,year,delta,epsilon) %>%
  rename(ISO = competed, Competition = pi)

codes<- read_excel("inst/extdata/country_codes.xlsx", sheet = 1,skip=1)
codes<-codes[,c("official_name_en","ISO3166-1-Alpha-3","Sub-region Name")]
codes$region<-codes$official_name_en
codes$ISO<-codes$`ISO3166-1-Alpha-3`
codes$sub_region<-codes$`Sub-region Name`
unpc <- left_join(unpc,codes, by = "ISO")


g1 <- ggplot(unpc, aes(delta, epsilon)) +
  geom_point(color="grey") +
  geom_smooth(aes(colour=year),
              method = "lm",
              show.legend = FALSE) +
  facet_wrap(~sub_region, scales = "free")+
  ggtitle("Scatterplot of Delta and Epsilon Indicators (fit=linear regression)")

g1

g2 <- ggplot(unpc, aes(delta, pi)) +
  geom_point(color="grey") +
  geom_smooth(aes(color = year),
              method = "lm",
              show.legend = FALSE) +
  facet_wrap(~sub_region, scales = "free")+
  ggtitle("Scatterplot of Delta and Pi Indicators (fit=linear regression)")


g2

ggsave(filename="inst/Paper/epsilondelta.jpeg",
       plot=g1,
       pointsize = 24,
       width = 18 ,
       height = 10,
       scale = 0.5,
       dpi = 800)


#Rayshader
library(rayshader)
library(imager)


gg <-
  ggplot(unpc_final, aes(epsilon, delta)) +
  geom_point(aes(col = pi)) +
  scale_color_viridis_c(option = "A")
#scale_color_continuous(limits = c(0, 8))

plot_gg(gg, width = 3.5, raytrace = FALSE, preview = TRUE)

plot_gg(gg,
        width = 3.5,
        windowsize = c(1600, 1600),
        height = 4,
        scale = 300,
        multicore = TRUE,
        fov = 70,
        zoom = 0.4,
        theta = 330,
        phi = 40)
render_snapshot(clear = TRUE)



# Correlation Plot:----
# Plot countries (descriptively analyze)


# Convergent vality assessment through case studies (US, CHN, FR, TZA, ZMB, ZAF, ISR)

unpc_case<-unpc_final[unpc_final$competed%in%c("ZAF","NGA","ZMB","AGO"),]

#USA CHN
a<- ggplot(unpc_case[unpc_case$competitor1=="USA"&unpc_case$competitor2=="CHN",], aes(x = year, y=pi,color=competed)) + geom_line()+
  ggtitle("Competition between USA and China")+
  ylab("UNPC")+
  xlab("Year")+
  ylim(0, 0.5)+
  theme_light()
a

# USA RUS
b<- ggplot(unpc_case[unpc_case$competitor1=="USA"&unpc_case$competitor2=="RUS",], aes(x = year, y=pi,color=competed)) + geom_line()+
  ggtitle("Competition between USA and Russia")+
  ylab("UNPC")+
  xlab("Year")+
  ylim(0, 0.5)+
  theme_light()
b

#USA FRA

c<- ggplot(unpc_case[unpc_case$competitor1=="USA"&unpc_case$competitor2=="FRA",], aes(x = year, y=pi,color=competed)) + geom_line()+
  ggtitle("Competition between USA and France")+
  ylab("UNPC")+
  xlab("Year")+
  ylim(0, 0.5)+
  theme_light()
c


#FRA RUS
d<- ggplot(unpc_case[unpc_case$competitor1=="FRA"&unpc_case$competitor2=="RUS",], aes(x = year, y=pi,color=competed)) + geom_line()+
  ggtitle("Competition between France and Russia")+
  ylab("UNPC")+
  xlab("Year")+
  ylim(0, 0.5)+
  theme_light()
d



#CHN RUS
e<- ggplot(unpc_case[unpc_case$competitor1=="CHN"&unpc_case$competitor2=="RUS",], aes(x = year, y=pi,color=competed)) + geom_line()+
  ggtitle("Competition between China and Russia")+
  ylab("UNPC")+
  xlab("Year")+
  ylim(0, 0.5)+
  theme_light()
e


gg<-ggarrange(a,b,c,d,e,ncol = 2, nrow = 3,legend="bottom",common.legend = T)
gg<-annotate_figure(gg,top="Time Development of the UNPC")

gg

ggsave(filename="inst/Paper/time_unpc.jpeg",
       plot=gg,
       pointsize = 24,
       width = 18 ,
       height = 10,
       scale = 0.5,
       dpi = 800)

#Plot (epsilon und Delta):
library(rayshader)
library(imager)


gg <-
  ggplot(unpc, aes(epsilon, delta)) +
  geom_point(aes(col = pi)) +
  scale_color_viridis_c(option = "A")+
  geom_point(data = data.frame(epsilon = 0.25,
                               delta = 0.25,
                               pi ))
#scale_color_continuous(limits = c(0, 8))

plot_gg(gg, width = 3.5, raytrace = FALSE, preview = TRUE)

plot_gg(gg,
        width = 3.5,
        windowsize = c(1600, 1600),
        height = 4,
        scale = 300,
        multicore = TRUE,
        fov = 70,
        zoom = 0.4,
        theta = 330,
        phi = 40)
render_snapshot(clear = TRUE)





# Applied Case: Foreign Aid ----
LuMesserschmidt/UNPC documentation built on Nov. 25, 2019, 8:17 a.m.