ggplot2が『The Grammar of Graphics』を反映していること

  • ggplot2パッケージはRのグラフィクスパッケージ
  • The Grammar of Graphicsの精神で作られているという
  • どういうことかというと:
  • ggplot()関数によってつくられるオブジェクトは、次のような要素を持つlist
    • $data
    • $layers
    • $scales
    • $mapping
    • $options
    • $coordinates
    • $facet
    • $plot_env
  • ggplot()関数はThe Grammar of Graphicsに書かれているように、「データ」を取り込み($data)、それのうち、何の要素を描く対象($mapping)にし、スケール($scale)やら座標系($coodinate)やらを指定し、最終的にそれを持って、描図する、という作りになっている
  • それを実現する諸関数を納めたのがggplot2パッケージ
  • このようにggplot2パッケージはThe Grammar of Graphicsの方針を踏襲している
  • 以下はメモ
# iris
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
gp <- ggplot(iris)
gp <- gp + aes(Sepal.Length,Sepal.Width)
gp + geom_point()
gp + geom_point(aes(colour = Petal.Length))
gp + geom_point(aes(colour = Species))
gp + geom_point(aes(colour = Species)) + scale_colour_brewer()
gp.1 <- gp + geom_point(aes(colour = Species)) + scale_colour_brewer()
gp.2 <- gp.1 + stat_smooth()
gp.3 <- gp.1 + stat_sum()
gp.4 <- gp.1 + coord_polar()
gp.5 <- ggplot(iris) + aes(Sepal.Length,Sepal.Width*10)
gp.6 <- gp + geom_point() + stat_smooth()
gp.7 <- gp + geom_point() + coord_trans(x = "exp",y = "log10") + stat_smooth()
gp.8 <- ggplot(iris) + aes(log(Sepal.Length),exp(Sepal.Width)) + geom_point() + stat_smooth()
gp.6
dev.new()
gp.7
dev.new()
gp.8
> str(gp.7)
List of 8
 $ data       :'data.frame':    150 obs. of  5 variables:
  ..$ Sepal.Length: num [1:150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
  ..$ Sepal.Width : num [1:150] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
  ..$ Petal.Length: num [1:150] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
  ..$ Petal.Width : num [1:150] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
  ..$ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ layers     :List of 2
  ..$ :Classes 'proto', 'environment' <environment: 0x000000000f3ebab8> 
  ..$ :Classes 'proto', 'environment' <environment: 0x000000000f558bd0> 
 $ scales     :Reference class 'Scales' [package "ggplot2"] with 1 fields
  ..$ scales: list()
  ..and 20 methods, of which 9 are possibly relevant:
  ..  add, clone, find, get_scales, has_scale, initialize, input, n, non_position_scales
 $ mapping    :List of 2
  ..$ x: symbol Sepal.Length
  ..$ y: symbol Sepal.Width
 $ options    :List of 1
  ..$ labels:List of 2
  .. ..$ x: chr "Sepal.Length"
  .. ..$ y: chr "Sepal.Width"
 $ coordinates:List of 2
  ..$ trans :List of 2
  .. ..$ x:List of 6
  .. .. ..$ name     : chr "power-2.718282"
  .. .. ..$ transform:function (x)  
  .. .. ..$ inverse  :function (x)  
  .. .. ..$ breaks   :function (x)  
  .. .. ..$ format   :function (x)  
  .. .. ..$ domain   : num [1:2] -Inf Inf
  .. .. ..- attr(*, "class")= chr "trans"
  .. ..$ y:List of 6
  .. .. ..$ name     : chr "log-10"
  .. .. ..$ transform:function (x)  
  .. .. ..$ inverse  :function (x)  
  .. .. ..$ breaks   :function (x)  
  .. .. ..$ format   :function (x)  
  .. .. ..$ domain   : num [1:2] 1e-100 Inf
  .. .. ..- attr(*, "class")= chr "trans"
  ..$ limits:List of 2
  .. ..$ x: NULL
  .. ..$ y: NULL
  ..- attr(*, "class")= chr [1:2] "trans" "coord"
 $ facet      :List of 1
  ..$ shrink: logi TRUE
  ..- attr(*, "class")= chr [1:2] "null" "facet"
 $ plot_env   :<environment: R_GlobalEnv> 
 - attr(*, "class")= chr "ggplot"
>