3.1 前准备
R中数据的可视化基本都是基于ggplot2的,运行前准备,安装必要的包
install.packages("tidyverse")
library(tidyverse)
3.2 第一步
3.2.1 数据的准备
ggplot2::mpg
displ:汽车的能源,汽油
hwy:fuel efficiency
3.2.2 创建ggplot
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy))
3.2.3 画图模板
ggplot(data = <DATA>) + <GEOM_FUNCTION>(mapping = aes(<MAPPINGS>))
3.3 Aesthetic mappings
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = class))
颜色分类ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, size = class))
大小分类ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, alpha = class))
灰度分类ggplot(data=mpg)+geom_point(mapping = aes(x = displ, y = hwy, shape = class))
形状分类3.4 Common problems
严格的写法可以保证少犯错误
3.5 Facets
facet_wrap()函数
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) + facet_wrap(~ class, nrow = 2)
一个变量ggplot(data=mpg)+geom_point(mapping= aes(x=displ, y = hwy))+facet_grid(drv~cyl)
2个变量的变化