3 Data visualisation (I)

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.1.png

3.2.3 画图模板

ggplot(data = ) + (mapping = aes())

3.3 Aesthetic mappings

ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = class)) 

颜色分类
3.2.png
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, size = class))

大小分类
3.3.png
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, alpha = class))

灰度分类
3.4.png
ggplot(data=mpg)+geom_point(mapping = aes(x = displ, y = hwy, shape = class))

形状分类
3.5.png

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)

一个变量
3.6.png
ggplot(data=mpg)+geom_point(mapping= aes(x=displ, y = hwy))+facet_grid(drv~cyl)

2个变量的变化
3.7.png

你可能感兴趣的:(3 Data visualisation (I))