forked from info-201-wi24/Info201-Final-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
91 lines (75 loc) · 2.92 KB
/
server.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
library(plotly)
library(ggplot2)
library(shiny)
library(bslib)
library(dplyr)
library("maps")
library("mapproj")
# Be sure to set your working directory to the final project file !!!
# Should be called "Info201-Final-Project"
obesity_combined_df <- read.csv("combined_Obesity_income.csv")
obesity_combined_df <- obesity_combined_df %>%
mutate(State = tolower(State))
obesity_combined_df <- obesity_combined_df %>%
mutate(obesity_per_diff = Crude.Prevalence...5 - Crude.Prevalence...3, na.rm = TRUE)
state_shape <- map_data("state")
blank_theme <- theme_bw() + theme(
plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
)
# This is the server section of the project.
server <- function(input, output){
# This is the first graph we have. It's a scatter plot.
output$viz_1_output_id <- renderPlotly({
selected_df <- obesity_combined_df %>%
filter(State %in% input$use_selection)
obesity_income_plot <- ggplot(selected_df) +
geom_point(aes(x = income_difference_20_10,
y = obesity_per_diff,
color = State)) +
scale_fill_brewer(palette = "Set3")+
blank_theme +
labs(
title = "The relationship between Children's Obesity Level and average income differences",
x = "Difference in Household Income between 2010 and 2020",
y = "Difference in Children's Obsesity Level Percentage in US between 2010 and 2020",
color = "States"
)
return(ggplotly(obesity_income_plot))})
output$viz_2_output_id <- renderPlotly({
state_shape <- map_data("state")
obesity_shape_df <- left_join(obesity_combined_df, state_shape, by = c("State" = "region"))
obesity_plot <- ggplot(obesity_shape_df) +
geom_polygon(aes(x = long, y = lat,
group = group,
fill = !!as.name(input$use_choice),
)) +
coord_map() +
blank_theme +
scale_fill_continuous(low = "light blue",
high = "dark blue") +
labs(
title = "Children Obsesity Level Across the States",
x = "Children's Obesity Level across states",
color = "States"
)
return(ggplotly(obesity_plot))
})
output$viz_3_output_id <- renderPlotly({
selected_df <- obesity_combined_df %>%
filter(State %in% input$user_selection)
obesity_income_plot <- ggplot(selected_df) +
geom_col(aes(x = Income_2020,
y = Obesity_2020,
fill = State)) +
blank_theme +
labs(
title = "The relationship between Children's Obesity Level and 2020 Household Income",
x = "Household Income in 2020",
y = "Children's Obsesity Level in US in 2020",
color = "States"
)
return(ggplotly(obesity_income_plot))})
# TODO Make outputs based on the UI inputs here
}