## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
## [1] "/Users/baoen/d4sg"

70 歲以下之十大死因平均生命年數損失

data_1=read.csv("70 歲以下之十大死因平均生命年數損失.csv",header=T)
data_1a=data_1[order(data_1$合計), ]
data_1a=data_1a[-9, ]

marker_1 = list(color = c('rgba(204,204,204,1)', 'rgba(204,204,204,1)',
                        'rgba(204,204,204,1)', 'rgba(204,204,204,1)',
                        'rgba(204,204,204,1)', 'rgba(204,204,204,1)',
                        'rgba(204,204,204,1)', 'rgba(204,204,204,1)',
                        'rgba(204,204,204,1)', 'rgba(222,45,38,0.8)'))

t <- list(size = 18)
m <- list(t = 50)
plot_ly(data_1a, y= ~ X, x = ~合計, name = "合計", type = "bar",
        marker = marker_1) %>%
  layout(
    title = "70 歲以下之十大死因平均生命年數損失",
    font = t,autosize = F, margin = m,
    xaxis = list(domain = c(0.3, 4), title = '平均損失年數'),
    yaxis = list(domain = c(0.1, 4), title = " ", 
                 categoryarray = data_1a$合計))

###105年急診醫療費用

data_2=read.csv("105年急診醫療費用.csv",header = F)
data_2a=data_2[order(data_2$V2), ]
data_2a=data_2a[-23, ]
marker = list(color = c('rgba(204,204,204,1)', 'rgba(204,204,204,1)',
                        'rgba(204,204,204,1)', 'rgba(204,204,204,1)',
                        'rgba(204,204,204,1)', 'rgba(204,204,204,1)',
                        'rgba(204,204,204,1)', 'rgba(204,204,204,1)',
                        'rgba(204,204,204,1)', 'rgba(204,204,204,1)',
                        'rgba(204,204,204,1)', 'rgba(204,204,204,1)',
                        'rgba(204,204,204,1)', 'rgba(204,204,204,1)',
                        'rgba(204,204,204,1)', 'rgba(204,204,204,1)',
                        'rgba(204,204,204,1)', 'rgba(204,204,204,1)',
                        'rgba(204,204,204,1)', 'rgba(204,204,204,1)',
                        'rgba(222,45,38,0.8)', 'rgba(204,204,204,1)'))
plot_ly(data_2a, x = ~V2, y = ~V1, type = "bar", marker = marker) %>%
  layout(title = "105年急診醫療費用",
    margin = m,
    xaxis = list(domain = c(0.3, 1), title = "醫療費用"),
    yaxis = list(domain = c(0.1, 1), title = ' ', 
                 categoryarray = data_2a$V1))

105年急診就診件數

data_3=read.csv("105年急診就診件數.csv",header = F)
data_3a=data_3[order(data_3$V2), ]
data_3a=data_3a[-23, ]
plot_ly(data_3a, x = ~V2, y = ~V1, type = "bar", marker = marker) %>%
  layout(title = "105年急診就診件數",
    margin = m,
    xaxis = list(domain = c(0.3, 1), title = "就診件數"),
    yaxis = list(domain = c(0.1, 1), title = ' ', 
                 categoryarray = data_3a$V1))

105年急診就診率

data_4=read.csv("105年急診就診率.csv",header = F)
data_4a=data_4[order(data_4$V2), ]
data_4a=data_4a[-23, ]

marker = list(color = c('rgba(204,204,204,1)', 'rgba(204,204,204,1)',
                        'rgba(204,204,204,1)', 'rgba(204,204,204,1)',
                        'rgba(204,204,204,1)', 'rgba(204,204,204,1)',
                        'rgba(204,204,204,1)', 'rgba(204,204,204,1)',
                        'rgba(204,204,204,1)', 'rgba(204,204,204,1)',
                        'rgba(204,204,204,1)', 'rgba(204,204,204,1)',
                        'rgba(204,204,204,1)', 'rgba(204,204,204,1)',
                        'rgba(204,204,204,1)', 'rgba(204,204,204,1)',
                        'rgba(204,204,204,1)', 'rgba(204,204,204,1)',
                        'rgba(204,204,204,1)', 'rgba(204,204,204,1)',
                        'rgba(222,45,38,0.8)', 'rgba(204,204,204,1)'))

