Improved charts with ggplot2

This commit is contained in:
Jakub Wajs 2019-05-16 00:37:48 +02:00
parent 83b050d711
commit 3b077f5269
9 changed files with 290 additions and 45 deletions

BIN
.RData

Binary file not shown.

226
.Rhistory
View File

@ -277,3 +277,229 @@ runApp()
runApp()
runApp()
runApp()
shiny::runApp()
install.packages("shinythemes")
runApp()
runApp()
runApp()
View(test)
View(balance)
View(bal)
balancePercTest <- bal %>% mutate(percentage = n/sum(.$n))
head(balancePercTest)
runApp()
runApp()
runApp()
runApp()
runApp()
balPerc <- getBalancePercentage(
getBalance(getMatchesList(input$first_team, input$second_team, football_data_ext))
)
runApp()
runApp()
runApp()
runApp()
View(football_data)
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
View(football_data)
getMatchesList("England", "Scotland", football_data)
getMatchesList("England", "Scotland", football_data) %>% arrange()
getMatchesList("England", "Scotland", football_data) %>% arrange(decreasing=TRUE)
getMatchesList("England", "Scotland", football_data) %>% arrange(.,decreasing=TRUE)
getMatchesList("England", "Scotland", football_data) %>% arrange(.$date)
getMatchesList("England", "Scotland", football_data) %>% arrange(.$date, decreasing=TRUE)
getMatchesList("England", "Scotland", football_data) %>% mutate(date = as.Date(date, "%Y-%m-%d")) %>% sort(date)
getMatchesList("England", "Scotland", football_data) %>% mutate(date = as.Date(date, "%Y-%m-%d")) %>% sort(date, decreasing = TRUE)
football_data %>% select(date)
football_data %>% select(date) %>% min()
football_data %>% select(date) %>% format(date, "%Y")
football_data %>% select(date) %>% format(.$date, "%Y")
football_data %>% select(date) %>% typeof()
football_data %>% select(date) %>% format(as.Date(date, format="%Y-%m-%d"),"%Y")
test <- football_data %>% select(date)
test[1]
test %>% head(1)
test %>% head(1) %>% format(., "%Y")
test %>% head(1) %>% typeof
test %>% head(1) %>% typeof()
football_data %>% select(date) %>% min()
football_data %>% select(date)
football_data %>% select(date) %>% aggregate(., by=list(.), max)
football_data %>% min(.$date)
football_data %>% select(date) %>% summarise(min = min(date))
football_data %>% select(date) %>% mutate(date = as.Date(date, "%Y-%m-%d")) %>% summarise(min = min(date))
football_data %>% select(date) %>% mutate(. = as.Date(date, "%Y-%m-%d")) %>% summarise(min = min(.))
football_data %>% select(date) %>% mutate(. = as.Date(., "%Y-%m-%d")) %>% summarise(min = min(.))
getDateFromData <- function(matchesData) {
return(
matchesData %>%
select(date) %>%
mutate(date = as.Date(date, "%Y-%m-%d"))
)
}
getDateFromData(football_data) %>%
summarise(max = max(date))
balance <- football_data %>%
count(winner, sort = TRUE)
return(balance)
runApp()
getDateFromData(football_data)
runApp()
runApp()
runApp()
runApp()
typeof(min_date_from)
getDateFromData(football_data) %>%
summarise(min = min(date)) %>% as.Date()
getDateFromData(football_data) %>%
summarise(min = min(date)) %>% as.Date(., format = "%Y-%m-%d")
getDateFromData(football_data) %>%
summarise(min = min(date)) %>% as.Date(date, format = "%Y-%m-%d")
getDateFromData(football_data) %>%
summarise(min = min(date)) %>% as.Date(date)
getDateFromData(football_data) %>%
summarise(min = min(date)) %>% strptime(date, "%Y-%m-%d")
getDateFromData(football_data) %>%
summarise(min = min(date)) %>% strptime(., "%Y-%m-%d")
getDateFromData(football_data) %>%
summarise(min = min(date))
getDateFromData(football_data) %>%
summarise(min = min(date)) %>% top_n(1)
getDateFromData(football_data) %>%
summarise(min = min(date)) %>% top_n(1) %>% typeof
runApp()
getDateFromData(football_data) %>%
summarise(min = min(date)) %>% slice(1)
getDateFromData(football_data) %>%
summarise(min = min(date)) %>% pull()
runApp()
getDateFromData(football_data) %>%
summarise(min = min(date)) %>% pull() %>% typeof()
getDateFromData(football_data) %>%
summarise(min = min(date)) %>% pull() %>% as.Date(., format = "%Y-%m-%d")
getDateFromData(football_data) %>%
summarise(min = min(date)) %>% pull() %>% as.Date(., format = "%Y-%m-%d") %>% typeof()
test <- getDateFromData(football_data) %>%
summarise(min = min(date)) %>% pull()
as.Date(test, "%Y-%m-%d")
as.Date(test, "%Y-%m-%d") %>% typeof()
test <- as.Date(test, "%Y-%m-%d")
typeof(test)
as.character(test)
test <- as.character(test)
test
typeof(test)
test <- as.Date(test, "%Y-%m-%d")
test
typeof(test)
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
test_date <- as.Date("2000-01-01", "%Y-%m-%d")
football_data %>% filter(as.Date(as.character(date, "%Y-%d-%m")) >= test_date)
runApp()
football_data %>%
filter(as.Date(as.character(date, "%Y-%d-%m")) >= as.Date(as.character(test_date, "%Y-%d-%m"))
& as.Date(as.character(date, "%Y-%d-%m")) <= as.Date(as.character(max_date_to, "%Y-%d-%m")))
football_data %>%
filter(as.Date(as.character(date, "%Y-%d-%m")) >= as.Date(as.character(test_date, "%Y-%d-%m")))
runApp()
install.packages("lubridate")
runApp()
runApp()
dateFrom <- as.Date(as.character(dateFrom, "%Y-%m-%d"))
runApp()
as_date(test)
test_date <- as_date(test)
test_date
typeof(test_date)
runApp()
runApp()
runApp()
football_data %>% filter(as_date(date) >= test_date)
runApp()
getMatchesList <- function(matchesData, first_team, second_team) {
return(
matchesData %>%
filter(home_team %in% c(first_team, second_team)
& away_team %in% c(first_team, second_team)) %>%
arrange()
)
}
getMatchesList(football_data, "England", "Scotland")
getMatchesList(football_data, "England", "Scotland") %>% filterByDate(min_date_from, max_date_to)
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
View(football_data_ext)
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
install.packages("shinydashboard")
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()
runApp()

