羅德興老師的教學歷程檔案 - 107-1 資訊科技大數據分析 - 繪圖分析(台灣例 1)
 

企業資訊與管理系
助理教授/日導
羅德興


歷程檔案 Portfolio

    繪圖分析(台灣例 1)

    資料檔tw_population3.csv  [download]
    # ex-taiwan10.r
    # http://www.rpubs.com/OzuShi/348822
    # for R-3.5.1
    # B3
     
    # 先至 台灣內政部 open data 下載臺灣行政區人口列表,
    # 儲存為 tw_population3.csv
     
    install.packages("dplyr")
    install.packages("data.table")
    install.packages("ggplot2")
    install.packages("maptools")
    install.packages("knitr")
    install.packages("kableExtra")
    # install.packages("fread")
    install.packages("mapproj")
     
    setwd("d:/temp0922")
    getwd()
     
    # Popution visualization using a bar plot
    # Read and plot Taiwan map data
    # How to plot a Taiwan Map colored with Population by ggplot2
    # Require packages and summary population data
     
    require("dplyr")
    require("stringr")
    require("data.table")
    require("ggplot2")
    require("maptools")
    require("knitr")
    require("kableExtra")
    library("mapproj")
     
    df <- read.csv("tw_population3.csv", h = T)
    df <- data.frame(df)
    View(df)
    summary(df)
     
    # Popution visualization using a bar plot
    # The following steps aims to make a better plot. 
    # 1. transform population unit to 10,000 units 
    # 2. flip the coordinate 3. sort the cities by its population
     
    colnames(df) <- c("city","category","population")
    ggplot(df, aes(x = reorder(city, population), y = population/10000, fill = category)) + 
      geom_bar(stat="identity") +
      coord_flip() + 
      labs(title = "台灣縣市人口分布圖", x = "縣市", y = "人口數(萬)") 
     
    # Read and plot Taiwan map data
    # Taiwan population data source is from the following website: http://www.gadm.org/country (shapefile form)
    # 1. Read shape file
    # 2. Tranform shp to dataframe
    # 3. Plot Taiwan Map by ggplot2
     
    taiwan_shp <- readShapeSpatial("gadm36_TWN_shp/gadm36_TWN_0.shp")
     
    taiwan_map <- fortify(taiwan_shp)
     
    ## Regions defined for each Polygons
    ggplot(taiwan_map, aes(x = long, y = lat, group=group)) +
      geom_path() + 
      coord_map()
     
     
    # Translate City Names from English to Chinese
    # Sys.setlocale('LC_ALL','C')
    print(as.character(taiwan_shp$NAME_2))
     
    全部共 0則留言
    登入帳號密碼代表遵守學術網路規範


    文章分類 Labels


    最新文章 Top10

    中華科技大學數位化學習歷程 - 意見反應