はだだだだ

定食にサラダは不要だと思う。

MENU

R markdownでプレゼンテーションを作ってみた(ioslides)

R markdownを使うことで、プレゼンテーション資料を作ることができます。形式は以下の3種類あります。

  • ioslides形式:出力ファイルは.html
  • slidy形式:出力ファイルは.html
  • beamer形式:出力ファイルは.pdf

参考:
R Markdownによるスライド生成

今回はioslides形式を使用してみました。

File > New File > R markdownと進みます。
f:id:hadadada00:20190609200958p:plain

presentationを選択し、フォーマットはHTML(ioslides)を選択します。
f:id:hadadada00:20190609201202p:plain

後はエディタにR markdownを記述し、完成したらknitrボタンを押下するとhtmlファイルが出力されます。

今回作成したhtmlファイルとR markdownは以下の通りです。

NBA players’ homestates

---
title: "NBA players' homestates"
author: "hadadada00"
date: "2019年6月9日"
output: ioslides_presentation
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(tidyverse)
library(nbastats)
library(maps)
library(formattable)

# players homestates data
homestates <- players %>% 
  filter(!is.na(birth_state)) %>% 
  mutate(region = tolower(birth_state)) %>% 
  group_by(region) %>% 
  summarise(n_players = n())

# states' polygon data
states <- map_data("state")

# merge playaers' data with polygon data
maps <- left_join(states, homestates, by = c("region"))
```

## About this presentation

I use nba players' data and try to find out if there is any trend in their homestates.

## About the data

I use my original data, which includes NBA players personal data and their stats since 1950.

You can use it by 
```
devtools::install_github("hadadada00/nbastats")
library(nbastats)
```

It has 3 datasets  
- players : player's personal data  
- player_data : player's personal data  
- seasons_stats : season stats data  
NOTE: "players" and "player_data" are different in units.

## Top 10 states
The top state is California, it has produced over 300 NBA players.  

```{r top10,echo = FALSE}
top10 <- homestates %>% 
  arrange(desc(n_players)) %>% 
  mutate(n = row_number()) %>% 
  filter(n <= 10) %>% 
  select(rank = n, region, n_players)

formattable(top10)
```
## Bottom 10 states
The worst is Vermont state, it has produced no NBA players.  
Bottom 10 states are all under 10, so compared with California state (344) , the difference is more than 30 times.

```{r bottom10,echo = FALSE}
bottom10 <- maps %>% 
  distinct(region, n_players) %>% 
  arrange(desc(n_players)) %>% 
  mutate(rank = row_number()) %>% 
  filter(rank >= 40) %>% 
  select(rank, region, n_players)

formattable(bottom10)
```
## Distribution of birth states
Without California, most of homestates are Eastern or Southern part of the US.  

```{r colored maps, echo = FALSE}
# draw colored maps
maps %>%
  ggplot(aes(x = long, y = lat,
             group = group, fill = n_players)) +
  geom_polygon(color = "gray90", size = 0.1) +
  scale_fill_gradient(low = "white", high = "red") +
  coord_map(projection = "albers",
            lat0 = 39, lat1 = 45) +
  labs(x = "", y = "")
```
## Comparing with teams' home towns
Hometowns' disrtibution is simillar to the players' distribution.

![](./NBA_hometowns.PNG)

なお、データは以下の記事で作成したデータを使用しております。
hadadada00.hatenablog.com

以上