-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10.static plot.R
141 lines (98 loc) · 3.63 KB
/
10.static plot.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
library(tidyverse)
library(tidycensus)
library(ggplot2)
library(treemapify)
library(geomtextpath)
library(RColorBrewer)
library(psych)
library(sf)
setwd("/Users/chenjieyi/Documents/GitHub/final-project-jieyi_hanzhe_jaeho")
static_df <- read.csv("data/02.merged_data.csv")
# first ----
plot_fun_forother <- function(df, selectvar){
df %>%
filter(year %in% c(2011:2019)) %>%
group_by(zipcode) %>%
select(as.name(selectvar), year) %>%
rename(n = as.name(selectvar)) %>%
ggplot() +
xlab("year") +
ylab(selectvar)+
geom_boxplot(aes(x = as.factor(year), y = n)) +
ggtitle(paste0("11. Average ", selectvar, " in chicago by year"))
}
plot_fun_forother(static_df, "housing_price")
ggsave("11. Average housing price trend from 2011 to 2019.png")
plot_fun_forother(static_df, "income")
ggsave("11. Average Median income trend from 2011 to 2019.png")
# second ----
readshp <- function(x){
st_read(file.path(path,x))
}
path <- "/Users/chenjieyi/Documents/GitHub/final-project-jieyi_hanzhe_jaeho"
zipcode <- readshp("data/zipcode_area.shp")
zipcode$zip <- as.numeric(zipcode$zip)
map_plot <- left_join(static_df, zipcode, by = c("zipcode" = "zip"))
map_plot_year <- function(df, y_num){
avg_price_year <- df %>%
filter(year == y_num)
avg_price_year <- st_sf(avg_price_year)
final_plot <- ggplot(avg_price_year) +
geom_sf(data = zipcode) +
geom_sf(data = avg_price_year, aes(fill = housing_price)) +
scale_fill_gradientn(limits = c(40000, 750000),
colours = c("beige", "bisque", "darkred")) +
ggtitle(paste0("11. Average housing price in ", y_num))
}
avg_2011 <- map_plot_year(map_plot, y_num = 2011)
ggsave("11. Average housing price in 2011 by zipcode.png")
avg_2019 <- map_plot_year(map_plot, y_num = 2019)
ggsave("11. Averagee housing price in 2019 by zipcode.png")
# third ----
plot_fun_for_race <- function(df, y_num) {
plot1_st1 <- df %>%
filter(year == y_num) %>%
select(white_rate, black_rate, asian_rate)
plot1_st2 <- plot1_st1 %>%
pivot_longer(cols = white_rate:asian_rate,
names_to = "race",
values_to = "prop")
plot1_st3 <- plot1_st2 %>%
group_by(race) %>%
mutate(avg_prop = mean(prop))
plot1_st4 <- plot1_st3[1:3,] %>%
select(-prop)
plot1_st4$label <- paste0(plot1_st4$race, "\n",
round(plot1_st4$avg_prop, 3), "%")
return(plot1_st4)
}
ggplot(plot_fun_for_race(static_df, y_num = 2011),
aes(fill = race,
area = avg_prop,
label = label)) +
geom_treemap() +
geom_treemap_text(colour = "white",
place = "centre") +
labs(title = "11. Average race distribution in 2011 chicago") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(legend.position = "none")
ggsave("11. Average race distribution in 2011 chicago.png")
ggplot(plot_fun_for_race(static_df, y_num = 2019),
aes(fill = race,
area = avg_prop,
label = label)) +
geom_treemap() +
geom_treemap_text(colour = "white",
place = "centre") +
labs(title = "11. Average race distribution in 2019 chicago") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(legend.position = "none")
ggsave("11. Average race distribution in 2019 chicago.png")
# fourth ----
corr_plot_df <- static_df %>%
filter(between(year, 2011, 2019)) %>%
select(-c(year, zipcode, grocery, bus_stop))
png(height=800, width=1000, file="11. Correlation plot between each variables.png", res = 96)
corPlot(corr_plot_df, main = "11. Correlation plot between each variables.png",
stars = TRUE, xlas = 2)
dev.off()