-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_visualization.R
executable file
·282 lines (241 loc) · 10.4 KB
/
data_visualization.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#install.packages("ggvis")
library(ggvis)
library(readr)
url <- 'http://people.terry.uga.edu/rwatson/data/carbonMeans.txt'
carbon <- read_delim(url, delim=',')
# Select year(x) and CO2(y) to create a x-y point plot
# Specify red points, as you find that aesthetically pleasing
carbon %>% ggvis(~year,~CO2) %>% layer_points(fill:= 'red')
# Notice how ‘%>%’ is used for creating a pipeline of commands
carbon %>% ggvis(~year,~CO2) %>% layer_points(fill:='red') %>%
scale_numeric('y',zero=T)
# Compute a new column containing the relative change in CO2
carbon$relCO2 = (carbon$CO2-280)/280
carbon %>% ggvis(~year,~relCO2) %>% layer_lines(stroke:='blue') %>%
scale_numeric('y',zero=T) %>%
add_axis('y', title = "CO2 ppm of the atmosphere", title_offset=50) %>%
add_axis('x', title ='Year', format = '####')
library(ggvis)
library(readr)
library(measurements)
url <- 'http://people.terry.uga.edu/rwatson/data/centralparktemps.txt'
t <- read_delim(url, delim=',')
#View(t)
t$C <- round(conv_unit(t$temperature,'F','C'),1)
#View(t)
t %>% ggvis(~C) %>% layer_histograms(width = 2, fill:='cornflowerblue') %>%
add_axis('x',title='Celsius') %>%
add_axis('y',title='Frequency')
library(ggvis)
library(DBI)
conn <- dbConnect(RMySQL::MySQL(), "richardtwatson.com", dbname="ClassicModels", user="db1", password="student")
# Query the database and create file for use with R
d <- dbGetQuery(conn,"SELECT productLine from Products;")
#View(d)
# Plot the number of product lines by specifying the appropriate column name
d %>% ggvis(~productLine) %>% layer_bars(fill:='chocolate') %>%
add_axis('x',title='Product line') %>%
add_axis('y',title='Count')
library(ggvis)
library(DBI)
conn <- dbConnect(RMySQL::MySQL(), "richardtwatson.com", dbname="ClassicModels", user="db1", password="student")
#Alternate source if statement above fails
#conn <- dbConnect(RMySQL::MySQL(), "richardtwatson.com", dbname="ClassicModels", user="db1", password="student")
# Get the monthly value of orders
d <- dbGetQuery(conn,"SELECT MONTH(orderDate) AS orderMonth, sum(quantityOrdered*priceEach) AS orderValue FROM Orders, OrderDetails WHERE Orders.orderNumber = OrderDetails.orderNumber GROUP BY orderMonth;")
#View(d)
# Plot data orders by month
# Show the points and the line
#head(d)
d %>% ggvis(~orderMonth, ~orderValue/1000000)%>%
layer_lines(stroke:='blue') %>%
layer_points(fill:='red') %>%
add_axis('x', title = 'Month') %>%
add_axis('y',title='Order value (millions)', title_offset=30)
library(ggvis)
library(DBI)
conn <- dbConnect(RMySQL::MySQL(), "richardtwatson.com", dbname="ClassicModels", user="db1", password="student")
d <- dbGetQuery(conn,"SELECT YEAR(orderDate) AS orderYear, MONTH(orderDate) AS Month, sum((quantityOrdered*priceEach)) AS Value FROM Orders, OrderDetails WHERE Orders.orderNumber = OrderDetails.orderNumber GROUP BY orderYear, Month;")
#View(d)
# Plot data orders by month and display by year
# ggvis expects grouping variables to be a factor, so convert
d$Year <- as.factor(d$orderYear)
d %>% group_by(Year) %>% ggvis(~Month,~Value/1000, stroke = ~Year) %>%
layer_lines() %>%
add_axis('x', title = 'Month') %>%
add_axis('y',title='Order value (thousands)', title_offset=50)
d %>% group_by(Year) %>% ggvis( ~Month, ~Value/100000, fill = ~Year) %>% layer_bars() %>%
add_axis('x', title = 'Month') %>%
add_axis('y',title='Order value (thousands)', title_offset=50)
library(ggvis)
library(DBI)
library(sqldf)
options(sqldf.driver = "SQLite") # to avoid conflict with RMySQL
# Load the driver
conn <- dbConnect(RMySQL::MySQL(), "richardtwatson.com", dbname="ClassicModels", user="db1", password="student")
orders <- dbGetQuery(conn,"SELECT 'Orders' as Category, MONTH(orderDate) AS month, sum((quantityOrdered*priceEach)) AS value FROM Orders, OrderDetails WHERE Orders.orderNumber = OrderDetails.orderNumber and YEAR(orderDate) = 2004 GROUP BY Month;")
#View(orders)
payments <- dbGetQuery(conn,"SELECT 'Payments' as Category, MONTH(paymentDate) AS month, SUM(amount) AS value FROM Payments WHERE YEAR(paymentDate) = 2004 GROUP BY MONTH;")
#View(payments)
# concatenate the two files
m <- sqldf("select month, Category, value from orders UNION select month, Category, value from payments")
#View(m)
m %>% group_by(Category) %>% ggvis(~month, ~value, stroke = ~ Category) %>%
layer_lines() %>%
add_axis('x',title='Month') %>%
add_axis('y',title='Value',title_offset=70)
library(sqldf)
options(sqldf.driver = "SQLite") # to avoid conflict with RMySQL
url <- "http://people.terry.uga.edu/rwatson/data/centralparktemps.txt"
t <- read_delim(url, delim=',')
#View(t)
t8 <- sqldf('select * from t where month = 8')
#View(t8)
t8 %>% ggvis(~year,~temperature) %>%
layer_lines(stroke:='red') %>%
layer_smooths(se=T, stroke:='blue') %>%
add_axis('x',title='Year',format = '####') %>%
add_axis('y',title='Temperature (F)', title_offset=30)
library(ggvis)
library(DBI)
conn <- dbConnect(RMySQL::MySQL(), "richardtwatson.com", dbname="ClassicModels", user="db1", password="student")
d <- dbGetQuery(conn,"SELECT amount from Payments;")
#View(d)
# Boxplot of amounts paid
d %>% ggvis(~factor(0),~amount) %>% layer_boxplots() %>%
add_axis('x',title='Checks') %>%
add_axis('y',title='')
library(ggvis)
library(DBI)
conn <- dbConnect(RMySQL::MySQL(), "richardtwatson.com", dbname="ClassicModels", user="db1", password="student")
d <- dbGetQuery(conn,"SELECT month(paymentDate) as month, amount from Payments;")
#View(d)
# Boxplot of amounts paid
d %>% ggvis(~month,~amount) %>% layer_boxplots() %>%
add_axis('x',title='Month', values=c(1:12)) %>%
add_axis('y',title='Amount', title_offset=70)
library(ggvis)
library(DBI)
conn <- dbConnect(RMySQL::MySQL(), "richardtwatson.com", dbname="ClassicModels", user="db1", password="student")
d <- dbGetQuery(conn,'SELECT count(*) as Frequency, productLine as Line, productScale as Scale from Products group by productLine, productScale')
#View(d)
d %>% ggvis( ~Scale, ~Line, fill= ~Frequency) %>%
layer_rects(width = band(), height = band()) %>%
layer_text(text:=~Frequency, stroke:='white', align:='left', baseline:='top') # add frequency to each cell
#dynamic visualization
#install.packages(shiny)
library(ggvis)
library(shiny)
carbon$relCO2 = (carbon$CO2-280)/280
carbon %>% ggvis(~year,~relCO2) %>%
layer_lines(stroke:=input_select(c("red", "green", "blue"))) %>%
scale_numeric('y',zero=T) %>%
add_axis('y', title = "CO2 ppm of the atmosphere", title_offset=50) %>%
add_axis('x', title ='Year', format='####')
#dynamic visualization
library(shiny)
carbon$relCO2 = (carbon$CO2-280)/280
slider <- input_slider(1, 5, label = "Width")
select_color <- input_select(label='Color',c("red", "green", "blue"))
carbon %>% ggvis(~year,~relCO2) %>%
layer_lines(stroke:=select_color, strokeWidth:=slider) %>%
scale_numeric('y',zero=T) %>%
add_axis('y', title = "CO2 ppm of the atmosphere", title_offset=50) %>%
add_axis('x', title ='Year', format='####')
#No plots from these statements - not supposed to have plots
#The sqldf statements are compared to dplyr functions
library(dplyr)
library(readr)
library(sqldf)
options(sqldf.driver = "SQLite") # to avoid conflict with DBI
url <- 'http://people.terry.uga.edu/rwatson/data/centralparktemps.txt'
t <- read_delim(url, delim=',')
#View(t)
# filter
sqldf("select * from t where year = 1999")
filter(t,year==1999)
# select
sqldf("select temperature from t")
select(t,temperature)
# a combination of filter and select
sqldf("select * from t where year > 1989 and year < 2000")
select(t,year, month, temperature) %>% filter(year > 1989 & year < 2000)
# arrange
sqldf("select * from t order by year desc, month")
arrange(t, desc(year),month)
# mutate -- create a new column
t_SQL <- sqldf("select year, month, temperature, (temperature-32)*5/9 as CTemp from t")
t_dplyr <- mutate(t,CTemp = (temperature-32)*5/9)
# summarize
sqldf("select avg(temperature) from t")
summarize(t,mean(temperature))
library(shiny)
library(ggvis)
library(dplyr)
library(readr)
url <- 'http://people.terry.uga.edu/rwatson/data/centralparktemps.txt'
t <- read_delim(url, delim=',')
View(t)
slider <- input_slider(1, 12,label="Month")
t %>%
ggvis(~year,~temperature) %>%
filter(month == eval(slider)) %>%
layer_points() %>%
add_axis('y', title = "Temperature", title_offset=50) %>%
add_axis('x', title ='Year', format='####')
#No plot
#install.packages(ggplot2)
#install.packages(ggmap)
#install.packages(mapproj)
#install.packages(DBI)
library(ggplot2)
library(ggmap)
library(mapproj)
library(DBI)
conn <- dbConnect(RMySQL::MySQL(), "richardtwatson.com", dbname="ClassicModels", user="db1", password="student")
# Google maps requires lon and lat, in that order, to create markers
d <- dbGetQuery(conn,"SELECT y(officeLocation) AS lon, x(officeLocation) AS lat FROM Offices;")
#View(d)
# show offices in the United States
# vary zoom to change the size of the map
map <- get_googlemap('united states',marker=d,zoom=4)
ggmap(map) + labs(x = 'Longitude', y = 'Latitude') + ggtitle('US offices')
library(ggplot2)
library(ggmap)
library(mapproj)
library(readr)
url <- 'http://people.terry.uga.edu/rwatson/data/pumps.csv'
pumps <- read_delim(url, delim=',')
#View(pumps)
url <- 'http://people.terry.uga.edu/rwatson/data/deaths.csv'
deaths <- read_delim(url, delim=',')
#View(deaths)
map <- get_googlemap('broadwick street, london, united kingdom',markers=pumps,zoom=15)
ggmap(map) + labs(x = 'Longitude', y = 'Latitude') + ggtitle('Pumps and deaths') +
geom_point(aes(x=longitude,y=latitude,size=count),color='blue',data=deaths) +
xlim(-.14,-.13) + ylim(51.51,51.516)
#install.packages(leaflet)
library(leaflet)
location <- geocode("university of georgia, athens, usa")
m<-leaflet() %>% addTiles() %>% addMarkers(lng=location$lon, lat=location$lat, popup="UGA")
m
#install.packages(leaflet)
library(leaflet)
location <- geocode("university of georgia, cortona, italy")
m<-leaflet() %>% addTiles() %>% addMarkers(lng=location$lon, lat=location$lat, popup="UGA")
m
#install.packages(leaflet)
library(leaflet)
location <- geocode("university of georgia, griffin, usa")
m<-leaflet() %>% addTiles() %>% addMarkers(lng=location$lon, lat=location$lat, popup="UGA")
m
#install.packages(ggmap)
library(ggmap)
hdf <- get_map("Athens, GA, USA")
ggmap(hdf)
library(DBI)
conn <- dbConnect(RMySQL::MySQL(), "richardtwatson.com", dbname="ClassicModels", user="db1", password="student")
d <- dbGetQuery(conn,"SELECT y(officeLocation) AS lon, x(officeLocation) AS lat FROM Offices;")
#View(d)
map<-leaflet() %>% addTiles()
map %>% addCircles(data=d, lat=~d$lat, lng=~d$lon)