ggplot2

Published by onesixx on


Cheat Sheet : 
https://ggplot2.tidyverse.org/   
https://www.rstudio.com/resources/cheatsheets/
Custom Cheat Sheet:
http://www.sthda.com/english/articles/32-r-graphics-essentials/125-ggplot-cheat-sheet-for-great-customization
Gallery:
http://www.ggplot2-exts.org/gallery/
http://sape.inf.usi.ch/quick-reference/ggplot2/
book:
http://r4ds.had.co.nz/data-visualisation.html

Intro.

  • Grammer of Graphic 이론에 기반하여 , 데이터를 aesthetic(color, shape, size) 고려하여
    graphic objects (points, lines, bars)에 매핑한다. 때로는 data의 statistical 변환을 주기도 한다. 
  • ggplot은 데이터 객체, 그래픽 객체의 분리 와 재사용의 의미가 있다.
  • 기본 그래픽 시스템은 그림 그리기 좋은 툴인 반면  ggplot2는 데이터를 이해하기 좋은 툴
  • 단점은 3D 안됨 (이거 정말 최악) 

기본구조

ggplot() + 
  layer(data = mpg, 
\t\tmapping = aes(x=displ, y=hwy, size=6, alpha=year, color=drv, group=class), 
\t\tgeom = 'point', 
\t\tstat = 'identity', 
\t\tposition = 'identity', 
\t\tparams = list(shape=21) )
  • Data와 Aesthetic을 Mapping하여 메타데이터를 생성하고, 
    1개 이상의  Layer를 Coordinate system에 추가
  • Mapping는
    Layer를 추가할때마다 상속(Inheritance)되고,
    재정의(overriding)/추가/ 삭제( NULL이용)이 가능하다. 
    단,  나중에 추가된 Layer의 aesthetic mapping은 상속되지 않는다.

구성요소

https://ggplot2.tidyverse.org/reference/index.html
https://www.r-bloggers.com/my-commonly-done-ggplot2-graphs/

Layer

data

  • data.table, data.table, data.frame, tbl, tbl_df

aes (Aeshthetic mapping)

  • Data를 plot요소(axe, color, size)에 mapping하는 방법 정의
  • legend의 기준

geom_* (Geometric object)

  • Obs.를 Shape( point, line, polygon, bar, text )으로 표현하는 기하학적 객체
  • 모든 geom은 default statistic을 가진다.

• geom_histogram = stat_bin + geom_bar
• geom_smooth = stat_smooth + geom_ribbon
• geom_density = stat_density + geom_ribbon

stat_* (Statistical transformation)

  • binning, quantiles, smoothing 등의 통계변환 => histogram, smooth, density
  • default stat대신에 새로운 변수 생성(count, prop)
  • 모든 statistic은 default geom 을 가진다.

position

  • identity를 기본값으로 사용, dodge / fill jitter 

Guides

Scales

  • Axis,  Labels, Legends
  • Linear scaling of x and y axes (linear scale)
  • 스케일변환 / 라벨/ 범례

coord_(Coordinate system)

  • Cartesian 직교좌표를  default사용
    – coord_flip()       x와y 바꾸기
    – coord_quickmap()     지도에 정확한 aspect ratio 설정
    – coord_polar()  극좌표계   (ex, bar chart &  Coxcomb chart)

Facetting

  • plot을 조건에 맞게panel별로 data의 subset 표기(분할)
    – facet_grid(<fomula>) 
    – facet_wrap(<fomula>, nrow=3))

Guides

Themes

  • Annotation: 주석표기 
  • Fortify

zooming

ggplot2의 내부보기

mpgPlot <- mpg %>% ggplot(aes(x=displ, y=hwy)) + geom_point()
mpgPlot %>% summary()
data: manufacturer, model, displ, year, cyl, trans, drv, cty, hwy, fl, class [234x11]
mapping:  x = ~displ, y = ~hwy
faceting: <ggproto object: Class FacetNull, Facet, gg>
    compute_layout: function
    draw_back: function
    draw_front: function
    draw_labels: function
    draw_panels: function
    finish_data: function
    init_scales: function
    map_data: function
    params: list
    setup_data: function
    setup_params: function
    shrink: TRUE
    train_scales: function
    vars: function
    super:  <ggproto object: Class FacetNull, Facet, gg>
-----------------------------------
geom_point: na.rm = FALSE
stat_identity: na.rm = FALSE
position_identity 

참고

메인pdf:
https://cran.r-project.org/web/packages/ggplot2/ggplot2.pdf

Resource
https://learnr.wordpress.com/2009/05/18/ggplot2-three-variable-time-series-panel-chart/
http://tutorials.iq.harvard.edu/R/Rgraphics/Rgraphics.html#org62fb6d5

ggplot2 resources

IQSS resources

http://freesearch.pe.kr/archives/3134 ggplot2를 이용한 R 시각화
https://rstudio-pubs-static.s3.amazonaws.com/130543_a9aa02e01e3e4e609c8b6085f4383a4f.html

book:: R로 하는 데이터 시각화

ggplot2 – 01 Grammar of Graphics
ggplot2 – 02 Drawing Graphs
ggplot2 – 03

Categories: ggplot2

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x