library(gridExtra)
library(ggplot2) 
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following object is masked from 'package:gridExtra':
## 
##     combine
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(shiny)
library(rsconnect)
## 
## Attaching package: 'rsconnect'
## The following object is masked from 'package:shiny':
## 
##     serverInfo
presidential_data <- read.csv("C:/Users/joshu/Downloads/1976-2020-president (2).csv")
graph_list <- list()
y <- unique(presidential_data$year)
for(i in 1:12){
  presidential_data_new <- filter(presidential_data, presidential_data$year == y[i])
  
  
  max_percent_rows <- presidential_data_new %>%
    group_by(state) %>%
    filter(Percent == max(Percent))
  
  Blue_state <- c()
  Red_state <- c()
  
  for(j in 1:51){
    if (max_percent_rows$party_simplified[j] == "DEMOCRAT"){
      Blue_state[j] <- max_percent_rows$state[j]
    }
    else if (max_percent_rows$party_simplified[j] == "REPUBLICAN"){
      Red_state[j] <- max_percent_rows$state[j]
    }
  }
  Blue_states <- na.omit(Blue_state)
  Blue_states <- lapply(Blue_states, tolower)
  
  Red_states <- na.omit(Red_state)
  Red_states <- lapply(Red_states, tolower)
  
  states_and_percentages <- data.frame(matrix(NA,nrow = 51,ncol = 0))
  states_and_percentages$region <- tolower(max_percent_rows$state)
  states_and_percentages$percent <- max_percent_rows$Percent
  
  
  map_data <- data.frame(map_data("state"))  
  map_data$fill <- ifelse(map_data$region %in% Blue_states, "blue",
                          ifelse(map_data$region %in% Red_states, "red", "purple"))
  map_data <- left_join(map_data,states_and_percentages,"region")
  current_year <- y[i]
  title <- paste("The Year is ",current_year)
  graph <- ggplot() +
    geom_map(data = map_data, map = map_data,
             aes(x = long, y = lat, map_id = region, fill = fill,alpha = percent),
             color = "white", size = 0.25) +
    scale_fill_manual(values = c("blue" = "blue", "red" = "red", "purple" = "purple"),
                      labels = c("Democrat", "Republican", "Other")) +
    labs(title = title) +
    theme(plot.title = element_text(hjust = 0.5),
          plot.background = element_rect(fill = "white", color = "white"),
          panel.background = element_rect(fill = "white"),
          axis.title = element_blank(),
          axis.text = element_blank(),
          axis.ticks = element_blank(),
            legend.position = "top")+
    guides(alpha = "none", fill = guide_legend(title = "Party"))
  
  graph_list[[i]] <- graph
  }

graphs_arranged <- grid.arrange(
  grobs = graph_list,  
  nrow = 3,  
  ncol = 4  
)

# Display the grid of graphs
graphs_arranged
## TableGrob (3 x 4) "arrange": 12 grobs
##     z     cells    name           grob
## 1   1 (1-1,1-1) arrange gtable[layout]
## 2   2 (1-1,2-2) arrange gtable[layout]
## 3   3 (1-1,3-3) arrange gtable[layout]
## 4   4 (1-1,4-4) arrange gtable[layout]
## 5   5 (2-2,1-1) arrange gtable[layout]
## 6   6 (2-2,2-2) arrange gtable[layout]
## 7   7 (2-2,3-3) arrange gtable[layout]
## 8   8 (2-2,4-4) arrange gtable[layout]
## 9   9 (3-3,1-1) arrange gtable[layout]
## 10 10 (3-3,2-2) arrange gtable[layout]
## 11 11 (3-3,3-3) arrange gtable[layout]
## 12 12 (3-3,4-4) arrange gtable[layout]
ui <- fluidPage(
  titlePanel("How the United States Voted in Presidential Elections"),
  sidebarLayout(
    sidebarPanel(
      sliderInput("slider", "Slider", min = 1, max = 12, value = 1,)
    ),
    mainPanel(
      plotOutput("graph")
    )
  )
)
server <- function(input, output) {
  graphs_list <- list(
  ggplot1 = graph_list[[1]],
  ggplot2 = graph_list[[2]],
  ggplot3 = graph_list[[3]],
  ggplot4 = graph_list[[4]],
  ggplot5 = graph_list[[5]],
  ggplot6 = graph_list[[6]],
  ggplot7 = graph_list[[7]],
  ggplot8 = graph_list[[8]],
  ggplot9 = graph_list[[9]],
  ggplot10 = graph_list[[10]],
  ggplot11 = graph_list[[11]],
  ggplot12 = graph_list[[12]]
)
  output$graph <- renderPlot({
    # Get the selected value from the slider
    selected_graph <- input$slider
    graph <- graphs_list[[paste0("ggplot", selected_graph)]]
    print(graph)
  })
}
shinyApp(ui = ui, server = server)
## PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
Shiny applications not supported in static R Markdown documents