plot_ly(data_4a, x = ~V2, y = ~V1, type = "bar", marker = marker) %>%
  layout(title = "105年急診就診率",
    margin = m,
    xaxis = list(domain = c(0.4, 1), title = "就診率(每十萬人)"),
    yaxis = list(domain = c(0.1, 1), title = ' ', 
                 categoryarray = data_4a$V1))

年齡與受傷機轉

#次數分配表
ecode_age.table<-table(data$age,data$ecode_type)
#轉換成資料框架
ecode_age<-as.data.frame.matrix(ecode_age.table)
ecode_age <- cbind(age = rownames(ecode_age), ecode_age)
ecode_age$age <- as.numeric(as.character(ecode_age$age))
total= rowSums(ecode_age)
ecode_age_rate <- data.frame(cbind(ecode_age$`1`/total,ecode_age$`2`/total,
                        ecode_age$`3`/total,ecode_age$`4`/total,
                        ecode_age$`5`/total,ecode_age$`6`/total,
                        ecode_age$`7`/total,ecode_age$`8`/total,
                        ecode_age$`9`/total,ecode_age$`10`/total,
                        ecode_age$`11`/total,ecode_age$`12`/total))
colnames(ecode_age_rate) <- c('1','2','3','4','5','6','7','8','9','10','11','12')
ecode_age_rate <- cbind(age = rownames(ecode_age_rate), ecode_age_rate)
ecode_age_rate$age <- as.numeric(as.character(ecode_age_rate$age))
is.data.frame(ecode_age_rate)
## [1] TRUE
button_type<-list(
  xanchor = 'center',
  yanchor = "top",
  x = -0.1,
  y = 1.27,
  buttons = list(
    list(method = "update",
         args = list(list(visible = c(TRUE, FALSE, FALSE, FALSE, FALSE, FALSE,
                                      FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))),
         label = "車禍"),
    
    list(method = "update",
         args = list(list(visible = c(FALSE, TRUE, FALSE, FALSE, FALSE, FALSE,
                                      FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))),
         label = "跌倒摔落"),
    
    list(method = "update",
         args = list(list(visible = c(FALSE, FALSE, TRUE, FALSE, FALSE, FALSE,
                                      FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))),
         label = "壓砸傷"),
    
    list(method = "update",
         args = list(list(visible = c(FALSE, FALSE, FALSE, TRUE, FALSE, FALSE,
                                      FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))),
         label = "窒息"),
    
    list(method = "update",
         args = list(list(visible = c(FALSE, FALSE, FALSE, FALSE, TRUE, FALSE,
                                      FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))),
         label = "穿刺切割傷"),
    
    list(method = "update",
         args = list(list(visible = c(FALSE, FALSE, FALSE, FALSE, FALSE, TRUE,
                                      FALSE, FALSE, FALSE, FALSE, FALSE, FALSE))),
         label = "燒燙傷"),
    
    list(method = "update",
         args = list(list(visible = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
                                      TRUE, FALSE, FALSE, FALSE, FALSE, FALSE))),
         label = "電擊傷"),
    
    list(method = "update",
         args = list(list(visible = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
                                      FALSE, TRUE, FALSE, FALSE, FALSE, FALSE))),
         label = "中毒"),
    
    list(method = "update",
         args = list(list(visible = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
                                      FALSE, FALSE, TRUE, FALSE, FALSE, FALSE))),
         label = "溺水"),
    
    list(method = "update",
         args = list(list(visible = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
                                      FALSE, FALSE, FALSE, TRUE, FALSE, FALSE))),
         label = "自然災害"),
    
    list(method = "update",
         args = list(list(visible = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
                                      FALSE, FALSE, FALSE, FALSE, TRUE, FALSE))),
         label = "自殺"),
    
    list(method = "update",
         args = list(list(visible = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
                                      FALSE, FALSE, FALSE, FALSE, FALSE, TRUE))),
         label = "重大運輸事故"),
    
    list(method = "update",
         args = list(list(visible = c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
                                      TRUE, TRUE, TRUE, TRUE, TRUE, TRUE))),
         label = "reset")))

