• 經濟資料視覺化處理
  • 課程介紹
    • Course Objectives
    • Before you start
    • 環境設定
      • In-class repo
      • 下載
      • Starting chunk
  • 1 Data cleaning
    • 1.1 data frame
    • 1.2 dplyr套件
      • 1.2.1 slice:切割資料行
      • 1.2.2 mutate:產生變數
      • 1.2.3 summarise:計算特徵值
      • 1.2.4 select:選擇變數
      • 1.2.5 filter:條件選取觀測值
      • 1.2.6 group_by:分群計算
    • 1.3 Pipe operator
    • 1.4 gather and spread
      • 1.4.1 gather
      • 1.4.2 spread
    • 1.5 變數類別調整
    • 1.6 Data import
  • 2 ggplot2: geom
    • 2.1 Grammar of Graphics
      • 引入資料
      • 基本語法
      • 層疊概念
      • 美學對應
    • 2.2 字串變數
      • 類別資料
      • 時間資料
    • 2.3 Geom介紹:boxplot
    • 2.4 設定中文字體
      • 查查有什麼字體
      • 設定字體
    • 2.5 圖形存檔
    • 2.6 每週一圖作業示範
  • 3 Aesthetic scales
    • 3.1 color/colour
      • 3.1.1 常用套件
      • 3.1.2 顏色模型
      • 3.1.3 調色盤palette
      • 3.1.4 ggplot應用
      • 3.1.5 其他
      • 3.1.6 練習
    • 3.2 Linetype
      • 使用線形名稱
      • 使用hex碼
      • 3.2.1 scale_linetype套用
    • 3.3 Date/Time
      • 3.3.1 limits: scale變數呈現範圍
      • 3.3.2 breaks: scale變數圖例說明範圍
      • 3.3.3 labels: scale變數圖例說明文字對應
    • 3.4 geom_text
    • 3.5 練習
    • 3.6 參考資料
  • 4 stat functions and positions
    • 4.1 直方圖
      • 4.1.1 間斷變數
      • 4.1.2 geom_col
      • 4.1.3 geom_bar
      • 4.1.4 連續變數
      • 4.1.5 geom_histogram
      • 4.1.6 optimal bins
    • 4.2 stat function
      • 4.2.1 一體兩面stat/geom
      • 4.2.2 override geom
      • 4.2.3 override stat
      • 4.2.4 自創stat函數
      • 4.2.5 stat_summary
    • 4.3 Positions
      • 4.3.1 stack
      • 4.3.2 fill
      • 4.3.3 dodge
      • 4.3.4 geom_bar應用
      • 4.3.5 其他position
  • 5 Annotation and Maps
    • 5.1 地圖基本元素
      • 5.1.1 Point, line, polygon
      • 5.1.2 Matrix expression
      • 5.1.3 北台灣範例
      • 5.1.4 Polygon with holes
      • 5.1.5 Multipolygons
    • 5.2 Annotation
      • 5.2.1 線、文字
      • 5.2.2 圖片
      • 5.2.3 QR code
    • 5.3 Simple features
    • 5.4 Coordinate Reference Systems (CRS)
    • 5.5 架構sf data frame
      • 5.5.1 Geometry (sfg)
      • 5.5.2 Column (sfc)
      • 5.5.3 與data frame合併 (sf)
      • 5.5.4 儲存成shp檔
    • 5.6 讀入shp檔
    • 5.7 常見圖資運算
      • 5.7.1 CRS轉換: st_transform()
      • 5.7.2 找中心點: st_centroid()
      • 5.7.3 輸出座標: st_coordinates()
      • 5.7.4 切出部份範圍: st_crop()
      • 5.7.5 幾何簡化: rmapshaper::ms_simplify()
      • 5.7.6 st_cast
      • 5.7.7 更多其他操作
    • 5.8 ggplot地圖繪製
      • 5.8.1 繪製goemetry: geom_sf()
      • 5.8.2 加文字: geom_sf_text(), geom_sf_label()
      • 5.8.3 設座標系統: coord_sf()
    • 5.9 練習:捷運車站
      • 5.9.1 CRS
      • 5.9.2 取出單線站點
      • 5.9.3 站點連成路線
      • 5.9.4 標站名
    • 5.10 其他相關圖資套件
      • 5.10.1 osmdata
      • 5.10.2 ggmap
      • 5.10.3 RQGIS
    • 5.11 其他圖資格式
      • 5.11.1 Raster
      • 5.11.2 GeoJSON
    • 5.12 Reference
  • 6 Guide, Facet and Theme
    • 6.1 Guides (axes+legend)
      • 6.1.1 Guides: axes
      • 6.1.2 Guides: legend
    • 6.2 Facet
      • 6.2.1 範例
      • 6.2.2 Option: scales
      • 6.2.3 範例
      • 6.2.4 Option: space
    • 6.3 Theme
      • 6.3.1 Component: plot
      • 6.3.2 Component: legend
    • 6.4 Size
      • 6.4.1 grid::unit()
      • 6.4.2 npc / snpc
      • 6.4.3 pt
      • 6.4.4 lines
      • 6.4.5 Guidelines
    • 6.5 Customize your graph
  • 7 Web application
    • 7.1 Webpage
      • 7.1.1 The basic
      • 7.1.2 A simple website project
      • 7.1.3 file path
      • 7.1.4 html tags
      • 7.1.5 div and span
      • 7.1.6 attributes and style
      • 7.1.7 CSS
      • 7.1.8 Javascript
      • 7.1.9 SVG
      • 7.1.10 JSON
      • 7.1.11 Online form
    • 7.2 不同步傳輸
    • 7.3 Webpage hosting
    • 7.4 flexdashboard
      • 7.4.1 Setup
      • 7.4.2 Publish
      • 7.4.3 External setup
      • 7.4.4 Inline web language
      • 7.4.5 Sidebar control
    • 7.5 流程圖:Sidebar UI設計流程圖
    • 7.6 流程圖:SVG生成與引入
  • 8 htmlwidgets (I): plotly
    • 8.1 HTML and Graphs
      • 8.1.1 靜態圖形引入html
      • 8.1.2 Rmd引入svg
      • 8.1.3 ggplot與svg
    • 8.2 網頁互動
      • 8.2.1 htmlwidget
      • 8.2.2 widget生成流程
      • 8.2.3 htmlwidget的引用
    • 8.3 Plotly.R
      • 8.3.1 產生
      • 8.3.2 修改
      • 8.3.3 使用
    • 8.4 Hover
      • 8.4.1 Hover info
      • 8.4.2 Hover text
    • 8.5 Plotly to htmlwidget
    • 8.6 Plotly.js
      • 8.6.1 Logics
      • 8.6.2 設定JSON資料
      • 8.6.3 外部儲存JSON資料
      • 8.6.4 引入js
    • 8.7 流程圖:htmlwidgets生成與引入
  • 9 htmlwidgets (II): integrate with flexdashboard
    • 9.1 Flexdashboard流程
    • 9.2 客製化面版互動
      • 9.2.1 面版影響plot
      • 9.2.2 plotly影響面版
      • 9.2.3 Events
    • 9.3 Animation
      • 9.3.1 animation_opts
      • 9.3.2 annimation_slider
      • 9.3.3 annimation_button
    • 9.4 Crosstalk
    • 9.5 Other themes
    • 9.6 Then what?
  • 10 中文亂碼
    • 10.1 範例
      • 方法一
      • 方法二
  • 國立臺北大學 經濟學系

