-
Notifications
You must be signed in to change notification settings - Fork 1
/
hw1.Rmd
357 lines (252 loc) · 11.1 KB
/
hw1.Rmd
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
---
title: "Hw1"
author: "Kacie Kang"
date: "2/20/2018"
output: html_document
runtime: shiny
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Winter Olympics Medals over Time
1. Medal Counts over Time
replace the old country names with new names.
```{r}
library(tidyverse)
library(plyr)
winter <- read.csv("data/winter.csv")
winter$Country<- revalue(winter$Country, c("EUN"="RUS","URS"="RUS"))
winter$Country<- revalue(winter$Country, c("EUA"="GER","FRG"="GER","GDR"="GER"))
winter$Country<- revalue(winter$Country, c("YUG"="SCG"))
winter$Country<- revalue(winter$Country, c("TCH"="SVK"))
winter$Country<- revalue(winter$Country, c("ROU"="ROM"))
winter
```
bonus point
count team medals as single one By deleting the same names
```{r}
winter2=winter [!duplicated(winter[c("Year", "City", "Sport", "Discipline", "Country", "Gender", "Event", "Medal" )]),]
winter2
```
library(magrittr)
# this is for the chain operator %>%
# also contained in the tidyverse package
Merge two spreadsheets, Calculate a summary of how many winter games each country competed in medaled in and how many medals of each type the country won.
```{r}
dictionary <- read.csv("data/dictionary.csv")
mydata <- merge(x=dictionary,y= winter, by.x=c('Code'), by.y=c('Country'))
alldata <- mydata %>%
group_by(Country,Medal) %>%
dplyr::summarise(Number = n())
Total <- mydata %>%
group_by(Country) %>%
dplyr::summarise(Total = n()) %>%
arrange(desc(Total)) %>%
dplyr::mutate(rank=row_number())
medalsum <-ggplot(Total, aes(x=Country, y=Total)) + scale_y_continuous(expand = c(0,0)) +geom_bar(position="dodge",stat="identity") + ylab("Medal Sum")
gsbsum <-ggplot(alldata, aes(x=Country, y=Number, fill=Medal)) + scale_y_continuous(expand = c(0,0)) +geom_bar(position="dodge",stat="identity") + coord_flip() + ylab("Medal sum by gsb")
alldata
mydata
medalsum
gsbsum
```
top10 countries
and #6, I used label here
```{r}
top10 <- Total %>%
filter(rank<=10)
ptop10 <- merge(alldata, top10, by="Country")
plottop10 <-ggplot(top10 , aes(x=reorder(Country,Total), y=Total)) + scale_y_continuous(expand = c(0,0)) +geom_bar(position="dodge",stat="identity",fill = 'steelblue', colour = 'darkred') + ylab("Medal Sum") +geom_text(mapping = aes(label = Total))
set.seed(1)
plotbygsb <-ggplot(data_plot_top10, aes(x=Country, y=Number, fill=Medal)) + scale_y_continuous(expand = c(0,0)) +geom_bar(position="dodge",stat="identity") + ylab("Medal sum by gsb")+scale_fill_brewer(palette = 'Accent')+geom_text(mapping = aes(label = Number))
top10
ptop10
plottop10
plotbygsb
```
show the trend of total numbers of medals over time of top 10 countries.
and compare
this one in more easier to see through and find the differences
```{r}
library("ggthemes")
pyear <- mydata %>%
group_by(Country,Year) %>%
dplyr::summarise(Number = n())
ptop10year <- merge(data_plot_time, top10, by="Country")
pptop10year <- ggplot(ptop10year, aes(Year, Number)) +
geom_line(aes(color=Country, group=Country)) + ylab("Medal Sum") +
theme_tufte()
pyear
ptop10year
pptop10year
```
2. Medal Counts adjusted by Population GDP
Just consider gold medals.
```{r}
dbyPopulationGDP <- merge(Total, dictionary, by="Country") %>%
dplyr::mutate(dividedbyPopulation=Total/Population*1000000) %>%
dplyr::arrange(desc(dividedbyPopulation)) %>%
dplyr::mutate(rankbyPopulation=row_number()) %>%
dplyr::mutate(dividedbyGDP=Total/GDP.per.Capita*1000000) %>%
dplyr::arrange(desc(dividedbyGDP)) %>%
dplyr::mutate(rankbyGDP=row_number())
dbyPopulationGDP
top10bypop <- dbyPopulationGDP %>% filter(rankbyPopulation<=10) %>%
arrange(desc(dividedbyPopulation))
top10bypop
set.seed(1234)
ptop10bypop <-ggplot(top10bypop, aes(x=reorder(Country,dividedbyPopulation), y=dividedbyPopulation)) + scale_y_continuous(expand = c(0,0)) +geom_bar(position="dodge",stat="identity",fill = 'steelblue', colour = 'darkred') +
ylab("byPopulation")
ptop10bypop
top10bydpg <- dbyPopulationGDP %>% filter(rankbyGDP<=10) %>%
arrange(desc(dividedbyPopulation))
top10bydpg
ptop10bygdp <-ggplot(top10bydpg, aes(x=reorder(Country,dividedbyGDP), y=dividedbyGDP)) + scale_y_continuous(expand = c(0,0)) +geom_bar(position="dodge",stat="identity",fill = 'steelblue', colour = 'darkred') +
ylab("byGDP")
ptop10bygdp
```
medal by country
```{r}
pbymedal <- ggplot(alldata, aes(x=Country, y=Number, fill=Country,label=Medal)) +
geom_bar(width = 1,stat="identity") +
coord_polar(theta = "y") +
theme_bw() +
guides(fill=guide_legend(title = "Country")) +
theme(axis.text.y= element_blank(),
axis.ticks = element_blank(),
axis.text.x= element_blank(),
axis.title.y= element_blank(),
)
pbymedal
```
```{r}
a<- merge(top10bypop, ptop10year, by="Country")
a [!duplicated(a[c(0:10 )]),]
ggplot(a)+geom_density(aes(x=Country, colour=Population))
ggplot(a)+geom_density(aes(x=Country, colour=GDP.per.Capita))
```
3. Host Country Advantage
it shows the host country has advantages
```{r}
library(tidyr)
library(rvest)
library(stringr)
library(plyr)
library("ggthemes")
wiki_hosts <- read_html("https://en.wikipedia.org/wiki/Winter_Olympic_Games")
hosts <- html_table(html_nodes(wiki_hosts, "table")[[5]], fill=TRUE)
hosts <- hosts[-1,1:3]
hosts$city <- str_split_fixed(hosts$Host, n=2, ",")[,1]
hosts$country <- str_split_fixed(hosts$Host, n=2, ",")[,2]
hosts$city<- revalue(hosts$city, c("St. Moritz" ="St.Moritz","Garmisch-Partenkirchen"= "Garmisch Partenkirchen"))
data_plot_average <- data_plot_time %>%
group_by(Country) %>%
mutate(Number_average_by_Year=sum(Number)/19)
data_plot_average <- unite(data_plot_average, "Country_Year", Country, Year,remove = FALSE)
data_plot_average$host <-data_plot_average$Country_Year %in% c("France_1924","Switzerland_1928","United States_1932","Germany_1936","Switzerland_1948","Norway_1952","Italy_1956","United States_1960","Austria_1964","France_1968","Japan_1972","Austria_1976","United States_1980","Yugoslavia_1984","Canada_1988","France_1992","Norway_1994","Japan_1998","United States_2002","Italy_2006","Canada_2010","Russia_2014")
data_plot_average
ggplot(data_plot_average, aes(Year, Number)) +
geom_line(aes(color=Country, group=Country)) + ylab("Medal Sum") +
theme_tufte()
ggplot(data_plot_average, aes(x=Year, y=Country)) + geom_point(alpha = 0.5, size = 1,aes(fill = host, colour=host))
```
4. Country success by sport / discipline / event
I play Snowboard too. so I chose Snowboard to show the data
I count the medal won by countries and present by pie pic
```{r}
dSnowboard <- mydata %>%
filter(Discipline == "Snowboard") %>%
group_by(Country) %>%
dplyr::summarise(SnowboardMedalbyCountry = n()) %>%
dplyr::mutate(Snowboardmedalsum=sum(SnowboardMedalbyCountry)) %>%
dplyr::mutate(SnowboardMedalratio=SnowboardMedalbyCountry/Snowboardmedalsum) %>%
arrange(desc(SnowboardMedalratio))
pSnowboard <-ggplot(dSnowboard, aes(x=Country, y=SnowboardMedalratio)) + geom_point(alpha = 1, size = 2)+ ylab("Ratio")
ggplot(dSnowboard, aes(x = "", y = SnowboardMedalratio, fill = Country)) +
geom_bar(stat = "identity") +
coord_polar(theta = "y")
dSnowboard
pSnowboard
```
5. Most successful athletes
he won the most medals in 15 years.
```{r}
most_Successful_Athletes <- winter %>%
group_by(Athlete,Country,Gender) %>%
dplyr::summarise(Total_Medal_by_Athletes = n()) %>%
dplyr::arrange(desc(Total_Medal_by_Athletes))
most_Successful_Athletes[1,1]
mostevent <- winter %>%
filter(Athlete == "BJOERNDALEN, Ole Einar") %>%
group_by(Year,Medal) %>%
dplyr::summarise(Totalmedal= n())
gmost<-ggplot(mostevent, aes(x=Year, y=Totalmedal)) + scale_y_continuous(expand = c(0,0)) +geom_bar(position="dodge",stat="identity",fill = 'steelblue', colour = 'darkred') +
ylab("Totalmedal")
gmost
```
6. Make two plots interactive
which shows up how much medal(S) BJOERNDALEN, Ole Einar won in different table/every year
which is easier for reader to zoom in the see the exact number
```{r}
library(devtools)
library(plotly)
ggplot(mostevent, aes(x=Year, y=Totalmedal,color = Year)) +
geom_point() + theme(legend.position="none") +
facet_wrap(~ Year) +
ggtitle("Medal(s)BJOERNDALEN, Ole Einar won through years ")
ggplotly(plotbygsb)
plotbygsb <-ggplot(data_plot_top10, aes(x=Country, y=Number, fill=Medal)) + scale_y_continuous(expand = c(0,0)) +geom_bar(position="dodge",stat="identity") + ylab("Medal sum by gsb")+scale_fill_brewer(palette = 'Accent')+geom_text(mapping = aes(label = Number))
```
7. Data Table
this contains data about snowboard medals won by countries and a ratio of which country won the most of it
because I play snowboard and I am very interested in it.
we can do search among the countries, medals by country, and sort by the small errow.
```{r}
library(DT)
datatable(dSnowboard) %>%
formatStyle('Country', color = 'white',
backgroundColor = 'blue', fontWeight = 'bold')
dSnowboard %>%
datatable(
rownames = FALSE,
colnames = "Data of Snowboard medals",
filter = list(position = "top"),
options = list(
dom = "Bfrtip",
buttons = I("colvis"),
language = list(sSearch = "Filter:")
),
extensions = c("Buttons", "Responsive")
)
```
This R Markdown document is made interactive using Shiny. Unlike the more traditional workflow of creating static reports, you can now create documents that allow your readers to change the assumptions underlying your analysis and see the results immediately.
To learn more, see [Interactive Documents](http://rmarkdown.rstudio.com/authoring_shiny.html).
## Inputs and Outputs
You can embed Shiny inputs and outputs in your document. Outputs are automatically updated whenever inputs change. This demonstrates how a standard R plot can be made interactive by wrapping it in the Shiny `renderPlot` function. The `selectInput` and `sliderInput` functions create the input widgets used to drive the plot.
```{r eruptions, echo=FALSE}
inputPanel(
selectInput("n_breaks", label = "Number of bins:",
choices = c(10, 20, 35, 50), selected = 20),
sliderInput("bw_adjust", label = "Bandwidth adjustment:",
min = 0.2, max = 2, value = 1, step = 0.2)
)
renderPlot({
hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks),
xlab = "Duration (minutes)", main = "Geyser eruption duration")
dens <- density(faithful$eruptions, adjust = input$bw_adjust)
lines(dens, col = "blue")
})
```
## Embedded Application
It's also possible to embed an entire Shiny application within an R Markdown document using the `shinyAppDir` function. This example embeds a Shiny application located in another directory:
```{r tabsets, echo=FALSE}
shinyAppDir(
system.file("examples/06_tabsets", package = "shiny"),
options = list(
width = "100%", height = 550
)
)
```
Note the use of the `height` parameter to determine how much vertical space the embedded application should occupy.
You can also use the `shinyApp` function to define an application inline rather then in an external directory.
In all of R code chunks above the `echo = FALSE` attribute is used. This is to prevent the R code within the chunk from rendering in the document alongside the Shiny components.