本文为学习记录 学习日志 R Data Science 1
##tidyverse包
#install.packages("tidyverse")
#install.packages(c("nycflights13", "gapminder", "Lahman"))
library(tidyverse)
## -- Attaching packages -------------------------------------------------- tidyverse 1.2.1 --
## √ ggplot2 3.1.0 √ purrr 0.2.5
## √ tibble 1.4.2 √ dplyr 0.7.8
## √ tidyr 0.8.2 √ stringr 1.3.1
## √ readr 1.3.1 √ forcats 0.3.0
## -- Conflicts ----------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
##该包包含了ggplot2, tibble,readr,purrr,dplyr等核心
##检查更新
#tidyverse_update()
##小技巧,指明对象来自哪个包,在包的名称后面加两个冒号,例如:
##dplyr::mutate() 表明mutate来自
##如果报错不是英文,运行以下代码
Sys.setenv(LANGUAGE = "en")
##dput()函数生成重建数据
##介绍第一章使用ggplot2进行数据可视化
library(tidyverse)
##ggplot2中内置了数据集mpg收集了 38种车型的观测数据
dim(ggplot2::mpg)##看到了吧双冒号的用法
## [1] 234 11
head(ggplot2::mpg)
## # A tibble: 6 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(~ f 18 29 p comp~
## 2 audi a4 1.8 1999 4 manua~ f 21 29 p comp~
## 3 audi a4 2 2008 4 manua~ f 20 31 p comp~
## 4 audi a4 2 2008 4 auto(~ f 21 30 p comp~
## 5 audi a4 2.8 1999 6 auto(~ f 16 26 p comp~
## 6 audi a4 2.8 1999 6 manua~ f 18 26 p comp~
#displ引擎大小,hwy燃油效率
#创建ggplot图形
ggplot(data = mpg) + #指定数据data
geom_point(mapping = aes(x = displ, y = hwy))##指定x y,图形类别
#看很轻易就得到了高颜值的图片,显示displ越大,hwy越小,负相关
#这种图的原理是图层的依次叠加,首先ggplot创建坐标系
#第一个参数是添加数据集ggplot(data=mpg)空白
#geomplot添加一个点层
#一个完整的图形就是由多个图层组成的
#每个几何对象函数都有mapping参数,aes()函数指定对应x,y的变量分别是什么
##绘图模板(发福利),用自己的数据替换尖括号部分
#ggplot(data=<数据集>)+
# <geom_哪种函数>(mapping=aes(x=变量1,y=变量2))
#其实更复杂的图只是在扩展这个模板,描绘更多细节
<“The greatest value of a picture is when it forces us to notice what we never expected to see.” — John Tukey>
#mpg数据集中的class是对车进行的分类,小型,中型等
#可以通过向二维点图中添加第上变量,比如class,将其映射为**图形属性**
#图形属性属于图中对象的可视化属性,包括点的**大小,颜色,形状**
#通过将图中的**图形属性**映射为数据集中的变量,以下示例将点的颜色映射为class变量
#需要在函数aes()中将图形属性名称和变量关联起来,如下关联了color与class变量
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = class))
#图中给出的信息提示了SUV这种车不大一样,属于离群组
##下面将图形属性大小size与class变量关联起来,但是注意size是有序的,而class是无序的
#所以会得到warning: Using size for a discrete variable is not advised.
#当然了图也会比较丑,告诉我们属性与变量的映射选择要合适
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, size = class))
## Warning: Using size for a discrete variable is not advised.
##继续图形属性还包括alpha(点的透明度),shape(点的形状)
# 上(发现透明度属性建议离散型变量。最好是有序的)
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, alpha = class))#alpha表示点的透明度
## Warning: Using alpha for a discrete variable is not advised.
# 下(发现形状不够用了,总共就6个,class却又7种
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, shape = class))#shape表示点的形状
## Warning: 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.
## Warning: Removed 62 rows containing missing values (geom_point).
##除了变量映射图形属性,还可手动设置图形属性,如下设置点为blue
#这时的颜色不会传达变量信息,仅仅改变外观而已
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy), color = "blue")
#但是
##R中有25种用数值进行标识的内置形,空心0-14边界颜色由color决定
#15-20实心形状由fill