从本篇开始介绍R中强大的绘图工具ggplot2,该工具个人感觉在R中地位不亚于Python中的Matplotlib。
本文介绍ggplot2的一些基本概念
1、ggplot2之父简介
2、ggplot2绘图原理中的对象概念
几何对象(geom)
标度(scale)
图像属性(aes)
坐标系(coord)
统计变换(stat)
分面(facet)
绘图主题(theme)
3、ggplot2简单绘图
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.
ggplot2是基于图层图形语法(the Grammar of Graphics),可简单粗暴的理解为,您可以先绘制好图形的每一部分,然后将各部分相加形成一张完善的图形,使用ggplot2时,会反复使用如下几个对象,简单介绍:
geometric objects,譬如散点points、 线性lines、柱状bars及方图Histogram等,可绘制的geom为:
> ggplot2::geom_
ggplot2::geom_rect ggplot2::geom_curve ggplot2::geom_errorbarh ggplot2::geom_violin ggplot2::geom_dotplot
ggplot2::geom_tile ggplot2::geom_contour_filled ggplot2::geom_linerange ggplot2::geom_histogram ggplot2::geom_label
ggplot2::geom_polygon ggplot2::geom_qq ggplot2::geom_density ggplot2::geom_hline ggplot2::geom_map
ggplot2::geom_density_2d ggplot2::geom_contour ggplot2::geom_sf ggplot2::geom_sf_text ggplot2::geom_sf_label
ggplot2::geom_line ggplot2::geom_crossbar ggplot2::geom_boxplot ggplot2::geom_hex ggplot2::geom_area
ggplot2::geom_quantile ggplot2::geom_spoke ggplot2::geom_bin2d ggplot2::geom_raster ggplot2::geom_count
ggplot2::geom_ribbon ggplot2::geom_rug ggplot2::geom_density2d_filled ggplot2::geom_bar ggplot2::geom_point
ggplot2::geom_jitter ggplot2::geom_blank ggplot2::geom_abline ggplot2::geom_qq_line ggplot2::geom_text
ggplot2::geom_step ggplot2::geom_col ggplot2::geom_freqpoly ggplot2::get_element_tree ggplot2::geom_pointrange
ggplot2::geom_errorbar ggplot2::geom_function ggplot2::geom_segment ggplot2::geom_smooth ggplot2::geom_vline
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(将数据取值映射到图形空间,使用颜色,形状,大小表示不同取值,使用图例,网格线展示标度),可使用的函数:
> ggplot2::scale_
ggplot2::scale_fill_date ggplot2::scale_colour_ordinal ggplot2::scale_size_area ggplot2::scale_y_sqrt
ggplot2::scale_linetype_discrete ggplot2::scale_shape ggplot2::scale_fill_steps ggplot2::scale_color_fermenter
ggplot2::scale_color_date ggplot2::scale_colour_discrete ggplot2::scale_fill_datetime ggplot2::scale_colour_identity
ggplot2::scale_y_discrete ggplot2::scale_color_identity ggplot2::scale_continuous_identity ggplot2::scale_alpha_binned
ggplot2::scale_shape_ordinal ggplot2::scale_fill_manual ggplot2::scale_fill_fermenter ggplot2::scale_colour_continuous
ggplot2::scale_y_binned ggplot2::scale_size_ordinal ggplot2::scale_colour_datetime ggplot2::scale_alpha_date
ggplot2::scale_type ggplot2::scale_x_datetime ggplot2::scale_discrete_manual ggplot2::scale_fill_grey
ggplot2::scale_color_brewer ggplot2::scale_alpha_datetime ggplot2::scale_x_sqrt ggplot2::scale_size_identity
ggplot2::scale_color_datetime ggplot2::scale_color_gradientn ggplot2::scale_y_time ggplot2::scale_linetype_binned
ggplot2::scale_colour_stepsn ggplot2::scale_color_stepsn ggplot2::scale_color_discrete ggplot2::scale_size_manual
data to the aesthetic attributes (colour, shape, size),可使用的函数:
> ggplot2::aes
ggplot2::aes_all ggplot2::aes_ ggplot2::aes_q ggplot2::aes_string ggplot2::aes_auto ggplot2::aes
information about the plot’s coordinate system(描述数据如何映射到图形,同时包含坐标轴和网格线axes, gridlines ),可使用的函数:
> ggplot2::coord_
ggplot2::coord_cartesian ggplot2::coord_equal ggplot2::coord_polar ggplot2::coord_flip ggplot2::coord_trans ggplot2::coord_map
ggplot2::coord_sf ggplot2::coord_fixed ggplot2::coord_munch ggplot2::coord_quickmap
statistical transformations of the data,对数据的汇总,可使用的函数:
> ggplot2::stat_
ggplot2::stat_density2d_filled ggplot2::stat_count ggplot2::standardise_aes_names ggplot2::stat_binhex ggplot2::stat_sf_coordinates
ggplot2::stat_summary ggplot2::stage ggplot2::stat_summary_bin ggplot2::stat ggplot2::stat_sum
ggplot2::stat_identity ggplot2::stat_summary2d ggplot2::stat_function ggplot2::stat_density_2d_filled ggplot2::stat_summary_hex
ggplot2::stat_ecdf ggplot2::stat_summary_2d ggplot2::stat_ellipse ggplot2::stat_density ggplot2::stat_sf
ggplot2::stat_bin2d ggplot2::stat_spoke ggplot2::stat_qq ggplot2::stat_density2d ggplot2::stat_qq_line
ggplot2::stat_smooth ggplot2::stat_contour ggplot2::stat_boxplot ggplot2::stat_bin_hex ggplot2::stat_ydensity
ggplot2::stat_bin_2d ggplot2::stat_contour_filled ggplot2::stat_unique ggplot2::stat_quantile ggplot2::stat_density_2d
ggplot2::stat_bin
A facet specifies how to break up and display subsets of data as small multiples. This is also known as conditioning or latticing/trellising.(将数据拆分为小子集,对各子集作图并联合展示,也成条件作图或网格图),可使用的函数:
> ggplot2::facet_
ggplot2::facet_grid ggplot2::facet_wrap ggplot2::facet_null
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.(主题涉及图形更细的方面,如背景色,字体大小等)。
> ggplot2::theme_
ggplot2::theme_void ggplot2::theme_test ggplot2::theme_set ggplot2::theme_classic ggplot2::theme_linedraw ggplot2::theme_dark
ggplot2::theme_replace ggplot2::theme_grey ggplot2::theme_get ggplot2::theme_bw ggplot2::theme_gray ggplot2::theme_update
ggplot2::theme_minimal ggplot2::theme_light
library('ggplot2')
ggplot(mpg,
aes(displ, cty, colour = class)) +#图像属性(aes)设置
geom_point()#几何对象(geom)设置,此处geom为散点图
参考资料:
https://github.com/hadley/
http://hadley.nz/