R语言之ggplot

Python015

R语言之ggplot,第1张

ggplot2的核心理念是将绘图与数据分离,数据相关的绘图与数据无关的绘图分离。按图层作图,保有命令式作图的调整函数,使其更具灵活性,并将常见的统计变换融入到了绘图中。

ggplot的绘图有以下几个特点:第一,有明确的起始(以ggplot函数开始)与终止(一句语句一幅图);其二,图层之间的叠加是靠“+”号实现的,越后面其图层越高。

ggplot2里的所有函数可以分为以下几类:

一个图形对象就是一个包含数据,映射,图层,标度,坐标和分面的列表,外加组件options

ggplot(数据, 映射) geom_xxx(映射, 数据) stat_xxx(映射, 数据)

点(point, text):往往只有x、y指定位置,有shape但没有fill

线(line,vline,abline,hline,stat_function等):一般是基于函数来处理位置

射(segment):特征是指定位置有xend和yend,表示射线方向

面(tile, rect):这类一般有xmax,xmin,ymax,ymin指定位置

棒(boxplot,bin,bar,histogram):往往是二维或一维变量,具有width属性

带(ribbon,smooth):透明是特征是透明的fill

补:包括rug图,误差棒(errorbar,errorbarh)

然后,就是按照你的需要一步步加图层了(使用“+”)。

ggplot2是R语言第三方可视化扩展包,在某种程度上它基本代替了R可视化。该包是RStudio首席科学家Hadley Wickham读博期间的作品,它强大的画图逻辑使得它称为R最流行的包之一。更多知识分享请到 https://zouhua.top/

ggplot2 is based on the grammar of graphics, the idea that you can build every graph from the same few components: a data set, a set of geoms—visual marks that represent data points, and a coordinate system。

To display data values, map variables in the data set to aesthetic properties of the geom like size, color, and x and y locations

aesthetic map variables in data to graphic properties. mappings control the relationship between data and graphic properties.

Aesthetic mapping means "something you can see"

Each type of geom accepts only a subset of all aesthetics-refer to the geom help pages to see what mappings each geom accepts. Aesthetic mappings are set with the aes() function.

scales map values in the data space to values in the aesthetic space(color, size, shape ...). scales are reported on the plot using axes and legends. Control aesthetic mapping.

Scales are modified with a series of functions using a scale_<aesthetic>_<type>naming scheme

The following arguments are common to most scales in ggplot2:

geometric objects are the actual marks we put on a plot

A plot must have at least one geometric object, and there is no upper limit. adding a geom by using the + operator.

It's often useful to transform your data before plotting, and that's what statistical transformations do.

Every geom function has a default statistic:

The ggplot2 theme system handles non-data plot elements such as

Built-in themes include: