ggplot2作图详解:分面(faceting)

来源:互联网 发布:资生堂红色蜜露 知乎 编辑:程序博客网 时间:2024/06/10 04:38

转载自:http://blog.csdn.net/u014801157/article/details/24372507

“facet”一词这里翻译为“分面”,不知道是否准确,可以斟酌。虽然我们前面说过ggplot2分面最终的效果是一页多图,但跟通常所说的在“一个页面中绘制多个图形”还是有区别的。ggplot2仅仅体现的是数据分组,同一页面中的多个小图是完全相同的类型。真正意义的“一页多图”在ggplot2中需要通过其他方法实现。

ggplot2的分面有两种方式,分别使用 facet_wrap 或 facet_grid 函数。

<span style="color: rgb(0, 0, 195);"># 准备工作</span><span style="color: rgb(0, 42, 84);">library</span>(ggplot2)<span style="color: rgb(0, 42, 84);">set.seed</span>(<span style="color: rgb(184, 93, 0);">100</span>)d.sub <span style="color: rgb(84, 0, 84); "><strong><-</strong></span> diamonds[<span style="color: rgb(0, 42, 84);">sample</span>(<span style="color: rgb(0, 42, 84);">nrow</span>(diamonds), <span style="color: rgb(184, 93, 0);">500</span>), ]<span style="color: rgb(0, 42, 84);">head</span>(d.sub, <span style="color: rgb(184, 93, 0);">4</span>)
##       carat       cut color clarity depth table price    x    y    z## 16601  1.01 Very Good     D     SI1  62.1    59  6630 6.37 6.41 3.97## 13899  0.90     Ideal     D     SI1  62.4    55  5656 6.15 6.19 3.85## 29792  0.30     Ideal     D     SI1  61.6    56   709 4.34 4.30 2.66## 3042   0.30 Very Good     G     VS1  62.0    60   565 4.27 4.31 2.66
<span style="color: rgb(0, 42, 84);">theme_set</span>(<span style="color: rgb(0, 42, 84);">theme_bw</span>())p  <span style="color: rgb(84, 0, 84); "><strong><-</strong></span> <span style="color: rgb(0, 42, 84);">ggplot</span>(<span style="color: rgb(0, 84, 0);">data</span>=d.sub, <span style="color: rgb(0, 42, 84);">aes</span>(<span style="color: rgb(0, 84, 0);">x</span>=carat, <span style="color: rgb(0, 84, 0);">y</span>=price))

1 缠绕分面 facet_wrap

facet_warp 即“缠绕分面”,对数据分类只能应用一个标准,不同组数据获得的小形按从左到右从上到下的“缠绕”顺序进行排列:

<span style="color: rgb(0, 42, 84);">levels</span>(d.sub$cut)
## [1] "Fair"      "Good"      "Very Good" "Premium"   "Ideal"
p  + <span style="color: rgb(0, 42, 84);">geom_point</span>() + <span style="color: rgb(0, 42, 84);">facet_wrap</span>(~cut)

显然这是对数据进行分组后绘制得到的图形,这类图形对于比较不同数据的趋势非常有帮助。facet_wrap 的参数如下:

