# ggplo2包再学习第一天_2020-02-19W
## 1.设置工作目录
setwd("./2020-02-19W/")
## 2.安装和导入(首次使用ggplot2时需要安装)
# install.packages("ggplot2") # 我已经安装过了,所以这里注释掉即可
library(ggplot2)
## 3.开始我们第一天的学习内容
### 3.1 什么是图形语法?
#@ ggplot2的绘图方法就是:the grammer of graphics
# ggplot2绘制的每张图都有以下每个部分组成:
#### 1.Data数据,绘图总是要有数据的,这是图的本源
#### 2.Layers层,几何图形元素和统计学变换方法
#### 3.Scales缩放变换,由数据空间到美学空间的Scales变换,例如颜色、大小、形状等等
#### 4.A coordinate system坐标系统,如笛卡尔坐标系统、极坐标系统
#### 5.Facet面,设置图形的分面或分格,每个分面或分格中又是一个完整的图形,最终由这样分面小图所组成的组合大图
#### 6.Theme主题,设置图形的主题的,例如设定图的背景颜色、字体字号或颜色等等,当然ggplo2包中有自己默认的主题,直接拿来用,但是如果感觉不美观的话,还可以自己再修改。
#@ 其实,图形语法说白了就是从数据空间到几何空间然后再到美学空间的映射。而每个用ggplot2中的图形语法所绘制的图形都是由数据、几何和美学三个主要的元素组成的。
### 3.2 测试数据:石油经济数据mpg
# Fuel economy data from 1999 and 2008 for 38 popular models of car #
#@ 导入R内置数据集包(其实是不必要导入的,当你打开R时,直接就可以用)
library(datasets)
#@ 载入数据
data(mpg)
#@ 获取数据维度信息
dim(mpg)
# [1] 234 11
#@ 获取数据类型
class(mpg)
# [1] "tbl_df" "tbl" "data.frame"
#@ 查看其数据结构
str(mpg)
# Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 234 obs. of 11 variables:
# $ manufacturer: chr "audi" "audi" "audi" "audi" ...
# $ model : chr "a4" "a4" "a4" "a4" ...
# $ displ : num 1.8 1.8 2 2 2.8 2.8 3.1 1.8 1.8 2 ...
# $ year : int 1999 1999 2008 2008 1999 1999 2008 1999 1999 2008 ...
# $ cyl : int 4 4 4 4 6 6 6 4 4 4 ...
# $ trans : chr "auto(l5)" "manual(m5)" "manual(m6)" "auto(av)" ...
# $ drv : chr "f" "f" "f" "f" ...
# $ cty : int 18 21 20 21 16 18 18 18 16 20 ...
# $ hwy : int 29 29 31 30 26 26 27 26 25 28 ...
# $ fl : chr "p" "p" "p" "p" ...
# $ class : chr "compact" "compact" "compact" "compact" ...
#@ mpg数据简单统计
summary(mpg)
# manufacturer model displ
# Length:234 Length:234 Min. :1.600
# Class :character Class :character 1st Qu.:2.400
# Mode :character Mode :character Median :3.300
# Mean :3.472
# 3rd Qu.:4.600
# Max. :7.000
# year cyl trans
# Min. :1999 Min. :4.000 Length:234
# 1st Qu.:1999 1st Qu.:4.000 Class :character
# Median :2004 Median :6.000 Mode :character
# Mean :2004 Mean :5.889
# 3rd Qu.:2008 3rd Qu.:8.000
# Max. :2008 Max. :8.000
# drv cty hwy
# Length:234 Min. : 9.00 Min. :12.00
# Class :character 1st Qu.:14.00 1st Qu.:18.00
# Mode :character Median :17.00 Median :24.00
# Mean :16.86 Mean :23.44
# 3rd Qu.:19.00 3rd Qu.:27.00
# Max. :35.00 Max. :44.00
# fl class
# Length:234 Length:234
# Class :character Class :character
# Mode :character Mode :character
#@ 查看前6行数据
head(mpg)
# # A tibble: 6 x 11
# manufacturer model displ year cyl trans drv cty hwy
# <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int>
# 1 audi a4 1.8 1999 4 auto~ f 18 29
# 2 audi a4 1.8 1999 4 manu~ f 21 29
# 3 audi a4 2 2008 4 manu~ f 20 31
# 4 audi a4 2 2008 4 auto~ f 21 30
# 5 audi a4 2.8 1999 6 auto~ f 16 26
# 6 audi a4 2.8 1999 6 manu~ f 18 26
# # ... with 2 more variables: fl <chr>, class <chr>
#@ 查看后6行数据
tail(mpg)
# # A tibble: 6 x 11
# manufacturer model displ year cyl trans drv cty hwy
# <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int>
# 1 volkswagen pass~ 1.8 1999 4 auto~ f 18 29
# 2 volkswagen pass~ 2 2008 4 auto~ f 19 28
# 3 volkswagen pass~ 2 2008 4 manu~ f 21 29
# 4 volkswagen pass~ 2.8 1999 6 auto~ f 16 26
# 5 volkswagen pass~ 2.8 1999 6 manu~ f 18 26
# 6 volkswagen pass~ 3.6 2008 6 auto~ f 17 26
# # ... with 2 more variables: fl <chr>, class <chr>
### 3.3 用ggplot2初次绘图
ggplot(mpg, aes(x = displ, y = hwy)) + #增加图形语法
geom_point() # 几何元素
#@ 由此图,我们可以看出ggplot2的默认点的颜色是黑色,默认背景颜色是灰色,默认坐标中有规则网格,但这张图充其量可以称为草图,因为有太多的参数可以调整和优化了,我们这里再出一张优化的图形,后续具体讲解如何绘制出美观的ggplot2点图!
# 单纯点图 #
ggplot(mpg, aes(x = displ, y = hwy, colour = class)) +
geom_point(size = 3) +
xlab("Engine displacement") +
ylab("Highway miles per gallon") +
theme_bw() +
theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
theme(legend.text = element_text(size = 15, colour = "black")) +
theme(legend.title = element_text(face = "bold", size = 20)) +
theme(axis.title.x =element_text(size=16), axis.title.y=element_text(size=16))
# 为点添加文字注释 #
library(ggrepel)
ggplot(mpg, aes(x = displ, y = hwy, colour = class)) +
geom_point(size = 3) +
geom_text_repel(aes(label = model)) +
xlab("Engine displacement") +
ylab("Highway miles per gallon") +
theme_bw() +
theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
theme(legend.text = element_text(size = 15, colour = "black")) +
theme(legend.title = element_text(face = "bold", size = 20)) +
theme(axis.title.x =element_text(size=16), axis.title.y=element_text(size=16))
## 4.好了,今天就到这里!
#@ 有位哲人说过:目标再远,一步一步终会到达。
sessionInfo()
# R version 3.6.2 (2019-12-12)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows 10 x64 (build 18363)
#
# Matrix products: default
#
# locale:
# [1] LC_COLLATE=Chinese (Simplified)_China.936
# [2] LC_CTYPE=Chinese (Simplified)_China.936
# [3] LC_MONETARY=Chinese (Simplified)_China.936
# [4] LC_NUMERIC=C
# [5] LC_TIME=Chinese (Simplified)_China.936
#
# attached base packages:
# [1] stats graphics grDevices utils datasets methods
# [7] base
#
# other attached packages:
# [1] ggrepel_0.8.1 ggplot2_3.2.1
#
# loaded via a namespace (and not attached):
# [1] Rcpp_1.0.3 rstudioapi_0.11 magrittr_1.5
# [4] MASS_7.3-51.4 tidyselect_1.0.0 munsell_0.5.0
# [7] colorspace_1.4-1 R6_2.4.1 rlang_0.4.4
# [10] fansi_0.4.1 dplyr_0.8.4 tools_3.6.2
# [13] grid_3.6.2 packrat_0.5.0 gtable_0.3.0
# [16] utf8_1.1.4 cli_2.0.1 withr_2.1.2
# [19] digest_0.6.24 lazyeval_0.2.2 assertthat_0.2.1
# [22] tibble_2.1.3 lifecycle_0.1.0 crayon_1.3.4
# [25] farver_2.0.3 purrr_0.3.3 vctrs_0.2.2
# [28] glue_1.3.1 labeling_0.3 compiler_3.6.2
# [31] pillar_1.4.3 scales_1.1.0 pkgconfig_2.0.3