# Load all we need
library("ggplot2")
## Warning: package 'ggplot2' was built under R version 4.0.5
library("sf")
## Warning: package 'sf' was built under R version 4.0.5
## Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1; sf_use_s2() is TRUE
library("rnaturalearth")
## Warning: package 'rnaturalearth' was built under R version 4.0.5
library("rnaturalearthdata")
## Warning: package 'rnaturalearthdata' was built under R version 4.0.5
library("shinyjs")
## Warning: package 'shinyjs' was built under R version 4.0.5
##
## Attaching package: 'shinyjs'
## The following objects are masked from 'package:methods':
##
## removeClass, show
library("dplyr")
## Warning: package 'dplyr' was built under R version 4.0.5
##
## 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
library("ggspatial")
## Warning: package 'ggspatial' was built under R version 4.0.5
library("shiny")
## Warning: package 'shiny' was built under R version 4.0.5
##
## Attaching package: 'shiny'
## The following object is masked from 'package:shinyjs':
##
## runExample
library("shinycssloaders")
## Warning: package 'shinycssloaders' was built under R version 4.0.5
library("shinythemes")
## Warning: package 'shinythemes' was built under R version 4.0.5
# Create Data Frame
world <- ne_countries(scale = "medium", returnclass = "sf")
alliance <- read.table("C:/Users/jebet/OneDrive/Escritorio/UEXT/ULB/Social network analysis/Directory/alliance_v4.1_by_directed_yearly.csv", sep=",", header=TRUE, na.strings="")
# Cut in these crazy BIG df ^^
# Get only each year one time
years <- alliance %>% group_by(year) %>% filter (! duplicated(year))
years <- years[["year"]]
# Get only each country once
countries <- alliance %>% group_by(state_name1) %>% filter (! duplicated(state_name1))
countries <- sort(countries[["state_name1"]])
# Selection vector of what we wanna know
relations <- c("defense","neutrality","nonaggression","entente")
# Function who select the colors who is gonna be selected
df_show <- function(fo_year, fo_country, fo_relation){
# Make the db with The selected country for the selected date
choise_df <- filter(alliance,year == fo_year & state_name1 == fo_country )
choise_df <- data.frame(state_name1 = choise_df$state_name1, state_name2 = choise_df$state_name2, defense = choise_df$defense, neutrality = choise_df$neutrality, entente = choise_df$entente, nonaggression =choise_df$nonaggression)
return(choise_df)
}
# Make the map and the db linked to it
Plot_World <- function(fo_year, fo_country, fo_relation){
# String year for the title
st_year <- toString(fo_year)
s_country <- is.element(world$sovereignt, fo_country)
# Make the db with The selected country for the selected date
choise_df <- filter(alliance,year == fo_year & state_name1 == fo_country )
choise_df <- data.frame(state_name1 = choise_df$state_name1, state_name2 = choise_df$state_name2, defense = choise_df$defense, neutrality = choise_df$neutrality, entente = choise_df$entente, nonaggression =choise_df$nonaggression)
# Choose witch kind of relations we want
bool_relations <- choise_df[fo_relation] == 1
v_relations <- choise_df$state_name2[bool_relations]
# Make the list of the countries who have this kind of relation
aes_relations <- is.element(world$sovereignt, v_relations)
# Plot the map
ggplot(data = world) +
geom_sf(aes(fill = aes_relations, color = s_country))+
scale_color_manual(values = c("black", "green"), labels = c("Frontier", "Selected"), name = "Country selection")+
scale_fill_manual(values = c("lightgrey", "lightgreen"),na.value = ("grey") ,labels = c("No relation", fo_relation), name = "Relations")+
theme(panel.grid.major = element_line(color = gray(.5), linetype = "dashed", size = 0.5), panel.background = element_rect(fill = "aliceblue"))+
ggtitle(paste("World map of relation of",fo_country), paste("In the year ",fo_year))+
xlab("Longitude") +
ylab("Latitude")
}
# Interactive interface with shiny
# Graphical Shiny that generate the HTML code
ui <- fluidPage(
navbarPage("Plot the World",
tabPanel("Relation Map", fluid = TRUE,
sidebarLayout(
sidebarPanel(
titlePanel("Make Selection"),
fluidRow(
column(8,
selectInput("cntry", "Select Country",countries),
selectInput("ye", "Select Date", years),
selectInput("rel", "Select Relation", relations)
),
)),
mainPanel(
fluidRow(
column(8, plotOutput('map',width = 800 ,height = 400 ))
),
# Generates The text dataframe
fluidRow(
column(8, dataTableOutput('table'))
)
)
)
),
tabPanel("Sna Analyse", fluid = TRUE,
titlePanel("Sna Analyse")
),
tabPanel("Data Informations", fluid = TRUE, icon = icon("chart-bar"),
titlePanel("Data Informations"),
column(6,
br(),
h4("All the data are imported from The Correlates of War © 2000-2022"),
h5(p(
"For more information please visit the ",
a("The Correlates of War",
href = "https://correlatesofwar.org/data-sets/formal-alliances"))),
h3(p("Overview")),
h5(p("he Correlates of War Formal Alliance data set seeks to identify each formal alliance between at least two states that fall into the classes of defense pact, neutrality or non-aggression treaty, or entente agreement. A defense pact (Type I) is the highest level of military commitment, requiring alliance members to come to each other’s aid militarily if attacked by a third party. As the labels imply, neutrality and non-aggression pacts (Type II) pledge signatories to either remain neutral in case of conflict or to not use or otherwise support the use of force against the other alliance members. Finally, ententes (Type III) provide for the least commitment and obligate members to consult in times of crisis or armed attack. Each alliance classifies the highest level of military support that an alliance member pledges to another alliance member. The data set provides the type, members, and appropriate dates of activity of each identified alliance."
)),
h5(p("Version 4.1 of the alliance data expands the original Singer and Small (1966) deffinitions of alliance types to include neutrality and non-aggression pacts separately. Neutrality pacts are now labeled as Type IIa alliances, and Non-Aggression pacts are labeled as Type IIb alliances. Alliance terms also distinguish between neutrality and non-aggression in the dyadic data."
)),
h5(p("In addition, the fourth version now differentiates between the overall alliance type and the terms of the alliance by specific member. In the alliance member list of data, for example, both the Singer and Small (1966) alliance type and the specific terms by alliance member are included. Only the terms operational for the specific dyad are included in the dyadic and direct-dyad data."
))
)
)
)
)
# Input and output that R on the server gat to generate the plot
server <- function(input, output){
output$table <- renderDataTable(df_show(fo_year = input$ye, fo_country = input$cntry, fo_relation = input$rel))
output$map <- renderPlot({Plot_World(fo_year = input$ye, fo_country = input$cntry, fo_relation = input$rel)})
}
# Launch the app and bridge Ui with Server
shinyApp(ui = ui, server = server)
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.