<span style="color: rgb(0, 0, 195);"># 非运行代码</span><span style="color: rgb(0, 42, 84);">facet_wrap</span>(facets, <span style="color: rgb(0, 84, 0);">nrow</span> = <span style="color: rgb(0, 0, 127);">NULL</span>, <span style="color: rgb(0, 84, 0);">ncol</span> = <span style="color: rgb(0, 0, 127);">NULL</span>, <span style="color: rgb(0, 84, 0);">scales</span> = <span style="color: rgb(209, 0, 0);">"fixed"</span>,           <span style="color: rgb(0, 84, 0);">shrink</span> = <span style="color: rgb(184, 93, 0);">TRUE</span>, <span style="color: rgb(0, 84, 0);">as.table</span> = <span style="color: rgb(184, 93, 0);">TRUE</span>, <span style="color: rgb(0, 84, 0);">drop</span> = <span style="color: rgb(184, 93, 0);">TRUE</span>)

  • facets:分面参数如 ~cut,表示用 cut 变量进行数据分类
  • nrow:绘制图形的行数
  • ncol:绘制图形的列数,一般nrow/ncol只设定一个即可
  • scales:坐标刻度的范围,可以设定四种类型。fixed 表示所有小图均使用统一坐标范围;free表示每个小图按照各自数据范围自由调整坐标刻度范围;free_x为自由调整x轴刻度范围;free_y为自由调整y轴刻度范围。
  • shrinks:也和坐标轴刻度有关,如果为TRUE(默认值)则按统计后的数据调整刻度范围,否则按统计前的数据设定坐标。
  • as.table:和小图排列顺序有关的选项。如果为TRUE(默认)则按表格方式排列,即最大值(指分组level值)排在表格最后即右下角,否则排在左上角。
  • drop:是否丢弃没有数据的分组,如果为TRUE(默认),则空数据组不绘图。

下面看看 scales 的设定效果:

p  + <span style="color: rgb(0, 42, 84);">geom_point</span>() + <span style="color: rgb(0, 42, 84);">facet_wrap</span>(~cut, <span style="color: rgb(0, 84, 0);">scales</span>=<span style="color: rgb(209, 0, 0);">"free"</span>) + <span style="color: rgb(0, 42, 84);">ggtitle</span>(<span style="color: rgb(209, 0, 0);">'scales="free"'</span>)p  + <span style="color: rgb(0, 42, 84);">geom_point</span>() + <span style="color: rgb(0, 42, 84);">facet_wrap</span>(~cut, <span style="color: rgb(0, 84, 0);">scales</span>=<span style="color: rgb(209, 0, 0);">"free_y"</span>) + <span style="color: rgb(0, 42, 84);">ggtitle</span>(<span style="color: rgb(209, 0, 0);">'scales="free_y"'</span>)


2 格网分面 facet_grid

格网分面可以应用多个标准对数据进行分组。还是先看看效果:

<span style="color: rgb(0, 42, 84);">qplot</span>(carat, price, <span style="color: rgb(0, 84, 0);">data</span>=diamonds, <span style="color: rgb(0, 84, 0);">alpha</span>=<span style="color: rgb(0, 42, 84);">I</span>(<span style="color: rgb(184, 93, 0);">0.2</span>)) + <span style="color: rgb(0, 42, 84);">facet_grid</span>(color~cut)

显然参数 color~cut 对数据的分组和小图排列有决定作用,波浪号前为小图分行标准,后面为分列标准。facet_grid 的完整用法为:

<span style="color: rgb(0, 0, 195);"># 非运行代码</span><span style="color: rgb(0, 42, 84);">facet_grid</span>(facets, <span style="color: rgb(0, 84, 0);">margins</span> = <span style="color: rgb(184, 93, 0);">FALSE</span>, <span style="color: rgb(0, 84, 0);">scales</span> = <span style="color: rgb(209, 0, 0);">"fixed"</span>, <span style="color: rgb(0, 84, 0);">space</span> = <span style="color: rgb(209, 0, 0);">"fixed"</span>, <span style="color: rgb(0, 84, 0);">shrink</span> = <span style="color: rgb(184, 93, 0);">TRUE</span>,           <span style="color: rgb(0, 84, 0);">labeller</span> = <span style="color: rgb(209, 0, 0);">"label_value"</span>, <span style="color: rgb(0, 84, 0);">as.table</span> = <span style="color: rgb(184, 93, 0);">TRUE</span>, <span style="color: rgb(0, 84, 0);">drop</span> = <span style="color: rgb(184, 93, 0);">TRUE</span>)

