Skip to content

Instantly share code, notes, and snippets.

@nacyot
Created October 24, 2013 07:23
Show Gist options
  • Save nacyot/7132723 to your computer and use it in GitHub Desktop.
Save nacyot/7132723 to your computer and use it in GitHub Desktop.

Visualization and Data Mapping

King Sejong

./images/세종대왕.jpeg

William Playfair

Score Data

ClassMathematicsKoreanEnglishScience
Score80906050

Source Code of Bar Plot

barplot(
    c(80, 90, 60, 70), 
    names.arg=c("Mathematics", "Korean", "English", "Science"), 
    col=rainbow(4),
    ylim=c(0, 100) 
    )

Graph

./images/bar_graph_rainbow.png

Data mapping

  • 데이터(변수)를 눈에 보이는 요소에 연결 시키는 일
  • 위 표에서 우리가 가진 변수
    • 과목(Class) : Nominal
    • 점수(Score) : Ratio

Variables => Aesthetics

  • Class => bar or bar.name
  • Score => bar.height

Graph Again

./images/bar_graph_rainbow.png

Where is the color?

  • Graph literacy

Variables => Aesthetics

  • Data.Class => bar.name
  • Data.Class => bar.color (Duplication!)
  • Data.Score => bar.height
barplot(
    # Date.Score => bar.height
    c(80, 90, 60, 70), 
    # Data.Class => bar.name 
    names.arg=c("Mathematics", "Korean", "English", "Science"), 
    # Data.Class => bar.color
    col=rainbow(4),
    # Y-axis range
    ylim=c(0, 100)
    )

Barplots

Useless Colors

Remove color

barplot(
    # Date.Score => bar.height
    c(80, 90, 60, 70), 
    # Data.Class => bar.name
    names.arg=c("Mathematics", "Korean", "English", "Science"), 
    # Y-axis range
    ylim=c(0, 100)
    )

Graph

./images/bar_graph_gray.png

Libreoffice Calc

./images/bar_graph_calc.png

Another Data

ClassKoreanEnglishPhysicsBiologyChemisry
CategoryLanguageLanguageScienceScienceScience
Score9085707560

Data Mapping Again

  • Data.Class => bar.name
  • Data.Category => bar.color
  • Data.Score => bar.height
barplot(
    # Date.Score => bar.height
    c(90, 85, 70, 75, 60), 
    # Data.Class => bar.name
    names.arg=c("Korean", "English", "Physics", "Biology", "Chemistry"),
    # Data.Category => Color
    col=c("gold", "gold", "azure", "azure", "azure")
    # Y-axis range
    ylim=c(0, 100)
    )

Graph

./images/bar_graph_type.png

ggplot2

  • Hadley Wickham
  • 그래프를 그리기 위한 R 패키지
  • R에서 가장 많이 쓰이는 패키지
# ggplot2 패키지 설치
install.packages('ggplot2')
# ggplot2 패키지 로드
library(ggplot2)

The grammar of graphics


  • Leland Wilkinson
  • ggplot2는 이 책의 이론에 기반
  • ggplot2 gg는 The grammar of graphics의 줄임말

Data

data_class =
    c("Korean", "English", "Physics", "Biology", "Chemistry")
data_score =
    c(90, 85, 70, 75, 60)
data_category =
    c("language", "language", "science", "science", "science")
score_data = data.frame(
    class = data_class,
    category = data_category,
    score =data_score
    )

# 평가
      class category score
1    Korean language    90
2   English language    85
3   Physics  science    70
4   Biology  science    75
5 Chemistry  science    60

Source Code

# 그래프 시작, 데이터 지정
bargraph = ggplot(score_data) +
    # 그래프 종류 지정
    geom_bar() +
    # 데이터와 시각적 요소의 맵핑 1
    # Data.Class => bar.name 
    aes(x = class) +
    # 데이터와 시각적 요소의 맵핑 2
    # Data.Score => bar.height
    aes(y = score) +
    # Y축 범위 지정
    ylim(0, 100)

Graph

./images/bar_graph_ggplot_first.png

Mapping Category Data To Color

# 그래프 시작, 데이터 지정
bargraph = ggplot(score_data) +
    # 그래프 종류 지정
    geom_bar() +
    # 데이터와 시각적 요소의 맵핑 1
    # Data.Class => bar.name 
    aes(x = factor(class)) +
    # 데이터와 시각적 요소의 맵핑 2
    # Data.Score => bar.height
    aes(y = score) +
    # 데이터와 시각적 요소의 맵핑 3
    # Data.Category => bar.color
    aes(fill = category) + 
    # Y축 범위 지정
    ylim(0, 100)

Graph

./images/bar_graph_ggplot_second.png

Coordination

# 그래프 시작, 데이터 지정
bargraph = ggplot(score_data) +
    # 그래프 종류 지정
    geom_bar() +
    # 데이터와 시각적 요소의 맵핑 1
    # Data.Class => bar.name 
    aes(x = class) +
    # 데이터와 시각적 요소의 맵핑 2
    # Data.Score => bar.height
    aes(y = score) +
    # 데이터와 시각적 요소의 맵핑 3
    # Data.Category => bar.color
    aes(fill = category) + 
    # 좌표계 지정
    coord_flip() +
    # Y축 범위 지정
    ylim(0, 100)

Graph

./images/bar_graph_ggplot_third.png

Creating Graph Process

./images/gg_process.png

Creating Graph Process

  • Variables
  • Algebra
  • Scales
  • Statistics
  • Geometry
  • Coordinates
  • Aesthetics

Creating Graph Process

  • Process
    • Variables
    • Algebra
    • Scales
    • Statistics
    • Geometry
    • Coordinates
    • Aesthetics
  • Who
    • Science
    • Programmer
    • Designer

Mathematical Graph

  • Two variables
  • ./images/xy.png

3 Dimension

  • Three variables
  • ./images/xyz.png

Gapminder

  • Prof. Hans Rosling
  • ./images/gapminder.png

What Dimension?

  • XY space is not 2 demention in visualization
    • X
    • Y
    • Color
    • Size
    • Interaction
      • Year
      • Nation

Charles Joseph Minard

  • What Dimension?

./images/Minard.png

Lapisan

MangakaMap

  • 2008
  • ./images/mangaka.png

Putne

  • 2013
  • ./images/putne.png

Moplo

  • 2013
  • ./images/moplo.png

Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment