Last active
April 26, 2019 13:07
-
-
Save smach/3d0187123e7ddadd04cb54a9843e263d to your computer and use it in GitHub Desktop.
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
if(!require(pacman)){ | |
install.packages("pacman") | |
} | |
p_load(ggplot2, dplyr, janitor) | |
district <-c("A","B","C","A","B", "C") | |
money <-c(500,324,245,654,234, 232) | |
year <- c("2001", "2001", "2001", "2002", "2002", "2002") | |
df <- data.frame(district, money, year, stringsAsFactors = FALSE) | |
total_by_year <- df %>% | |
group_by(year) %>% | |
summarize( | |
money = sum(money) | |
) %>% | |
mutate( | |
district = "All" | |
) %>% | |
ungroup() | |
df_with_total <- bind_rows(df, total_by_year) | |
districts <- c("All", sort(unique(district))) | |
# Define UI for application that draws a bar chart | |
ui <- fluidPage( | |
# Application title | |
titlePanel("School Expenditure by Year"), | |
# Dropdown | |
sidebarLayout( | |
sidebarPanel( | |
selectInput("mydistrict", "Select District", choices = districts, selected = "All", ) | |
), | |
# Set a place to show a bar chart of spending | |
mainPanel( | |
plotOutput("distSpending") | |
) | |
) | |
) | |
# Define server logic required to draw the barchart | |
server <- function(input, output) { | |
# filter data based on user input | |
my_graph_data <- reactive({ | |
dplyr::filter(df_with_total, district == input$mydistrict) | |
}) | |
# draw the bar chart | |
output$distSpending <- renderPlot({ | |
ggplot(my_graph_data(), aes(x=year, y=money)) + | |
geom_bar(stat="identity", color = "black", fill="#0072B2") + | |
ggtitle(paste("District:", input$mydistrict)) + | |
theme_minimal() + | |
theme(panel.border = element_blank(), panel.grid.major = element_blank(), | |
plot.title = element_text(size = 18, face = "bold"), | |
panel.grid.minor = element_blank(), axis.line = element_line(colour = "gray")) + | |
scale_y_continuous(expand = c(0, 0)) | |
}) | |
} | |
# 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