經濟資料視覺化處理

第 10 章 中文亂碼

假設字串A的內容是中文亂碼,可試用以下兩種方式解決:

Encoding(A) <- "UTF-8" # 此函數為generic replacement,A內容會被改掉,不用另外回存A
tmcn::toUTF8(A) -> A 

10.1 範例

load(url("https://github.com/tpemartin/course-108-1-inclass-datavisualization/blob/master/%E4%BD%9C%E5%93%81%E5%B1%95%E7%A4%BA/homework1/graphData_homework2019-10-07_001.Rda?raw=true"))

在Windows下可能出現亂碼的是:

  • colnames(graphData$Case_10_result)

  • graphData$Case_10_result$六都

方法一

A <- colnames(graphData$Case_10_result) # 把有問題的字串叫A
Encoding(A) <- "UTF-8"
A -> colnames(graphData$Case_10_result) # 記得把轉好的A存回源頭

A <- as.charater(graphData$Case_10_result$`六都`) # Encoding只對character class有效,此變數為factor,所以要先改class
Encoding(A) <- "UTF-8"
A -> graphData$Case_10_result$六都

# graphData$Case_10_result %>% View

方法二

A <- colnames(graphData$Case_10_result) # 把有問題的字串叫A
tmcn::toUTF8(A) -> A
A -> colnames(graphData$Case_10_result) # 記得把轉好的A存回源頭

A <- graphData$Case_10_result$`六都` # tmcn::toUTF8可應付廣義class,無需轉class
tmcn::toUTF8(A) -> A
A -> graphData$Case_10_result$`六都`

# graphData$Case_10_result %>% View