Skip to content

Commit

Permalink
new version
Browse files Browse the repository at this point in the history
  • Loading branch information
yiqiang committed May 6, 2018
1 parent a82f9b1 commit 6b043e1
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 52 deletions.
Binary file added data/ajmatrix.rda
Binary file not shown.
1 change: 1 addition & 0 deletions data/axis_allies.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
In the Wolrd War II, The Axis powers, also known as the Axis and the Rome–Berlin–Tokyo Axis, were the nations that fought in World War II against the Allied forces. The Axis powers agreed on their opposition to the Allies, but did not completely coordinate their activity. The Axis grew out of the diplomatic efforts of Germany, Italy, and Japan to secure their own specific expansionist interests in the mid-1930s.<br>The Allies of World War II, called the United Nations from the 1 January 1942 declaration, were the countries that together opposed the Axis powers.The leaders of the "Big Three"—the Soviet Union, the United Kingdom, and the United States—controlled Allied strategy.<br><br>In the following Chord Diagram, different colors indicate military alliances. Clicking on one country, the Barplot on the right side will show cities being attacked associated to (may not be in) that country's force.
File renamed without changes.
Binary file added data/groupColors.rda
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
69 changes: 69 additions & 0 deletions data_preprocessing.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
library(tidyverse)
####Data Preprocessing####0
setwd("/media/yiqiang/D/USF/622DataViz/final project/finalproject/dataviz/")
## Part 1: parse data to get map data
df <- read_csv("data/operations.csv")
map_data <- df %>% filter(is.na(Country) == FALSE & is.na(`Target Longitude`) == FALSE & is.na(`Takeoff Longitude`) == FALSE) %>%
filter(`Takeoff Latitude` != '4248', `Takeoff Longitude` != 1355) %>%
filter(`Target Longitude` > 0, `Takeoff Longitude` > 0, `Target Latitude` > 0, `Target Longitude` <=360) %>%
mutate(id=1:n())
map_data$`Mission Date` <- as.Date(anytime::anydate(map_data$`Mission Date`))
map_data$start_week <- cut(map_data$`Mission Date`, "week")
flight_route <- gcIntermediate(map_data[ , c("Takeoff Longitude", "Takeoff Latitude")],
map_data[ , c("Target Longitude", "Target Latitude")],
n=20,
addStartEnd=TRUE,
sp=TRUE)
write.csv(map_data, file = "data/simplified_data.csv", row.names = FALSE)
save(flight_route, file="data/flight_route.rda")

## Part 2: parse data to get obs count ts data
Mission <- df %>% select(`Mission Date`, `Mission ID`) %>%
filter(is.na(`Mission Date`) == F)
Mission$`Mission Date` <- as.Date(anytime::anydate(Mission$`Mission Date`))
Mission$start_month <- cut(Mission$`Mission Date`, "month")
Mission$start_month = as.Date(Mission$start_month)
Mission <- Mission %>% arrange(`Mission Date`)
monthly_ct <- Mission %>% group_by(start_month) %>% summarise('ct' = n())
write.csv(monthly_ct, file = "data/monthly_obs_count.csv", row.names = FALSE)

