* ggplot2の本を買ったので、まとめを書く。 :R: ** GeoMetric Object :geom: geom_xxx と書かれ、グラフの図示の仕方?を決める point, smooth, path, line, histogram, density, freqploy, bar, jitter ** Layer 5つのlayer(geom, geom_params, stat, stat_params, data, mapping, position)がある どのレイヤーを使い、そこでは何かをfillしたりする。 statも計算し、それをパラメータに沿って表現する 実際に使う場合は、shortcutの形でデフォルト値を利用する (geom_XXX)[mappig, data, ..., geom, position], (stat_XXX)[mapping, data,,,, summary(p) とすると、その内訳がでる。*pはグラフ化オブジェクト layerは、Rのオブジェクトなので、あとから利用できる bestfitだけ定義しておいて、順次、データに当てはめていく *** data 普通に対象のデータフレーム。通常のデフォルト設定をoverrideするもの データはあとで、差し替え可能 %+%という書式。他のは無理そう。 *** parameters geom, statの両方にある。 *** DONE mapping CLOSED: [2010-12-25 土 10:56] aesといっしょに使われる。実際にグラフ上に描画するもの xとy aes(x=weight, y=height, colour=age)とか、、 あとから、値を変えられる(add, override, removeは、通常の代入の形) aesは値をコピーしてる。geom_point(aes(colour="darkblue"))は、"darkblue"の値を作る。 (一方、geom_point(colour="darkblue")は、表示をdarkblueにする) *** Grouping individualにいくか? collectiveにいくか? => groupの役割 collectiveに行く場合は、statの力を使ってる **** 3つのcommon case 1. たくさんのsubjectをひとつのグループにしたstatを表示 geom_line 2. あるまとまりごと、discreteなものに対して適用 geom_boxplot 3. 個別geomsの違いを際立たせる。collective geomや、blob of pointsにする 難しいので飛ばす *** (geom) or (stat) 1. geom 表がある。本の中に。少しだけ抜き書き。 | <10> | <26> | <10> | <26> | | Name | Description | Dafault stat | Aesthetics | |------------+----------------------------+------------+----------------------------| | bar | Bars, rectangle with bases on y-axix | bin | colour, fill,linetype | | boxplot | Box and whisker plot | boxplot | colour, fill,lower, middle,size,upper,weight, x, ymax, ymin | | tile | Tile plot as densely as possible, assuming that every tile is the same size | identity | colour, fill, linetype, size, x,y | | line | connect observations, in order of x value | identity | colour, linetype, size, x, y | | | | | | 2. stats これも表がある | Name | Description | | bin | Bin data | | boxplot | Caluculate components of box-and-whisker plot | | contour | Contours of 3d data | | density | Density estimation 1d | | density_2d | Density estimation 2d | | function | Superimpose a function | | identity | Don't transform data | | qq | Calculation for quantile-quantile plot | | quantile | Continuous quantiles | | smooth | Add a smoother | | spoke | Convert angle and radius to xend and yend | | step | Create stair steps | | sum | Sum unique values. Useful for overplotting on scatter-plots | | summary | Summarise y values at every unique x | | unique | Remove duplicates | qplot(carat, ..density.., ..density..とダブルドットなのは、 既存のcolumnNameとの衝突を防ぐため ** Positioning(主にfacettingについて) :facet: *** Position adjustments dodge, fill, identity, jitter, stackの5種類 indentityはそのまんまという意味? *** DONE Position scales CLOSED: [2010-12-26 日 16:16] linearとかlogとか。scale_x_log10() <= scale_x_continuous(trans="log10") xlim(10,20), xlim("a","b","c), xlim(as.Date(c("2008-02-23","2008-03-23")))とか。 scale_x_date(format="%m%)とか expandで軸?を拡張できる expand = c(1.2,0.3) 最初が倍率、二番目は足し算 coord_cartesianでは、数値処理の見た目をへんこうできる。 discreteに関しては、breakと *** Faceting facet_gridとfacet_wrapがある *** ToolBox layerについて学べる。dataを描き、summaryを見せ、meta dataを加える 基本、分布、散布、地図、統計サマリー、注釈、重み付けなどを見せる。 *** Basic plot 二次元を記述するので、x,yを指定。 どう記述するのか、 色、大きさ、はめ込み方(filled geomと、fill) point geomは、shapeとline path geomは、有向な線 | geom_area | area plot | | geom_bar(stat="identity") | barchart | | geom_line | scaterplot | | geom_polygon | polygon | | geom_text | label,hjust,vjust,angle | | geom_title | image, level, fill | *** disply distribution ヒストグラムが基本。binwidthを指定。 分類して表示したい場合 - facets_grid(cut ~ .) - geom_histogram(aes(fill = cut),binwidth = 0.1, posiiton="fill") - geom_freqpoly(aes(y = ..density.., colour = cut), binwidth = 0.1) 下の二つは、stat_binが使われている。 - geom_boxplotでもいける。 qplot(cut, depth, data=diamonds, geom="boxplot") qplot(carat, depth, data=diamonds, geom="boxplot", group=round_any(carat, 0.1, floor)) (これは、continuous data) - qplot(depth, data=diamonds, geom="density", xlim=c(54,70), fill=cut, alpha=I(0.2)) (これは、geom_freqployのカーネル近似版になる) *** scatterplot with overplotting - shapeを変える geom_point(shape=1), geom_point(shape=".") - 色の濃さを変える geom_point(colour=alpha("black", 1/3)) - jitterする。jit <- position_jitter(width=0.5); geom_jitter(position = jit) - 二次元を使い、頻度?を濃さで表示。 stat_bin(bins=10), stat_bin(binwidth=c(0.02,200)) stat_binhex() hex型で表示をする - surface plot - stats summary - draw map - annotating - weighted data *** Facetting facet_wrap, facet_grid 2つのタイプがある(facet_grid, facet_wrap) facettingされたそれぞれに対して、個別の軸スケールを適用したい場合は、scales = "free", scales = "free_x" - facet_gridは、行・列を指定。 spaceパラメータは、分割された個々のパネルの大きさをフレキシブルに変えられる。space = "free" - facet_wrapは1dのデータを二次元にする 1. これはよくある方式 2. 例では、年代別のmoview-rating **** continuous variableに対してのfacet カテゴリー化が必要。方法は3つ - 同じ大きさのbinをいくつかに cut_interval(x, n = 10) - 分ける間隔自体を指定 cut_interval(x, length=7.5) - binの数を指定 cut_number(x, n = 8) *** Coordinate systems 描画の位置を決めるシステム、3次元はまだない。 coord_cartesin, coord_polarとか smoothなどで使う統計数値のマッピンングも影響をうけてる。 **** Cartesian coordinate systems xlim, ylimが指定できる。 cartesin sysytem VS scale coord_cartesian(xlim = c(325, 500) VS scale_x_continuous(limits = c(325,500) 統計処理なんかを入れる場合、scale_x_continuousは事前処理だし、coord_cartesianは事後処理 | Aesthetic | Discrete | Continuous | |-----------------+----------+------------| | Colour and fill | brewer | gradient | | | grey | gradient2 | | | hue | gradientn | | | identity | | | | manual | | |-----------------+----------+------------| | Position(x, y) | discrete | continuous | | | | date | |-----------------+----------+------------| | Shape | shape | | | | identity | | | | manual | | |-----------------+----------+------------| | Line type | linetype | | | | identity | | | | manual | | |-----------------+----------+------------| | Size | identity | size | | | manual | | |-----------------+----------+------------| | 新幹線 | 15000 | | バス | 200 | | 電車 | 160 | *** Theme theme_get()で現在の設定がみられる。 opts() か theme_update()で変更できる データとは関係ない部分については、themeが担当する themeには、elementsとfunctionsがある また、themeそのものを設定することもできる theme_set(previoius_theme)とか **** Elements | Theme element | Type(関数が入る?) | Description | |-------------------+-------------------+-----------------------------------| | axis.line | segment | line along axis | | axis.text.x | text | x axis label | | axis.text.y | text | y axis label | | axis.ticks | segment | axis tick marks | | axis.title.x | text | horizontal tick labels | | axis.title.y | text | vertical tick labels | | | | | | legend.background | rect | background of legend | | legend.key | rect | background underneath legend keys | | legent.text | text | legend labels | | legend.title | text | legend name | | | | | | panel.background | rect | background of panel | | panel.boarder | rect | border around panel | | panel.grid.major | line | major grind lines | | panel.grid.minor | line | minor grid lines | | | | | | plot.backgrond | rect | background of facet lables | | plot.title | text | plot title | | | | | | strip.background | rect | background of facet lables | | strip.text.x | text | text for horizontal strips | | strip.text.y | text | text for vertical strips | **** Functions theme_segment, theme_text, theme_rect, theme_line, theme_blank 1. theme_segmentとtheme_lineはよく似ている。colour, fill, linetypeを担当 2. theme_rectは、backgroundの四角の描写に fill colour, border colour, size and linetype 3. theme_blankは、何も描写しない。デフォルト値を空白で上書きしたいときかな? colour = NA, fill = NAという戦術もあり 4. *** scalesとgeomsをカスタマイズ set_default_scale()で変更できる set_default_scale(対象aethetic, discrete_or_continuous, 設定関数(値),設定値への引数) という形 update_stat_defaults, update_geom_defaultsで、stat, geomを変更 Aetheticsの細目については、apendax Bにある |