-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeographic and Time Series Data Visualization
215 lines (166 loc) · 5.5 KB
/
Geographic and Time Series Data Visualization
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# Color
## Specifying Individual Colors
library(gcookbook)
library(ggplot2)
ggplot(heightweight, aes(x = ageYear, y = heightIn)) +
geom_point(size = 3, color = hsv(0.8, 0.8, 0.7))
## Continuous Scales
hwplot <- ggplot(heightweight, aes(x = ageYear, y = heightIn, color = weightLb)) +
geom_point(size = 3)
hwplot + scale_color_gradient()
### Built-Ins
hwplot +
scale_colour_distiller(palette = "BuPu")
hwplot +
scale_color_viridis_c()
### Custom Gradient Colors
hwplot +
scale_color_gradient(low = "black", high = hsv(0.25, 0.75, 0.9))
### Custom Divergent Palette
hwplot +
scale_colour_gradient2(
low = "red",
mid = "white",
high = "blue",
midpoint = 100.5,
space= "Lab",
na.value = "grey50"
)
library(scales)
hwplot +
scale_colour_gradient2(
low = muted("red"),
mid = "white",
high = muted("blue"),
midpoint = 100.5,
space = "Lab",
na.value = "grey50"
)
## Discrete Color Palettes
uspopage_plot <- ggplot(uspopage, aes(x = Year, y = Thousands, fill = AgeGroup)) +
geom_area()
uspopage_plot
uspopage_plot +
scale_fill_viridis_d()
uspopage_plot +
scale_fill_brewer(palette = "Pastel1")
uspopage_plot +
scale_fill_brewer(palette = "Reds", direction = -1) +
theme_dark()
### Manual Discrete Color Palettes
p <- ggplot(mtcars, aes(mpg, wt, color = factor(cyl))) +
geom_point(size = 3)
pal <- c("8" = "red", "4" = "blue", "6" = "darkgreen")
p + scale_colour_manual(values = pal)
# Geographic Data
## Glyph Maps
statesmap = map_data('state')
head(statesmap)
ggplot(statesmap, aes(long, lat, group = group)) +
geom_polygon(colour = 'black', fill = NA)
library(crimedata)
library(tidyverse)
crimes <- get_crime_data(years = c(2018), type = "sample", output = "tbl")
crimes <- crimes %>%
select(-c("uid", "offense_code", "census_block", "date_start", "date_end"))
head(crimes)
ggplot() +
geom_polygon(data = statesmap,
aes(x = long, y = lat, group = group),
colour = 'black',
fill = NA) +
geom_point(data = crimes,
aes(longitude, latitude, color = offense_against),
size = 3)
## Choropleth
crimes <- USArrests %>%
rownames_to_column() %>%
mutate(state = tolower(rowname)) %>%
select(-c(rowname)) %>%
data.frame()
head(crimes)
states_map <- map_data("state")
crime_map <- left_join(states_map, crimes,
by = c("region" = "state") )
head(crime_map)
library(mapproj)
ggplot(crime_map,
aes(x = long, y = lat, group = group, fill = Assault)) +
geom_polygon(colour = "black") +
coord_map("polyconic")
ggplot(crimes, aes(map_id = state, fill = Assault)) +
geom_map(map = states_map) +
expand_limits(x = states_map$long, y = states_map$lat) +
coord_map("polyconic")
# Time series
## Dense Time series Heat maps
crashes <- read.csv("C:\\Users\\wodnj\\OneDrive\\바탕 화면\\Data Visualization\\DSC 465 - Week 4\\Data File\\chicago_crashes.csv")
ggplot(crashes, aes(x = CRASH_DAY_OF_WEEK, y = CRASH_HOUR, fill = INJURIES_TOTAL),
na.rm = TRUE) +
geom_tile()
ggplot(crashes, aes(INJURIES_TOTAL)) +
geom_histogram(bins = 10)
summary(crashes$INJURIES_TOTAL)
ggplot(crashes, aes(x = factor(CRASH_DAY_OF_WEEK), y = factor(CRASH_HOUR)),
na.rm = TRUE) +
stat_summary_2d(fun = "mean", aes(z = INJURIES_TOTAL), geom = "tile")
ggplot(crashes, aes(x = factor(CRASH_DAY_OF_WEEK), y = factor(CRASH_HOUR)),
na.rm = TRUE) +
geom_bin2d()
## Inflation Adjustments
CPITable <- read.csv("C:\\Users\\wodnj\\OneDrive\\바탕 화면\\Data Visualization\\DSC 465 - Week 4\\Data File\\CPITable.csv")
head(CPITable)
cpiLookup <- function(y) {
return(CPITable %>%
filter(Year %in% y) %>%
select(Value) %>%
.$Value ) }
cpiLookup(2010)
cpiLookup(c(2010, 2015))
typeof(cpiLookup(c(2010, 2015)))
moneyOverTime = tibble(y = c(2015, 2016, 2017, 2018), x = c(100, 100, 100, 100))
head(moneyOverTime)
moneyOverTime <- moneyOverTime %>%
mutate(adjustedX = x * cpiLookup(max(.$y)) / cpiLookup(y))
moneyOverTime
## Dealing with Dates
priceIndex <- read.csv("C:\\Users\\wodnj\\OneDrive\\바탕 화면\\Data Visualization\\DSC 465 - Week 4\\Data File\\ConsumerPriceIndex.csv")
head(priceIndex)
cIndex <- priceIndex %>%
select(-c("Avg.", "Dec.1")) %>%
pivot_longer(-c(Year, Avg), names_to = "Month", values_to = "value")
head(cIndex)
cIndexDateStr <- cIndex %>%
mutate(date_string = paste(Year, Month, "01", sep = "-"))
head(cIndexDateStr)
altCIdx <- cIndex %>%
unite("date_string", c(Year, Month), sep = "-")
head(altCIdx)
library(lubridate)
cIndex <- cIndexDateStr %>%
mutate(Date = as_date(date_string, "%Y-%b-%d"))
head(cIndex)
timeplot <- ggplot(cIndex, aes(Date, value)) +
geom_line() +
theme_bw()
timeplot
cIdxWD <- cIndex %>%
mutate(weekday = wday(Date))
head(cIdxWD)
cIdxWD %>%
filter(Date > ymd(20060601), Date < ymd("2008-12-31")) %>%
ggplot(aes(x = year(Date) %>% factor(),
y = month(Date) %>% factor()),
na.rm = TRUE) +
stat_summary_2d(fun = "median", aes(z = value), geom = "tile")
cIdxWD %>%
filter(month(Date) <= 9, month(Date) >= 3) %>%
filter(year(Date) >= 2006, year(Date) <= 2008) %>%
ggplot(aes(x = month(Date) %>% factor(),
y = value),
na.rm = TRUE) +
facet_wrap( ~ year(Date)) +
geom_col()
## Polar Coordinates
timeplot +
coord_polar()