## Part 3: chorddiag data
diag_data <- df %>% select(`Target Country`, Country) %>% group_by(., `Target Country`, Country) %>%
count() %>% filter(., is.na(Country) == FALSE, is.na(`Target Country`) == FALSE, n>100,
`Target Country` != 'UNKNOWN OR NOT INDICATED')
library(igraph)
diag_data <- as.data.frame(diag_data)
diag_data[, 1] <- as.character(diag_data[, 1])
diag_data[, 2] <- as.character(diag_data[, 2])
diag_data[, 3] <- as.numeric(diag_data[, 3])
diag_data <- as.matrix(diag_data)
diag_net <- igraph::graph.edgelist(diag_data[,1:2])
E(diag_net)$weights <- as.numeric(diag_data[,3])
ajmatrix <- get.adjacency(diag_net,attr='weights', sparse = FALSE)
Occupied <- toupper(c("Albania", "Belgium", "Czechoslovakia", "Denmark", "Estonia",
"Ethiopia", "France", "Greece", "Luxemburg", "Netherlands",
"Norway", "Philippine Islands", "Poland", "Yugoslavia"))
Axis <- toupper(c("Bulgaria", "Finland", "Germany", "Hungary", "Italy", "Japan", "Romania"))
Allies <- toupper(c("Argentina", "Australia", "Bolivia", "Brazil", "Canada", "China",
"Chile", "Columbia", "Costa Rica", "Cuba", "France", "India", "Iraq",
"Lebanon", "Mexico", "New Zealand", "Paraguay", "South Africa", "Soviet Union",
"Great Britain", "USA"))
Neutral <- toupper(c("Andorra", "Ireland", "Liechtenstein", "Portugal", "Spain", "Sweden",
"Switzerland", "Turkey", "Uruguay", "Vatican City"))
groupColors = c()
for(country in dimnames(ajmatrix)[[1]]){
if(country %in% Occupied){
groupColors = c(groupColors, "#636363")
}else if(country %in% Axis){
groupColors = c(groupColors, "#25338a")
}else if (country %in% Allies){
groupColors = c(groupColors, "#9ecae1")
}else if (country %in% Neutral){
groupColors = c(groupColors, "#bdbdbd")
}else{
groupColors = c(groupColors, "#bdbdbd")
}
}
save(ajmatrix, file = "data/ajmatrix.rda")
save(groupColors, file="data/groupColors.rda")

108 changes: 66 additions & 42 deletions server.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,26 @@ library(mapview)
library(crosstalk)
library(sf)
library(plotly)
####Data Preprocessing####

## Part 1: parse data to get map data

# df <- read_csv("operations.csv")
# df <- df %>% filter(is.na(Country) == FALSE & is.na(`Target Longitude`) == FALSE & is.na(`Takeoff Longitude`) == FALSE) %>%
# filter(`Takeoff Latitude` != '4248', `Takeoff Longitude` != 1355) %>%
# filter(`Target Longitude` > 0, `Takeoff Longitude` > 0, `Target Latitude` > 0, `Target Longitude` <=360) %>%
# mutate(id=1:n())
# df$`Mission Date` <- as.Date(anytime::anydate(df$`Mission Date`))
# df$start_week <- cut(df$`Mission Date`, "week")
# flight_route <- gcIntermediate(df[ , c("Takeoff Longitude", "Takeoff Latitude")],
# df[ , c("Target Longitude", "Target Latitude")],
# n=20,
# addStartEnd=TRUE,
# sp=TRUE)
# write.csv(df, file = "simplified_data.csv", row.names = FALSE)

## Part 2: parse data to get obs count ts data

# df <- read_csv("operations.csv")
# Mission <- df %>% select(`Mission Date`, `Mission ID`) %>%
# filter(is.na(`Mission Date`) == F)
# Mission$`Mission Date` <- as.Date(anytime::anydate(Mission$`Mission Date`))
# Mission$start_month <- cut(Mission$`Mission Date`, "month")
# Mission$start_month = as.Date(Mission$start_month)
# Mission <- Mission %>% arrange(`Mission Date`)
# monthly_ct <- Mission %>% group_by(start_month) %>% summarise('ct' = n())
# write.csv(monthly_ct, file = "monthly_obs_count.csv", row.names = FALSE)

df <- read_csv("simplified_data.csv")
load("flight_route.rda") # to accelerate the loading speed
library(chorddiag)
####Loading Data####
raw_data <- read_csv("data/operations.csv")
df <- read_csv("data/simplified_data.csv")
load("data/flight_route.rda") # to accelerate the loading speed
df_takeoff <- df %>% st_as_sf(., coords = c("Takeoff Longitude", "Takeoff Latitude"))
df_target <- df %>% st_as_sf(., coords = c("Target Longitude", "Target Latitude"))

ts_df <- read_csv("monthly_obs_count.csv")


