ggplot2

Published onesixx on

http://onesixx.com/ggplot2-0/
Cheat Sheet : https://ggplot2.tidyverse.org/    (https://www.rstudio.com/resources/cheatsheets/)
http://r4ds.had.co.nz/data-visualisation.html
http://www.ggplot2-exts.org/gallery/
http://sape.inf.usi.ch/quick-reference/ggplot2/

ggplot2의 내부

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

 

plot 구성요소

Data와 Aesthetic을 Mapping하여 메타데이터를 생성하고,  1개 이상의  Layer를 Coordinate system에 추가

ggplot(data = <DATA>) + 
    <GEOM_FUNCTION>( mapping = aes(<MAPPINGS>),
                        stat = <STAT>, 
                    position = <POSITION>  ) +
    <COORDINATE_FUNCTION> +
    <FACET_FUNCTION>
ggplot() +
  layer(data = mpg,
        mapping = aes(x=displ, y=hwy,  size=6, alpha=year, color=drv, group=class),
        geom = 'point',
        stat = 'identity',
        position = 'identity',
        params = list(shape=21)
   )

Mapping는 Layer를 추가할때마다 상속(Inheritance)되고, 재정의(overriding)/추가/ 삭제( NULL이용)이 가능하다. 
단,  추가된 Layer의 aesthetic mapping은 상속되지 않는다.

https://ggplot2.tidyverse.org/reference/index.html

Layer data     data.table, data.frame, tbl, tbl_df
mapping Aestheric mappings
legend의 기준이 됨
axe, color, size(그래프요소)에 Data(데이터요소)를
mapping(대응)하는 방법
Geom   Geometric object

Shape of the elements

Obs.를 point, line, polygon, bar, text 으로 표현하는 기하학적 객체
=> boxplot, point, range

모든 geom은 default statistic을 가진다. 

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)
스케일변환 / 라벨/ 범례
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)
non-data elements Themes   Annotation: 주석표기 
Fortify
 
         
  zooming      
         

 

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

 

Categories: ggplot2

onesixx

Blog Owner

Leave a Reply

Your email address will not be published.