生信人

找回密码
立即注册
搜索
热搜: 活动 交友 discuz
发新帖

0

收听

12

听众

278

主题
发表于 2021-9-25 19:56:29 | 查看: 1893| 回复: 0

从本篇开始介绍R中强大的绘图工具ggplot2,该工具个人感觉在R中地位不亚于Python中的Matplotlib。


本文速览

  1. 1、ggplot2之父简介

  2. 2、ggplot2绘图原理中的对象概念
  3.   几何对象(geom)
  4.   标度(scale)
  5.   图像属性(aes)
  6.   坐标系(coord)
  7.   统计变换(stat)
  8.   分面(facet)
  9.   绘图主题(theme)

  10. 3、ggplot2简单绘图
复制代码

1、ggplot2之父简介

ggplot2的作者为Hadley Wickham博士,一位为统计应用领域做出了突出贡献的统计学家,被号称为改变了R的男人;

2019年国际统计学年会(Joint Statistical Meetings, JSM)上被授于考普斯总统奖(The Committee of Presidents of Statistical Societies Awards,COPSS ),改奖项是国际统计学领域的最高奖项,被誉为 “统计学的诺贝尔奖”;

Wickham现为Rstudio(Rstudio为R语言的一种知名IDE)的首席科学家,同时为奥克兰大学、斯坦福大学和赖斯大学的统计系兼职教授;

Wickham为了使得数据科学更简洁、高效、有趣,开发了大量知名R工具包及R相关书籍,部分如下。

Packages

DATA SCIENCE WITH THE TIDYVERSE

**ggplot2**for visualising data.

dplyr for manipulating data.

tidyr for tidying data.

stringr for working with strings.

lubridate for working with date/times.


DATA IMPORT

readr for reading .csv and fwf files.

readxl for reading .xls and .xlsx files.

haven for SAS, SPSS, and Stata files.

httr for talking to web APIs.

rvest for scraping websites.

xml2 for importing XML files.


SOFTWARE ENGINEERING

devtools for general package development.

roxygen2 for in-line documentation.

testthat for unit testing

pkgdown to create beautiful package websites


Books of Wickham to use R effectively

R for Data Science, with Garrett Grolemund, introduces the key tools for doing data science with R.

ggplot2: elegant graphics for data analysis shows you how to use ggplot2 to create graphics that help you understand your data.

Advanced R helps you master R as a programming language, teaching you what makes R tick.

R packages teaches good software engineering practices for R, using packages for bundling, documenting, and testing your code.

除此之外,Wickham业余时间热爱烘焙和制作鸡尾酒,为自己做了一个网站放家庭食谱,感兴趣的可以去看看。

2、ggplot2绘图原理中的对象概念

ggplot2是基于图层图形语法(the Grammar of Graphics),可简单粗暴的理解为,您可以先绘制好图形的每一部分,然后将各部分相加形成一张完善的图形,使用ggplot2时,会反复使用如下几个对象,简单介绍:

几何对象(geom)

geometric objects,譬如散点points、 线性lines、柱状bars及方图Histogram等,可绘制的geom为:

  1. > ggplot2::geom_

  2. ggplot2::geom_rect              ggplot2::geom_curve             ggplot2::geom_errorbarh         ggplot2::geom_violin            ggplot2::geom_dotplot           
  3. ggplot2::geom_tile              ggplot2::geom_contour_filled    ggplot2::geom_linerange         ggplot2::geom_histogram         ggplot2::geom_label            
  4. ggplot2::geom_polygon           ggplot2::geom_qq                ggplot2::geom_density           ggplot2::geom_hline             ggplot2::geom_map               
  5. ggplot2::geom_density_2d        ggplot2::geom_contour           ggplot2::geom_sf                ggplot2::geom_sf_text           ggplot2::geom_sf_label         
  6. ggplot2::geom_line              ggplot2::geom_crossbar          ggplot2::geom_boxplot           ggplot2::geom_hex               ggplot2::geom_area              
  7. ggplot2::geom_quantile          ggplot2::geom_spoke             ggplot2::geom_bin2d             ggplot2::geom_raster            ggplot2::geom_count            
  8. ggplot2::geom_ribbon            ggplot2::geom_rug               ggplot2::geom_density2d_filled  ggplot2::geom_bar               ggplot2::geom_point            
  9. ggplot2::geom_jitter            ggplot2::geom_blank             ggplot2::geom_abline            ggplot2::geom_qq_line           ggplot2::geom_text              
  10. ggplot2::geom_step              ggplot2::geom_col               ggplot2::geom_freqpoly          ggplot2::get_element_tree       ggplot2::geom_pointrange        
  11. ggplot2::geom_errorbar          ggplot2::geom_function          ggplot2::geom_segment           ggplot2::geom_smooth            ggplot2::geom_vline
复制代码

标度(scale)