View File

@ -28,5 +28,5 @@ getBalance <- function(football_data) {
getBalancePercentage <- function(balance) {
balancePerc <- balance %>%
mutate(percentage = n/sum(.$n) * 100)
mutate(percentage = n/sum(.$n))
}

View File

@ -27,27 +27,23 @@ getBalanceData <- function(input) {
balanceBetweenTeams <- function(input, output) {
output$balance <- renderPlot({
barplot(
getBalanceData(input)$n,
main = "Balance between teams",
col = rainbow(3),
names.arg = getBalanceData(input)$winner
)
ggplot(getFilteredResults(input), aes(x=as.factor(winner),
fill=as.factor(n))) +
geom_bar(fill = rgb(0.1,0.4,0.5,0.7)) +
theme_minimal() +
labs(x = "", y = "Number of matches")
})
}
# Balance percentage
balancePercentage <- function(input, output) {
output$balancePieChart <- renderPlot({
pie(
getBalancePercentage(
getBalanceData(input)
)$percentage,
main = "Match balance (percentage)",
labels = getBalancePercentage(
getBalanceData(input)
)$winner,
col = rainbow(3)
)
ggplot(getBalancePercentage(getBalanceData(input))) +
geom_bar(
aes(x="", y=percentage, fill=winner),
stat = "identity") +
coord_polar("y", start = 0) +
theme_void() +
geom_text(aes(x=1, y = cumsum(percentage) - percentage/2, label = percent(percentage)))
})
}

17
UI/BalanceComponent.R Normal file
View File

@ -0,0 +1,17 @@
# Balance component
balanceComponent <- function(pieChartSize, barplotSize)
{
fluidRow(
column(barplotSize,
wellPanel(
plotOutput("balance")
)
),
column(pieChartSize,
wellPanel(
plotOutput("balancePieChart")
)
)
)
}

30
UI/UI.R Normal file
View File

@ -0,0 +1,30 @@
#
# Main UI File
#
# Import files
source("UI/SidebarPanel.R")
source("UI/BalanceComponent.R")
# UI
ui <- fluidPage(
theme = shinytheme("cerulean"),
# Application title
headerPanel("International football results 1872-2018"),
# Sidebar layout elements
sidebarLayout(
teamsSidebarPanel(),
# Main panel
mainPanel(
fluidPage(
balanceComponent(6, 6),
fluidRow(
DT::dataTableOutput("results_pagination")
)
)
)
)
)

View File

@ -1,26 +0,0 @@
#
# Main UI File
#
# Import files
source("UI/SidebarPanel.R")
# UI
ui <- fluidPage(
theme = shinytheme("slate"),
# Application title
titlePanel("International football results 1872-2018"),
# Sidebar layout elements
sidebarLayout(
teamsSidebarPanel(),
# Main panel
mainPanel(
plotOutput("balance"),
plotOutput("balancePieChart"),
DT::dataTableOutput("results_pagination")
)
)
)

6
app.R
View File

@ -11,11 +11,13 @@ library(shiny)
library(dplyr)
library(shinythemes)
library(lubridate)
library(ggplot2)
library(scales)
# Load files
source("Data/Loader.R")
source("UI/main.R")
source("Server/main.R")
source("UI/UI.R")
source("Server/Server.R")
# Run the application
shinyApp(ui = ui, server = server)