和facet_wrap比较,除不用设置ncol和nrow外(facets公式已经包含)外还有几个参数不同:

  • margins

注意:这不是设定图形边界的参数。它是指用于分面的包含每个变量元素所有数据的数据组。

<span style="color: rgb(0, 42, 84);">qplot</span>(carat, price, <span style="color: rgb(0, 84, 0);">data</span>=diamonds, <span style="color: rgb(0, 84, 0);">alpha</span>=<span style="color: rgb(0, 42, 84);">I</span>(<span style="color: rgb(184, 93, 0);">0.2</span>)) + <span style="color: rgb(0, 42, 84);">facet_grid</span>(color~cut, <span style="color: rgb(0, 84, 0);">margins</span>=<span style="color: rgb(184, 93, 0);">TRUE</span>)

  • space

这个参数要配合scales使用,如果为fixed(默认),所有小图的大小都一样,如果为free/free_x/free_y,小图的大小将按照坐标轴的跨度比例进行设置。

<span style="color: rgb(0, 42, 84);">qplot</span>(carat, price, <span style="color: rgb(0, 84, 0);">data</span>=diamonds, <span style="color: rgb(0, 84, 0);">alpha</span>=<span style="color: rgb(0, 42, 84);">I</span>(<span style="color: rgb(184, 93, 0);">0.2</span>)) + <span style="color: rgb(0, 42, 84);">facet_grid</span>(color~cut, <span style="color: rgb(0, 84, 0);">space</span>=<span style="color: rgb(209, 0, 0);">"free_x"</span>, <span style="color: rgb(0, 84, 0);">scales</span>=<span style="color: rgb(209, 0, 0);">"free_x"</span>)

  • labeller

这是设定小图标签的,facet_grid的函数说明档讲得比较明白,参考之。或许会在后面介绍。

下面简单看看2个以上分组标准获得什么样的图:

<span style="color: rgb(0, 42, 84);">qplot</span>(carat, price, <span style="color: rgb(0, 84, 0);">data</span>=diamonds, <span style="color: rgb(0, 84, 0);">alpha</span>=<span style="color: rgb(0, 42, 84);">I</span>(<span style="color: rgb(184, 93, 0);">0.2</span>)) + <span style="color: rgb(0, 42, 84);">facet_grid</span>(color+clarity~cut)

虽然有规律,但也是相当的复杂,哈。

3 SessionInfo

<span style="color: rgb(0, 42, 84);">sessionInfo</span>()
## R version 3.1.0 (2014-04-10)## Platform: x86_64-pc-linux-gnu (64-bit)## ## locale:##  [1] LC_CTYPE=zh_CN.UTF-8       LC_NUMERIC=C              ##  [3] LC_TIME=zh_CN.UTF-8        LC_COLLATE=zh_CN.UTF-8    ##  [5] LC_MONETARY=zh_CN.UTF-8    LC_MESSAGES=zh_CN.UTF-8   ##  [7] LC_PAPER=zh_CN.UTF-8       LC_NAME=C                 ##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            ## [11] LC_MEASUREMENT=zh_CN.UTF-8 LC_IDENTIFICATION=C       ## ## attached base packages:## [1] tcltk     stats     graphics  grDevices utils     datasets  methods  ## [8] base     ## ## other attached packages:## [1] ggplot2_0.9.3.1 zblog_0.1.0     knitr_1.5      ## ## loaded via a namespace (and not attached):##  [1] colorspace_1.2-4 digest_0.6.4     evaluate_0.5.3   formatR_0.10    ##  [5] grid_3.1.0       gtable_0.1.2     highr_0.3        labeling_0.2    ##  [9] MASS_7.3-31      munsell_0.4.2    plyr_1.8.1       proto_0.3-10    ## [13] Rcpp_0.11.1      reshape2_1.2.2   scales_0.2.4     stringr_0.6.2   ## [17] tools_3.1.0

0 0
原创粉丝点击