Created
August 19, 2022 14:15
-
-
Save amzoss/12888cbc8f5684b59e291ef69d36a8ad to your computer and use it in GitHub Desktop.
Extra code from Data Matters 2022
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Showing how to replicate geom_bar() with a combination of | |
# count() and geom_col | |
inclusiveness_index %>% count(Continent) %>% | |
rename("count" = "n") %>% | |
ggplot() + | |
geom_col(mapping=aes(x=Continent, y=count)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Showing how to use a Color Brewer palette instead of manual color values. | |
color_scatter + | |
labels + | |
scale_color_brewer(palette = "Set2") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This version uses a logical test to color the dots. It also shows | |
# using scientific notation to indicate large numbers. | |
ggplot(gap, aes(x=gdp,y=life.exp)) + | |
geom_point(aes(size=pop, color=pop<200000000)) + | |
geom_smooth(method = "lm", se=FALSE, color="grey50") + | |
labs( | |
title="Averages across all years of the traditional Gapminder dataset", | |
x="Average GDP per capita (log 10)", | |
y="Average life expectany at birth", | |
size="Average total population", | |
color="Region" | |
) + | |
theme_bw() + | |
scale_x_log10(breaks=c(1000,10000)) + | |
scale_size_continuous(breaks=c(75e5,75e6,75e7), | |
labels=c("7.5 million", | |
"75 million", | |
"750 million")) + | |
geom_text(aes(label=country), | |
data=gap %>% dplyr::filter(pop > 200000000)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is the Shiny app we all worked on together as a group. | |
# For the code to work, it should be placed in the Day 2 folder | |
# in a subdirectory - you can call that new subdirectory anything you want. | |
# | |
# ------------------------- | |
# | |
# This is a Shiny web application. You can run the application by clicking | |
# the 'Run App' button above. | |
# | |
# Find out more about building applications with Shiny here: | |
# | |
# http://shiny.rstudio.com/ | |
# | |
library(shiny) | |
library(tidyverse) | |
got <- read_csv("../../data/got_ratings.csv") | |
inclusiveness_index <- read_excel('../../data/inclusiveness_index/global_data_for_website_2020.xlsx', | |
na="9999") %>% | |
rename_with(~ str_remove_all(.x, "[(|)]") %>% str_replace_all("[-| ]", ".")) %>% | |
dplyr::filter(Continent != "Antarctica") %>% | |
mutate(Continent = Continent %>% as_factor() %>% fct_infreq(), | |
Index.categories.2020 = Index.categories.2020 %>% as_factor() %>% | |
fct_relevel(c("High", "Medium-High", "Medium", "Medium-Low", "Low", "No data"))) | |
incl_bar_data <- inclusiveness_index %>% | |
count(Continent, Index.categories.2020) | |
palette_drop_down <- selectInput( | |
inputId = "got_palette", | |
label = "Select a color palette", | |
choices = c("Rainbow", "Purple-Green", "Yellow-Orange-Red")) | |
# Define UI for application that draws a histogram | |
ui <- navbarPage( | |
# overall page title | |
title = "Group-Designed Shiny App", | |
# First tab | |
tabPanel( | |
"Game of Thrones Heatmap", | |
sidebarLayout( | |
sidebarPanel( | |
palette_drop_down | |
), | |
mainPanel( | |
fluidRow( | |
column( | |
width=6, | |
plotOutput(outputId = "got_heatmap") | |
), | |
column( | |
width=6, | |
plotOutput(outputId = "got_scatter") | |
) | |
) | |
) | |
) | |
), | |
# Second tab | |
tabPanel( | |
"Inclusiveness Categorical Heatmap", | |
plotOutput(outputId = "incl_heatmap") | |
), | |
# Third tab | |
tabPanel("tab 3", "Tab 3 Contents") | |
) | |
# Define server logic required to draw a histogram | |
server <- function(input, output) { | |
pal <- reactive({ | |
case_when( | |
input$got_palette == "Rainbow" ~ "Spectral", | |
input$got_palette == "Purple-Green" ~ "PRGn", | |
input$got_palette == "Yellow-Orange-Red" ~ "YlOrRd", | |
TRUE ~ "Blues" | |
) | |
}) | |
output$got_heatmap <- renderPlot({ | |
ggplot(got, aes(x=moral, y=physical)) + | |
geom_bin2d(bins=8) + | |
scale_fill_distiller(palette=pal(), direction=1) | |
}) | |
output$got_scatter <- renderPlot({ | |
ggplot(got, aes(x=moral, y=physical, color = physical)) + | |
geom_point() + | |
scale_color_distiller(palette=pal(), direction=1) | |
}) | |
output$incl_heatmap <- renderPlot({ | |
ggplot(incl_bar_data, aes(x=Continent, | |
y=Index.categories.2020, | |
fill=n)) + | |
geom_tile() | |
}) | |
} | |
# Run the application | |
shinyApp(ui = ui, server = server) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is the default Shiny web app with a few modifications. | |
# | |
# ------------------------- | |
# | |
# This is a Shiny web application. You can run the application by clicking | |
# the 'Run App' button above. | |
# | |
# Find out more about building applications with Shiny here: | |
# | |
# http://shiny.rstudio.com/ | |
# | |
library(shiny) | |
library(tidyverse) | |
# Define UI for application that draws a histogram | |
ui <- fluidPage( | |
# Application title | |
titlePanel("Old Faithful Geyser Data"), | |
# Sidebar with a slider input for number of bins | |
sidebarLayout( | |
sidebarPanel( | |
sliderInput(inputId = "bins", | |
label = "No. of bins:", | |
min = 1, | |
max = 51, | |
value = 25) | |
), | |
# Show a plot of the generated distribution | |
mainPanel( | |
plotOutput(outputId = "distPlot") | |
) | |
) | |
) | |
# Define server logic required to draw a histogram | |
server <- function(input, output) { | |
output$distPlot <- renderPlot({ | |
ggplot(faithful, aes(x=waiting)) + | |
geom_histogram(bins = input$bins) + | |
labs( | |
x = 'Waiting time to next eruption (in mins)', | |
title = 'Histogram of waiting times' | |
) | |
}) | |
} | |
# Run the application | |
shinyApp(ui = ui, server = server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment