Interactive Data Visualization

Row

Car Failure Analysis

Car Failure

Car Failures in US

1624

Labor Cost

Massachusetts

21

California

200

Texas

293

Florida

168

Row

Failures By State

Top States

Map

Map

---
title: "109001014's Dashboard"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: fill
    social: [ "twitter", "facebook", "menu"]
    source_code: embed
---

```{r setup, include=FALSE}
#設定環境 如果無法設定套件環境,請下載套件,利用install.packages("XXX")指令
library(flexdashboard)
library(knitr)
library(DT)
library(rpivotTable)
library(ggplot2)
library(plotly)
library(dplyr)
library(openintro)
library(highcharter)
library(ggvis)

# 當圖形帶有中文,請安裝此套件
knitr::opts_chunk$set(fig.showtext=TRUE)
library(showtext)
showtext_auto()
```

```{r}
#帶入資料
data <- read.csv("C:/Users/user/Downloads./vehicle.csv")
```

```{r}
#預先指定顏色
mycolors <- c("blue", "#FFC125", "darkgreen", "darkorange")
```

Interactive Data Visualization
=====================================

Row
-------------------------------------

### Car Failure Analysis

```{r}
#顯示文字方塊
valueBox(paste("Car Failure"),
         color = "warning")
```

### Car Failures in US

```{r}
#顯示文字方塊,帶入數值
valueBox(length(data$State),
         icon = "fa-building")
```

### **Labor Cost**

```{r}
#顯示計量表,並指定顏色範圍。lc代表工資labor cost。
gauge(round(mean(data$lc),
            digits = 2),
            min = 0,
            max = 350,
            gaugeSectors(success = c(0, 150),
                         warning = c(150, 200),
                         danger = c(200, 450),
                         colors = c("green", "yellow", "red")))
```

### Massachusetts

```{r}
#顯示文字方塊,帶入特定州別條件下的數值
valueBox(sum(data$State == "MA"),
         icon = 'fa-user')
```

### California

```{r}
#顯示文字方塊,帶入特定州別條件下的數值
valueBox(sum(data$State == "CA"),
         icon = 'fa-user')
```

### Texas

```{r}
#顯示文字方塊,帶入特定州別條件下的數值
valueBox(sum(data$State == "TX"),
         icon = 'fa-user')
```

### Florida

```{r}
#顯示文字方塊,帶入特定州別條件下的數值
valueBox(sum(data$State == "FL"),
         icon = 'fa-user')
```

Row
-------------------------------

### Failures By State

```{r}
#繪製各州別的汽車故障總數長條圖
p1 <- data %>%
         group_by(State) %>%          #依州別分群計算
         summarise(count = n()) %>%   #計算總數
         plot_ly(x = ~reorder(State,count,desc),     #X軸為州別,並依照數量遞減排序
                 y = ~count,          #Y軸為州別
                 color = "red",       #顏色為紅色
                 type = 'bar') %>%    #圖形類別為長條圖
layout(xaxis = list(title = "Failures By State"),   #X軸的名稱
yaxis = list(title = 'Count'))                      #Y軸的名稱
p1   #呼叫圖形
```

### Top States

```{r}
#繪製汽車故障總數大於50的州佔比甜甜圈圖
p2 <- data %>%
         group_by(State) %>%
         summarise(count = n()) %>%
         filter(count>100) %>%    # 篩選總數大於50
         plot_ly(labels = ~State,
                 values = ~count,
                 marker = list(colors = mycolors)) %>%  #指定顏色
         add_pie(hole = 0.5) %>%  #中間空圈大小
         layout(xaxis = list(zeroline = F,
                             showline = F,
                             showticklabels = F,
                             showgrid = F),
                yaxis = list(zeroline = F,
                             showline = F,
                             showticklabels=F,
                             showgrid=F))
p2
```

Map
========================================

### Map

```{r}
#繪製汽車故障州別(State)的地圖
car <- data %>%
         group_by(State) %>%
         summarize(total = n())
car$State <- abbr2state(car$State)

highchart() %>%
         hc_title(text = "Car Failures in US") %>%
         hc_subtitle(text = "Source: Vehiclefailure.csv") %>%
         hc_add_series_map(usgeojson, car,
                           name = "State",
                           value = "total",
                           joinBy = c("woename", "State")) %>%
         hc_mapNavigation(enabled = T)
```