plot_ly(ecode_age_rate, x = ~age) %>%
  add_lines(y = ~`1`, name = "車禍") %>%
  add_lines(y = ~`2`, name = "跌倒摔落") %>%
  add_lines(y = ~`3`, name = "壓砸傷") %>%
  add_lines(y = ~`4`, name = "窒息") %>%
  add_lines(y = ~`5`, name = "穿刺切割傷") %>%
  add_lines(y = ~`6`, name = "燒燙傷") %>%
  add_lines(y = ~`7`, name = "電擊傷") %>%
  add_lines(y = ~`8`, name = "中毒") %>%
  add_lines(y = ~`9`, name = "溺水") %>%
  add_lines(y = ~`10`, name = "自然災害") %>%
  add_lines(y = ~`11`, name = "自殺") %>%
  add_lines(y = ~`12`, name = "重大運輸事故") %>%
  
  layout(
    title = "年齡與受傷機轉",
    margin = m,
    xaxis = list(domain = c(0.2, 4), title = '年齡'),
    yaxis = list(domain = c(0.1, 1), title = "比率"),
    updatemenus = list(button_type))

車禍人口金字塔

traffic=read.table("traffic.csv",sep = ",",header = T)
plot_ly(traffic, type = "bar", orientation = 'h') %>%
  add_trace(
    x = ~Freq[traffic$Gender==1],
    y = ~Age[traffic$Gender==1],
    marker = list(
      color = 'blue'
    ),
    name = '男性'
  ) %>%
  add_bars(
    x = ~Freq[traffic$Gender==2],
    y = ~Age[traffic$Gender==2],
    marker = list(
      color = 'red'
    ),
    name = '女性'
  ) %>%
  layout(title = "車禍人口金字塔", bargap = 0.1, barmode = 'overlay',
    margin = m,
         yaxis = list(domain = c(0.1, 1), title = "年齡"),
         xaxis = list(domain = c(0.1, 1), tickmode = 'array',title = "人數"),
         updatemenus = list(
           list(
             type = "buttons",
             y = 0.5,
             buttons = list(
               
               list(method = "update",
                    args = list(list(visible = c(FALSE, TRUE))),
                    label = "男"),
               
               list(method = "update",
                    args = list(list(visible = c(TRUE, FALSE))),
                    label = "女"),
               
               list(method = "update",
                    args = list(list(visible = c(TRUE, TRUE))),
                    label = "reset")))
         ))

跌倒人口金字塔

fall=read.table("fall.csv",sep = ",",header = T)
plot_ly(fall, type = "bar", orientation = 'h') %>%
  add_trace(
    x = ~Freq[fall$Gender==1],
    y = ~Age[fall$Gender==1],
    marker = list(
      color = 'blue'
    ),
    name = '男性'
  ) %>%
  add_bars(
    x = ~Freq[fall$Gender==2],
    y = ~Age[fall$Gender==2],
    marker = list(
      color = 'red'
    ),
    name = '女性'
  ) %>%
  layout(title = "跌倒人口金字塔", bargap = 0.1, barmode = 'overlay',
         margin = m,
         yaxis = list(domain = c(0.1, 1), title = "年齡"),
         xaxis = list(domain = c(0.1, 1), tickmode = 'array',title = "人數"),
         updatemenus = list(
           list(
             type = "buttons",
             y = 0.5,
             buttons = list(
               
               list(method = "update",
                    args = list(list(visible = c(FALSE, TRUE))),
                    label = "男"),
               
               list(method = "update",
                    args = list(list(visible = c(TRUE, FALSE))),
                    label = "女"),
               
               list(method = "update",
                    args = list(list(visible = c(TRUE, TRUE))),
                    label = "reset")))
         ))