ts_df <- read_csv("data/monthly_obs_count.csv")
load("data/groupColors.rda")
load("data/ajmatrix.rda")
allies_axis_text <- read_file("data/axis_allies.txt")
####Sever####
shinyServer(function(input, output, session) {
target_map <- SharedData$new(df_target, key = ~id, group = "grp1")
takeoff_map <- SharedData$new(df_takeoff, key = ~id, group = "grp2")
lf <- leaflet(target_map) %>%
addTiles(urlTemplate = "https://api.mapbox.com/styles/v1/yiqiang/cjglw67l500262tqmguz0efum/tiles/256/{z}/{x}/{y}?access_token=pk.eyJ1IjoieWlxaWFuZyIsImEiOiJjamI2MjJ2aDgzZTJiMzdvMTEza25vN3czIn0.da7McqdO9XgggUE0LTCx3Q"
) %>% addPolylines(data = flight_route, opacity = 0.3, fillOpacity = 0.2, color = "black", stroke = TRUE, weight = 1) %>%
addCircleMarkers(fillOpacity = 0.1, color = "red", radius = 1, group = "grp1") %>%
addCircleMarkers(data = takeoff_map, fillOpacity = 0.1, color = "blue", radius = 1, group = "grp2") %>%
addCircleMarkers(fillOpacity = 0.1, color = "#08519c", radius = 1, group = "grp1") %>%
addCircleMarkers(data = takeoff_map, fillOpacity = 0.1, color = "#969696", radius = 1, group = "grp2") %>%
fitBounds(lng1=65.298593, lat1 = -15.124991, lng2 = -7.286442, lat2 = -173.134400) %>%
setView(lat=24.719341, lng=68.718978, zoom=3) %>%
addMiniMap(tiles = "https://api.mapbox.com/styles/v1/yiqiang/cjglw67l500262tqmguz0efum/tiles/256/{z}/{x}/{y}?access_token=pk.eyJ1IjoieWlxaWFuZyIsImEiOiJjamI2MjJ2aDgzZTJiMzdvMTEza25vN3czIn0.da7McqdO9XgggUE0LTCx3Q",
Expand All @@ -71,12 +44,13 @@ shinyServer(function(input, output, session) {
observe({
tmp_selected_target <- target_map$data(withSelection = TRUE) %>% filter(selected_ == TRUE)
tmp_selected_takeoff <- takeoff_map$data(withSelection = TRUE) %>% filter(selected_ == TRUE)


output$barplot <- renderPlotly(({
tmp_data <- tmp_selected_target %>% group_by(`Aircraft Series`) %>% count() %>% arrange(n)
tmp_data$Aircraft_Series <- tmp_data$`Aircraft Series`
p <- ggplot(tmp_data) +
aes(x = `Aircraft_Series`, y = `n`, fill=`Aircraft_Series`,
aes(x = `Aircraft_Series`, y = `n`,
text=sprintf("Aircraft Series: %s<br>Num: %s", `Aircraft_Series`, `n`)) +
geom_bar(stat = 'identity') +
ylab("Count") + xlab("") + ggtitle("Aircraft Series Barplot") +
Expand All @@ -94,19 +68,25 @@ shinyServer(function(input, output, session) {
}))

output$piechart <- renderPlotly(({
tmp_data <- tmp_selected_target %>% group_by(`Target Country`) %>% count()
colors <- c(rev(c("#deebf7", "#c6dbef", "#9ecae1", "#6baed6", "#4292c6", "#2171b5", "#08519c")),
rep("#deebf7", 20))
tmp_data <- tmp_selected_target %>% group_by(`Target Country`) %>% count() %>% arrange(., desc(n))
tmp_data <- as.data.frame(tmp_data)
textpos <- ifelse(tmp_data$n/sum(tmp_data$n, na.rm = TRUE) < 0.04, "none", "auto")
m <- list(l = 50, r = 50, b = 20, t = 55, pad = 2)
p2 <- plot_ly(tmp_data, labels = ~`Target Country`, values = ~n, type = 'pie',
textposition = textpos, textinfo = 'label+percent') %>%
insidetextfont = list(color = '#FFFFFF'),
textposition = textpos, textinfo = 'label+percent',
marker = list(colors = colors,
line = list(color = '#FFFFFF', width = 0.5)
)) %>%
layout(paper_bgcolor = "#f0f0f0", title = "<b>Attacked Country Pie Chart<b>",
margin = m, font = list(size = 14))
p2$elementId <- NULL
p2
}))
leafletProxy("mymap") %>% clearControls()
leafletProxy("mymap") %>% addLegend("topright", colors= c("blue", "red"),
leafletProxy("mymap") %>% addLegend("topright", colors= c("#969696", "#08519c"),
labels=c(paste("Takeoff:", nrow(tmp_selected_takeoff)), paste("Target:", nrow(tmp_selected_target))),
title="Num",
opacity = 0.5)
Expand All @@ -121,6 +101,50 @@ shinyServer(function(input, output, session) {
p3$elementId <- NULL
p3
})

output$chorddiag <- renderChorddiag(
chorddiag(ajmatrix, groupColors = groupColors, groupnamePadding = 40,
showGroupnames = FALSE, tooltipGroupConnector = ' <- ',
clickAction = "Shiny.onInputChange('sourceIndex', d.source.index+1);
Shiny.onInputChange('targetIndex', d.target.index+1);",
margin = 35, width = 10)
)

groupNames <- dimnames(ajmatrix)[[1]]

city_data <- reactive({
if(is.null(input$sourceIndex)){
t <- raw_data %>% filter(., Country == "USA",
`Target Country` == "GERMANY") %>%
group_by(., `Target City`) %>% count() %>% arrange(desc(n)) %>% head(10) %>% as.data.frame()
return(t)
}else{
t <- raw_data %>% filter(., Country == groupNames[input$targetIndex],
`Target Country` == groupNames[input$sourceIndex]) %>%
group_by(., `Target City`) %>% count() %>% arrange(desc(n)) %>% head(10) %>% as.data.frame()
return(t)
}
})
observe({
tmp_city_data <- city_data()
output$barplot2 <- renderPlotly({p3 <- ggplot(tmp_city_data) +
aes(x = `Target City`, y = `n`,
text=sprintf("Target City: %s<br>Num: %s", `Target City`, `n`)) +
geom_bar(stat = 'identity') +
ylab("Count") + xlab("") + ggtitle("Target City Barplot") +
theme(axis.text.x = element_text(angle = 20, hjust = 1, colour = "#4f4f4f"),
plot.title = element_text(size = 14, face = "bold", colour = "#4f4f4f"),
plot.background = element_rect(fill = "white"),
plot.margin = unit(c(0.5,0.5, 0.5, 0.5), "cm"),
axis.title=element_text(size=12),
legend.position="none")
p3 <- ggplotly(p3, tooltip="text")
p3$elementId <- NULL
p3})

})

output$allies_axis <- renderText(allies_axis_text)

})

37 changes: 27 additions & 10 deletions ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ library(mapview)
library(crosstalk)
library(plotly)
library(shinydashboard)
# df <- read_csv("operations.csv")
# df <- st_as_sf(df) %>% filter(is.na(Country) == FALSE & is.na(`Target Longitude`) == FALSE &
# is.na(`Takeoff Longitude`) == FALSE) %>%
# filter(`Takeoff Latitude` != '4248', `Takeoff Longitude` != 1355) %>%
# filter(`Target Longitude` > 0, `Takeoff Longitude` > 0)
# df <- SharedData$new(df)


shinyUI(dashboardPage(
dashboardHeader(title = "World War II Viz"),
Expand Down Expand Up @@ -56,11 +51,33 @@ shinyUI(dashboardPage(
),
tabItem(
tabName = "time",
# fluidRow(box(column(width=12,
# img(src="world-war-ii-facts.jpg", width=1000)))),

fluidRow(
box(
plotlyOutput("timeseries")
)
)
box(title = tags$b("Military Allies in WW2"),
status = "primary",
solidHeader = F,
collapsible = F,
width = 12,
fluidRow(
tags$style("#allies_axis {font-size:15px;
display:block; }"),
column(width = 9,
htmlOutput("allies_axis")),
column(width = 3, align = "center",
img(src = "allied powers.jpg", height = '170px', width = '220px'))
))
),
fluidRow(box(
splitLayout(
cellWidths = c("40%", "60%"),
cellArgs = list(style = "padding: 6px"),
chorddiagOutput("chorddiag", height = 450),
plotlyOutput("barplot2", height = 450)
), width = 12
))

)
)
# fluidRow(
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added www/allied powers.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added www/world-war-ii-facts.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6b043e1

Please sign in to comment.