scales map values in the data space to values in the aesthetic space. This includes the use of colour, shape or size. Scales also draw the legend and axes, which make it possible to read the original data values from the plot(将数据取值映射到图形空间,使用颜色,形状,大小表示不同取值,使用图例,网格线展示标度),可使用的函数:

  1. > ggplot2::scale_

  2. ggplot2::scale_fill_date           ggplot2::scale_colour_ordinal      ggplot2::scale_size_area           ggplot2::scale_y_sqrt              
  3. ggplot2::scale_linetype_discrete   ggplot2::scale_shape               ggplot2::scale_fill_steps          ggplot2::scale_color_fermenter     
  4. ggplot2::scale_color_date          ggplot2::scale_colour_discrete     ggplot2::scale_fill_datetime       ggplot2::scale_colour_identity     
  5. ggplot2::scale_y_discrete          ggplot2::scale_color_identity      ggplot2::scale_continuous_identity ggplot2::scale_alpha_binned        
  6. ggplot2::scale_shape_ordinal       ggplot2::scale_fill_manual         ggplot2::scale_fill_fermenter      ggplot2::scale_colour_continuous   
  7. ggplot2::scale_y_binned            ggplot2::scale_size_ordinal        ggplot2::scale_colour_datetime     ggplot2::scale_alpha_date         
  8. ggplot2::scale_type                ggplot2::scale_x_datetime          ggplot2::scale_discrete_manual     ggplot2::scale_fill_grey           
  9. ggplot2::scale_color_brewer        ggplot2::scale_alpha_datetime      ggplot2::scale_x_sqrt              ggplot2::scale_size_identity      
  10. ggplot2::scale_color_datetime      ggplot2::scale_color_gradientn     ggplot2::scale_y_time              ggplot2::scale_linetype_binned     
  11. ggplot2::scale_colour_stepsn       ggplot2::scale_color_stepsn        ggplot2::scale_color_discrete      ggplot2::scale_size_manual
复制代码

图像属性(aes)

data to the aesthetic attributes (colour, shape, size),可使用的函数:

  1. > ggplot2::aes

  2. ggplot2::aes_all    ggplot2::aes_       ggplot2::aes_q      ggplot2::aes_string ggplot2::aes_auto   ggplot2::aes
复制代码

坐标系(coord)

information about the plot’s coordinate system(描述数据如何映射到图形,同时包含坐标轴和网格线axes, gridlines ),可使用的函数:

  1. > ggplot2::coord_

  2. ggplot2::coord_cartesian ggplot2::coord_equal     ggplot2::coord_polar     ggplot2::coord_flip      ggplot2::coord_trans     ggplot2::coord_map      
  3. ggplot2::coord_sf        ggplot2::coord_fixed     ggplot2::coord_munch     ggplot2::coord_quickmap
复制代码

统计变换(stat)

statistical transformations of the data,对数据的汇总,可使用的函数:

  1. > ggplot2::stat_

  2. ggplot2::stat_density2d_filled  ggplot2::stat_count             ggplot2::standardise_aes_names  ggplot2::stat_binhex            ggplot2::stat_sf_coordinates   
  3. ggplot2::stat_summary           ggplot2::stage                  ggplot2::stat_summary_bin       ggplot2::stat                   ggplot2::stat_sum               
  4. ggplot2::stat_identity          ggplot2::stat_summary2d         ggplot2::stat_function          ggplot2::stat_density_2d_filled ggplot2::stat_summary_hex      
  5. ggplot2::stat_ecdf              ggplot2::stat_summary_2d        ggplot2::stat_ellipse           ggplot2::stat_density           ggplot2::stat_sf               
  6. ggplot2::stat_bin2d             ggplot2::stat_spoke             ggplot2::stat_qq                ggplot2::stat_density2d         ggplot2::stat_qq_line           
  7. ggplot2::stat_smooth            ggplot2::stat_contour           ggplot2::stat_boxplot           ggplot2::stat_bin_hex           ggplot2::stat_ydensity         
  8. ggplot2::stat_bin_2d            ggplot2::stat_contour_filled    ggplot2::stat_unique            ggplot2::stat_quantile          ggplot2::stat_density_2d        
  9. ggplot2::stat_bin
复制代码

分面(facet)

A facet specifies how to break up and display subsets of data as small multiples. This is also known as conditioning or latticing/trellising.(将数据拆分为小子集,对各子集作图并联合展示,也成条件作图或网格图),可使用的函数:

  1. > ggplot2::facet_

  2. ggplot2::facet_grid ggplot2::facet_wrap ggplot2::facet_null
复制代码

绘图主题(theme)

A theme controls the finer points of display, like the font size and background colour. While the defaults in ggplot2 have been chosen with care, you may need to consult other references to create an attractive plot.(主题涉及图形更细的方面,如背景色,字体大小等)。

  1. > ggplot2::theme_

  2. ggplot2::theme_void     ggplot2::theme_test     ggplot2::theme_set      ggplot2::theme_classic  ggplot2::theme_linedraw ggplot2::theme_dark     
  3. ggplot2::theme_replace  ggplot2::theme_grey     ggplot2::theme_get      ggplot2::theme_bw       ggplot2::theme_gray     ggplot2::theme_update   
  4. ggplot2::theme_minimal  ggplot2::theme_light  
复制代码

3、ggplot2简单绘图

  1. library('ggplot2')
  2. ggplot(mpg,
复制代码

参考资料:

http://hadley.nz/


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

您需要登录后才可以回帖 登录 | 立即注册

QQ|Archiver|手机版|小黑屋|生信人

GMT+8, 2024-4-19 21:08 , Processed in 0.053687 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表