第一章 使用ggplot2进行数据可视化(上)
一、安装和加载tidyverse软件包
#安装
install.packages("tidyverse")
#加载
library(tidyverse)
1、mpg数据框
mpg
# A tibble: 234 x 11
manufacturer model displ year cyl trans drv cty hwy fl class
<chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compact
2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compact
3 audi a4 2 2008 4 manual(m6) f 20 31 p compact
4 audi a4 2 2008 4 auto(av) f 21 30 p compact
5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compact
6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compact
7 audi a4 3.1 2008 6 auto(av) f 18 27 p compact
8 audi a4 quattro 1.8 1999 4 manual(m5) 4 18 26 p compact
9 audi a4 quattro 1.8 1999 4 auto(l5) 4 16 25 p compact
10 audi a4 quattro 2 2008 4 manual(m6) 4 20 28 p compact
# ... with 224 more rows
2、ggplot2绘图,ggplot2为tidyverse的一个软件包
绘制mpg的图形,displ放在x轴,hwy放在y轴。
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
3、ggplot2绘图模板
将尖括号里面的数据替换为数据集、几何对象函数或者映射集合即可。
ggplot(data = <DATA>) +
<GEOM_FUNCTION>(mapping = aes(<MAPPINGS>))
4、绘制其他图
ggplot(data = mpg) +
geom_point(mapping = aes(x = hwy, y = cyl))
二、图形的属性
图形的属性是图中对象的可视化属性,包括数据点的大小、形状和颜色。比如向上图中二维的散点图中添加第三个变量,比如class,方式是将它映射为图形属性。在aes()函数中将图形属性名称和变量名称关联起来。ggplot2会为每一个变量值分配唯一的图形属性水平(本例中是唯一的颜色),这个过程称为标度变换。
还可以将class映射为控制数据点透明度的alpha图形属性或者点状
class映射为透明度alpha或者点的形状
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, alpha = class))
#运行后
ggplot(data = mpg) +
+ geom_point(mapping = aes(x = displ, y = hwy, alpha = class))
Warning message:
Using alpha for a discrete variable is not advised.
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, shape= class))
#运行后
> ggplot(data = mpg) +
+ geom_point(mapping = aes(x = displ, y = hwy, shape= class))
Warning messages:
1: The shape palette can deal with a maximum of 6 discrete values because more than 6 becomes
difficult to discriminate; you have 7. Consider specifying shapes manually if you must have them.
2: Removed 62 rows containing missing values (geom_point).
出现了警告信息Warning messages。 ggplot2只能同时使用6种形状,超出时不显示。
对几何对象设置图形属性
#对几何对象设置图形属性
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy), color = "blue")
三、分面
通过函数facet_wrap(),注意其用法
#分面
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_wrap(~class,nrow = 2)
通过函数facet_grid(),注意其用法
#分面
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_grid(drv ~ cyl)